Elixir basics · Lesson 20 (open contents)
01 · Let the code speak02 · Meet six kinds of values03 · Put values together04 · Build a new list05 · Make the shapes match06 · Choose with case07 · Parse a number safely08 · Unpack &1 and the pipe09 · Put code in a project10 · Ask precise true-or-false questions11 · Read and update nested data12 · Count visible text and bytes13 · Let function clauses choose14 · Solve a list twice15 · Choose the smallest clear control form16 · Give domain data a shape17 · Put one tested module in Mix18 · Make success and failure predictable19 · Read and write one real file20 · Take only the data you need21 · Give different data one shared action22 · Write a callback contract23 · Write down the data contract24 · Test behavior and boundaries25 · Turn a project into a command26 · Write the project promise27 · Parse one real line28 · Keep the public API small29 · Make the promise executable30 · Package the edge and leave clues31 · Prove the project is done
ELIXIR · INTERMEDIATE · LESSON 2025 minutes

Take only the data you need

Compare eager Enum work with a lazy Stream pipeline.

01 · Start with the whole example

Run this first

Run it once, change one input, and compare the new result.

Elixir
# Describe a large pipeline, then ask for three results
result =
  1..1_000_000
  |> Stream.map(&(&1 * 2))
  |> Stream.filter(&(rem(&1, 3) == 0))
  |> Enum.take(3)

result
Check the result first
  • The pipeline returns [6, 12, 18] without building a million-item result list.
02 · Take the code apart

Read from the first line down

  1. Read Stream.map/2: Add a lazy transformation.
  2. Read Stream.filter/2: Add a lazy filter.
  3. Read Enum.take/2: Consume only the requested number of values.
03 · Meet the new symbols

Symbols are not secret signs

Stream.map/2

Add a lazy transformation.

Stream.filter/2

Add a lazy filter.

Enum.take/2

Consume only the requested number of values.

04 · Ideas inside the code

Match each name to its meaning

01

eager

Enum functions compute their full result immediately.

02

lazy

Stream functions describe work and wait until a consumer asks for values.

03

terminal operation

A function such as Enum.take/2 asks a stream to produce results.

05 · Make the idea clear

Why these forms are useful

Run the example first. Predict one result, then change one input and run it again.

Enum functions compute their full result immediately.

Stream functions describe work and wait until a consumer asks for values.

06 · Change it yourself

Close the answer and try

Square a large range lazily and take the first two squares above 20.

Practice starting point
1..1_000
|> Stream.map(____)
|> Stream.filter(____)
|> Enum.take(2)

Target result: Return [25, 36].

Stuck? Read one hint

Square with &(&1 * &1) and keep values greater than 20.

After you run it, see one answer
One answer
1..1_000
|> Stream.map(&(&1 * &1))
|> Stream.filter(&(&1 > 20))
|> Enum.take(2)
Think about it: What makes a Stream start producing values?

A consumer such as Enum.take/2 or Enum.to_list/1.

Take with you

Remember these three lines

  1. 1

    Enum is eager.

  2. 2

    Stream delays work.

  3. 3

    Choose a bounded consumer for large or endless inputs.

Lesson completeRun the code once, then mark this lesson as done.