Logo

Programming-Idioms

  • D
  • Smalltalk
  • Ada

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.

y := x select: [:item | item p].
import std.algorithm.iteration;
auto y = x.filter!(p);

Using UFCS, we can write filter!(p)(x) as x.filter!(p)
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]
for Item of X loop
   if P (Item) then
      Y.Append (Item);
   end if;
end loop;
(def y (filter p x))

New implementation...