Elixir provides a set of functions that enumerate over collections which implement the Enumerable
protocol. These collections include lists and maps.
Enum.filter/2
takes two parameters:
- a collection, and
- a function which returns a
boolean
Filtering a list:
Filtering maps:
Note that the functions in the
Enum
module are eager. TheStream
module allows lazy enumeration of enumerables and provides infinite streams.
A lazy filter using streams:
Additional reading: