Lazy Evaluation with the Stream Module
Process large or infinite collections efficiently using Elixir streams, which compute values lazily on demand.
Lazy Evaluation with the Stream Module is a free Elixir & Phoenix: Scalable Backend Development lesson on CoddyKit. This is lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Elixir & Phoenix: Scalable Backend Development learning path, and your progress syncs across the web and the CoddyKit app. The Elixir & Phoenix: Scalable Backend Development course includes 4 lessons in total.
Eager vs Lazy
Enum is eager: each step builds a full intermediate list. Stream is lazy: it composes operations and computes only when needed.
The Cost of Eager Pipelines
Chaining several Enum calls walks the whole collection multiple times and allocates a list at each stage.
1..1_000_000
|> Enum.map(&(&1 * 2))
|> Enum.filter(&(rem(&1, 3) == 0))
|> Enum.take(5)
|> IO.inspect()Building a Stream
Swap Enum for Stream to defer work. Nothing runs until a consuming function is called.
stream =
1..1_000_000
|> Stream.map(&(&1 * 2))
|> Stream.filter(&(rem(&1, 3) == 0))
IO.inspect(stream)Forcing Evaluation
A stream stays inert until an Enum function (like take or to_list) pulls values through it.
result =
1..1_000_000
|> Stream.map(&(&1 * 2))
|> Enum.take(5)
IO.inspect(result)Why Streams Are Efficient
Lazy pipelines fuse the steps and stop as soon as enough elements are produced, avoiding huge intermediate lists.
Infinite Streams
Stream.iterate/2 creates endless sequences that are safe because evaluation is lazy.
Stream.iterate(0, &(&1 + 1))
|> Enum.take(5)
|> IO.inspect()Stream.cycle for Repetition
Stream.cycle repeats a finite list forever, useful for assigning rotating values.
Stream.cycle(["a", "b", "c"])
|> Enum.take(7)
|> IO.inspect()Generating with Stream.unfold
Stream.unfold/2 builds a stream from a state and a function returning the next value and new state.
Stream.unfold(1, fn n -> {n, n * 2} end)
|> Enum.take(5)
|> IO.inspect()Streaming Files Line by Line
File.stream!/1 reads a file lazily, one line at a time, so you can process huge files with constant memory.
# File.stream!("big.log")
# |> Stream.filter(&String.contains?(&1, "ERROR"))
# |> Enum.count()Stream.chunk_every
Group a stream into batches lazily, handy for paginating work without loading everything.
1..10
|> Stream.chunk_every(3)
|> Enum.to_list()
|> IO.inspect()When to Choose Stream
Use Stream for large datasets, multi-step pipelines, infinite sequences, or IO. Use Enum for small collections where simplicity wins.
Quick Check
Test your lazy evaluation knowledge.
Recap
You learned lazy evaluation:
Enumis eager;Streamis lazy and composable- Streams compute only when consumed by an
Enumfunction - They enable infinite sequences and constant-memory file processing
- Prefer streams for large/multi-step pipelines, Enum for small data
Frequently Asked Questions
Is the “Lazy Evaluation with the Stream Module” lesson free?
Yes — the full text of “Lazy Evaluation with the Stream Module” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Elixir & Phoenix: Scalable Backend Development course, upgrade to CoddyKit PRO. The Elixir & Phoenix: Scalable Backend Development course includes 4 lessons in total.
What will I learn in “Lazy Evaluation with the Stream Module”?
Process large or infinite collections efficiently using Elixir streams, which compute values lazily on demand. You practise Elixir & Phoenix: Scalable Backend Development with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Elixir & Phoenix: Scalable Backend Development?
No prior experience is required. Elixir & Phoenix: Scalable Backend Development on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.
How long does the “Lazy Evaluation with the Stream Module” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Elixir & Phoenix: Scalable Backend Development lesson?
Yes. Every Elixir & Phoenix: Scalable Backend Development lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Functions, Modules, and Pipelining
- Working with Enumerable Collections
- Recursion and Higher-Order Functions
- Lazy Evaluation with the Stream Module