concept Enum in category elixir

appears as: Enum
Elixir in Action

This is an excerpt from Manning's book Elixir in Action.

There are many operations you can do with lists, but this section mentions only a couple of the most basic ones. For a detailed reference, see the documentation for the List module (https://hexdocs.pm/elixir/List.html). There are also many helpful services in the Enum module (https://hexdocs.pm/elixir/Enum.html).

The Enum module deals with many different enumerable structures and is not limited to lists. The concept of enumerables will be explained in detail in chapter 4, when we discuss protocols.

Enum.each/2 is just one example of an iteration powered by a higher-order function. Elixir’s standard library provides many other useful iteration helpers in the Enum module. The Enum module is a Swiss Army knife for loops and iterations; it contains a lot of useful functions. You should spend some time researching the module documentation (https://hexdocs.pm/elixir/Enum.html). Here we’ll look at some of the most frequently used Enum functions.

To make the iteration happen, you have to send the stream to an Enum function, such as each, map, or filter. You can also use the Enum.to_list/1 function, which converts any kind of enumerable into a list:

iex(5)> Enum.to_list(stream)    #1  
[2, 4, 6]

#1   At this point, stream iteration takes place.

Enum.to_list/1 (and any other Enum function, for that matter) is an eager operation. It immediately starts iterating through the input and creates the result. In doing so, Enum.to_list/1 requests that the input enumerable start producing values. This is why the output of the stream is created when you send it to an Enum function.