jonnifyelixir
F3 · The Elixir Language / F3.04 · enum & streams

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

SOURCE · 6 ELEMENTS [1, 2, 3, 4, 5, 6] map(& &1 * &1)realises 6 → full list filter(& rem(&1, 2) == 0)realises 6 → full list take(2)keeps first 2 of the list RESULT [4, 16]

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

SOURCE [1, 2, 3] TRANSFORM Enum.map(& &1 * &1) RESULT [1, 4, 9]

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.

F3.04.1
Enum, the eager workhorse
The Enumerable protocol and the functions that walk any collection — map, filter, reduce, group_by, frequencies.
F3.04.2
Comprehensions
for over generators, with filters and :into — set-builder notation rendered as Elixir syntax.
F3.04.3
Lazy streams
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

Related in this course