Logo

Programming-Idioms

  • C
  • Php

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.

function p($element) {  /* .... */ }

$y = array_filter ($x, "p");

Filtering code goes to function p which is called for each element of array $x,
and if function returns true, that element goes to array $y.

for Item of X loop
   if P (Item) then
      Y.Append (Item);
   end if;
end loop;

New implementation...