Provides a set of algorithms that enumerate over collections according to the
Enumerable
protocol:
iex> Enum.map([1, 2, 3], fn(x) -> x * 2 end)
[2,4,6]
Some particular types, like dictionaries, yield a specific format on
enumeration. For dicts, the argument is always a {key, value}
tuple:
iex> dict = %{a: 1, b: 2}
iex> Enum.map(dict, fn {k, v} -> {k, v * 2} end)
[a: 2, b: 4]
Note that the functions in the Enum
module are eager: they always start
the enumeration of the given collection. The Stream
module allows
lazy enumeration of collections and provides infinite streams.
Since the majority of the functions in Enum
enumerate the whole
collection and return a list as result, infinite streams need to
be carefully used with such functions, as they can potentially run
forever. For example:
Enum.each Stream.cycle([1,2,3]), &IO.puts(&1)