Appearance
Enumerables and Streams
Read first: Recursion
Source: Elixir guide, Enumerables and Streams.
Most work with collections uses Enum and Stream, not recursion that you write by hand.
Enumerables
Enum works on anything that implements the Enumerable ProtocolA mechanism for polymorphism: the behavior depends on the data type, and the dispatch is on the type of the first argument. Anyone can implement a protocol, at any time.Read the page · Glossary. Lists, maps, and ranges are all enumerable.
elixir
Enum.map([1, 2, 3], fn x -> x * 2 end) #=> [2, 4, 6]
Enum.map(%{1 => 2, 3 => 4}, fn {k, v} -> k * v end) #=> [2, 12]
Enum.map(1..3, fn x -> x * 2 end) #=> [2, 4, 6]Enum only enumerates. For operations that are specific to one type, for example to insert at a position, use the module of that type (List.insert_at/3). See the Enum cheatsheet for the full list.
Eager and lazy
Every Enum function is eager. Many of them take an enumerable and return a list, so a Pipe operator|> takes the result of the expression on its left and passes it as the first argument of the call on its right. It makes the data that changes easy to see.Read the page · Glossary builds an intermediate list at every step.
The pipe operator
|> passes the result on its left as the first argument of the call on its right:
elixir
odd? = fn x -> rem(x, 2) != 0 end
1..100_000 |> Enum.map(&(&1 * 3)) |> Enum.filter(odd?) |> Enum.sum() #=> 7500000000
# same as: Enum.sum(Enum.filter(Enum.map(1..100_000, &(&1 * 3)), odd?))3 steps
Streams
Stream has lazy versions of these functions. A stream is a value that describes a computation, so nothing runs until you pass it to Enum.
elixir
1..100_000 |> Stream.map(&(&1 * 3)) |> Stream.filter(odd?) |> Enum.sum() #=> 7500000000Streams are good for large or infinite collections:
elixir
stream = Stream.cycle([1, 2, 3])
Enum.take(stream, 10) #=> [1, 2, 3, 1, 2, 3, 1, 2, 3, 1]Enum.map/2 on that stream never finishes. Stream.resource/3 wraps resources so that they open before the enumeration and close after it, also after a failure. File.stream!/1 uses it: "path" |> File.stream!() |> Enum.take(10) reads only ten lines.
Start with Enum.map/2 and Enum.reduce/3. Use Stream only when you need laziness.
Under the hood Our own text. It is not from the official docs.
Where the intermediate lists live. Enum.map builds a whole new list on the process heap before Enum.filter starts, and filter builds another list. We ran 1..2_000_000 |> Enum.map(&(&1 * 3)) |> Enum.filter(odd?) |> Enum.sum() in a new process. The heap of that process was 75 MB. The Stream version of the same Pipe operator|> takes the result of the expression on its left and passes it as the first argument of the call on its right. It makes the data that changes easy to see.Read the page · Glossary composes the steps into functions and runs each number through all of them, one at a time. It builds up nothing, and the heap ended at 609 words.
Why it is faster here. The eager run took about 250 ms and the lazy run took about 65 ms on this machine. The eager version writes a list of 2 million elements and then a list of 1 million elements. These are about 48 MB of list cells at 16 bytes each, and the garbage collector must then free them. The lazy version does not do this work. This is a result for this pipeline. For a small collection, the composition of functions can cost more than it saves.
Sources: we measured with Process.info(pid, :total_heap_size) and :timer.tc/1 on Erlang/OTP 29. The difference between eager and lazy is from the chapter.
Check yourself
Answer in your head. Then open the answer.
What is the difference between
EnumandStream?Enumis eager.Streamsupports lazy operations: streams are lazy enumerables that you can compose.