Erlang basics · Lesson 20 (open contents)
01 · Open erl02 · Meet common terms03 · Tell two kinds of text apart04 · Bind a variable once05 · Split and filter a list06 · Parse a number safely07 · Choose one path with case08 · Let a function keep going09 · Put code in a module10 · Compare terms and Boolean results11 · Read and update nested maps12 · Build and inspect UTF-8 binaries13 · Let clauses choose the function path14 · Solve a list twice15 · Choose with patterns, guards, and errors16 · Describe a module and one record17 · Put one tested module in Rebar318 · Return success or failure as data19 · Read and write one file safely20 · Build a data pipeline with functions21 · Keep records behind a module boundary22 · Define a module contract23 · Describe the data contract24 · Test small rules and whole flows25 · Shape builds and make 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
ERLANG · INTERMEDIATE · LESSON 2025 minutes

Build a data pipeline with functions

Filter, transform, and fold a list with small named steps.

01 · Start with the whole example

Run this first

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

Erlang
%% Keep each transformation small and inspectable
Scores = [41, 60, 72],
Passing = lists:filter(fun(N) -> N >= 60 end, Scores),
Doubled = lists:map(fun(N) -> N * 2 end, Passing),
lists:foldl(fun(N, Sum) -> N + Sum end, 0, Doubled).
Check the result first
  • The result is 264: (60 * 2) + (72 * 2).
02 · Take the code apart

Read from the first line down

  1. Read lists:filter/2: Keep items for which a predicate returns true.
  2. Read lists:map/2: Transform every item.
  3. Read lists:foldl/3: Accumulate a result from left to right.
03 · Meet the new symbols

Symbols are not secret signs

lists:filter/2

Keep items for which a predicate returns true.

lists:map/2

Transform every item.

lists:foldl/3

Accumulate a result from left to right.

04 · Ideas inside the code

Match each name to its meaning

01

higher-order function

A higher-order function receives another function, returns one, or both.

02

pipeline

A pipeline passes data through a sequence of small transformations.

03

fold

A fold visits each item while carrying one accumulated result.

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.

A higher-order function receives another function, returns one, or both.

A pipeline passes data through a sequence of small transformations.

DESIGN DECISION

Why this shape?

Why pass data through a series of small steps?

Choice

A pipeline turns nested calls into a top-to-bottom data journey. Each step should still be understandable and testable on its own.

Cost and boundary

A pipe is not a superior loop. If argument order is awkward or a step hides substantial I/O, an ordinary call can be clearer.

A FAMILIAR POINT OF VIEW

Coming from Java, Python, or JavaScript

Java

Familiar starting point
Stream chains or fluent method calls.
What BEAM changes
Elixir |> inserts the left value into the next call; Erlang usually nests calls or names intermediate values.
False friend
Enum is usually eager. Stream is lazy.

Python

Familiar starting point
Generators, comprehensions, and named intermediate values.
What BEAM changes
Captures and pipes compose small functions without mutating a list.
False friend
&String.trim/1 is a function value; String.trim/1 only names a function and arity.

JavaScript

Familiar starting point
map/filter chains, callbacks, and iterators.
What BEAM changes
Values are immutable by default; each step returns another result.
False friend
Do not read &1 as JavaScript's bitwise ampersand.
06 · Change it yourself

Close the answer and try

Return the count of even numbers.

Practice starting point
length(lists:filter(fun(N) -> N ____ 2 =:= 0 end, [1,2,3,4])).

Target result: Return 2.

Stuck? Read one hint

Use rem for the remainder operator.

After you run it, see one answer
One answer
length(lists:filter(fun(N) -> N rem 2 =:= 0 end, [1,2,3,4])).
Think about it: Does this pipeline create Erlang processes?

No. These list functions run ordinary sequential transformations.

Take with you

Remember these three lines

  1. 1

    Pass functions as values.

  2. 2

    Name intermediate data while learning.

  3. 3

    Use a fold when one result grows across the list.

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