Logo

Programming-Idioms

  • Lisp
  • C++

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.

#include <ranges>
auto y = x | std::views::filter(p);
#include <algorithm>
#include <iterator>
std::copy_if (x.begin (), x.end (), std::back_inserter(y), p);
(setf y (remove-if-not p x))
for Item of X loop
   if P (Item) then
      Y.Append (Item);
   end if;
end loop;

New implementation...