← Back to lesson 20: Build a data pipeline with functions

Build a data pipeline with functions: full examples and further reading

The original lesson material is preserved here for reference. The main notebook focuses on this lesson's core question.

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

Turn a list of scores into the sum of doubled passing scores.

Run this first

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

Original full example
%% 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).

Steps for the full example

  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.

Original lesson notes

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.

Symbols and terms
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.
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.

Original full exercise

Return the count of even numbers.

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

Return 2.

Show hint

Use rem for the remainder operator.

Show original answer
Reference answer
length(lists:filter(fun(N) -> N rem 2 =:= 0 end, [1,2,3,4])).
Does this pipeline create Erlang processes?

No. These list functions run ordinary sequential transformations.

Original lesson takeaways
Design trade-offs and language comparisons
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.
Original course references