F3 · Data & shape · module 4
Enumerables & streams
A great deal of Elixir is walking collections: a list of lessons, a map of scores, a learner's
history. Anything that can be walked one element at a time implements the Enumerable
protocol, and the Enum module is the toolkit for all of them.
Stream is its lazy twin — the same operations, deferred until something asks for
a result.
In F3.03 you piped a learner's progress through a few Enum calls. This
module takes that further: the functions that summarise a collection, the for
comprehension, and lazy streams that run over a full history without building every intermediate list.
When the work happens
Enum · eager — 6 + 6 + 6 elements touched
Every step builds a full intermediate list, even though two results are wanted.
One protocol, many collections
Lists, ranges, maps, MapSets, and streams are different data structures, but
Enum treats them identically because each implements the same protocol. The same
call works on any of them. Choose a source and watch the result hold steady.
Enum.map over a source · select one
The protocol is the abstraction: write against Enum once, and it runs on
whatever collection you hand it. Maps are enumerable too — they yield {key, value}
pairs, so the function takes a pair.
Three deep dives
Three pages, building from eager traversal to its lazy counterpart, all over the portal's data.
map, filter, reduce, group_by, frequencies.for over generators, with filters and :into — set-builder notation rendered as Elixir syntax.Stream builds a recipe that runs only when pulled — eager versus lazy, early exit, and infinite sequences.F2 · higher-order functions & folds
Map, filter, and reduce as the shape of working with collections.
F3 · Enum & Stream
Those operations as a real, protocol-backed library — eager in Enum, lazy in Stream.
Start with Enum, then comprehensions, then lazy streams. Next module: F3.05 — Structs.
References
Primary sources for this lesson, and where it connects in the course.
Sources
- Elixir — Enumerables and streams (guide) — eager versus lazy traversal.
- Elixir —
Stream— lazy enumerables. - Elixir —
Enumerableprotocol — what makes a thing enumerable.