Filtration

Contents
  • filter
    • filterfalse
  • compress
    • Recursively
    • With zip and catMaybes
    • With zip and mapMaybe
    • With zip, map, and filter

In contrast with takewhile which stops the first time it encounters a non-matching value, filter continues past non-matching values and produces a list containing all of the values that satisfy the predicate.

filter

Here we use filterfilter in Python to select, from among the natural numbers, only those that are even:

>>> it = filter(lambda x: x % 2 == 0, count(1))

>>> list(islice(it, 10))
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

Sign up for access to the full page, plus the complete archive and all the latest content.