Erlang basics · Lesson 20 (open contents)
Build a data pipeline with functions
Filter, transform, and fold a list with small named steps.
Run this first
Run it once, change one input, and compare the new result.
%% 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).- The result is
264:(60 * 2) + (72 * 2).
Read from the first line down
- Read
lists:filter/2: Keep items for which a predicate returns true. - Read
lists:map/2: Transform every item. - Read
lists:foldl/3: Accumulate a result from left to right.
Symbols are not secret signs
lists:filter/2Keep items for which a predicate returns true.
lists:map/2Transform every item.
lists:foldl/3Accumulate a result from left to right.
Match each name to its meaning
higher-order function
A higher-order function receives another function, returns one, or both.
pipeline
A pipeline passes data through a sequence of small transformations.
fold
A fold visits each item while carrying one accumulated result.
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.
Why this shape?
Why pass data through a series of small steps?
A pipeline turns nested calls into a top-to-bottom data journey. Each step should still be understandable and testable on its own.
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.
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
Enumis usually eager.Streamis 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/1is a function value;String.trim/1only 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
&1as JavaScript's bitwise ampersand.
Close the answer and try
Return the count of even numbers.
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
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.
Remember these three lines
- 1
Pass functions as values.
- 2
Name intermediate data while learning.
- 3
Use a fold when one result grows across the list.