Logo

Programming-Idioms

  • C
  • D

Idiom #57 Filter list

Create the list y containing the items from the list x that satisfy the predicate p. Respect the original ordering. Don't modify x in-place.

import std.algorithm;
auto y = [1, 2, 3, 4, 5].filter!(a => a%2==0);

Actual example.
y is a range containing only [2, 4]
import std.algorithm.iteration;
auto y = x.filter!(p);

Using UFCS, we can write filter!(p)(x) as x.filter!(p)
for Item of X loop
   if P (Item) then
      Y.Append (Item);
   end if;
end loop;

New implementation...