Since the early days, Elixir has allowed us to easily process collections using the Enum
and Stream
modules. In the first case, the collection is traversed as soon as it reaches an Enum
call, if we use the respective Stream
function instead, we can delay the actual enumeration of the collection until the very last moment. This is why we say Stream
functions are lazy and Enum
functions are eager.
As a result, if we transform a collection by chaining Enum.map/2
calls, an intermediate collection will be created for each call to an Enum
function. This behavior contrasts with the behavior we would get if we chained a bunch of Stream.map/2
calls. In the latter case, the entire collection would be traversed at most once, and only when the resulting stream reaches an Enum
function (such as Enum.to_list/1
), forcing each element of the stream to be evaluated.
The Enum
and Stream
modules provide an invaluable set of tools to work with collections, but do not leverage the Elixir capability to run...