Logo

Programming-Idioms

  • Rust
  • Ruby

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(&:p)

select is also aliased to find_all.
let y: Vec<_> = x.iter().filter(p).collect();

This collects into a vector (other collections are possible)
for Item of X loop
   if P (Item) then
      Y.Append (Item);
   end if;
end loop;

New implementation...