jonnifyelixir
F2 · Functional / F2.08 / F2.09 · lab

F2 · Functional Programming · the lab

The data-pipeline lab

The capstone of the chapter. Take a small dataset and build a pipeline over it — select rows, transform them, order them, then reduce them to an answer. Each stage shows the rows it produces, and the configuration writes the idiomatic Elixir it stands for.

Every idea from F2 meets here. Each stage is a pure function of the rows before it (F2.01); each intermediate is a new immutable value, never an edit of the last (F2.02); every stage takes a function as its argument (F2.03); map, filter, and reduce are the folds (F2.05); and the pipe composes them into one expression (F2.08). Configure the four stages below and read the result, the rows, and the code together.

One record through the pipeline

SOURCE "Mouse,25,gear,5" PARSE String.split |> to struct FILTER keep when rating >= 4 MAP price × 0.9 (discount) COLLECT [ ] — waiting rec

"Mouse,25,gear,5"
At the source — run it through parse → filter → map → collect.

The lab

The dataset is eight products, each with a price, a category, and a rating. Set each stage and watch the rows flow through. A stage left at its neutral setting passes the rows on unchanged and drops out of the generated pipeline.

pipeline
a sequence of stages a dataset flows through.
stage
one pure step — filter, map, sort, or reduce.
immutable intermediate
the rows between two stages; a new value, always inspectable.
aggregation
the reduce that collapses many rows into one answer.

products |> filter |> map |> sort |> reduce

the dataset · 8 products

Keyboard · $80 · ★4 Mouse · $25 · ★5 Monitor · $300 · ★4 Desk · $220 · ★3 Lamp · $40 · ★5 Chair · $180 · ★4 Notebook · $6 · ★5 Pen · $2 · ★3
filter Enum.filter/2
1
map Enum.map/2
sort Enum.sort_by/3
reduce Enum.reduce · sum · count · group_by

the pipeline this builds



      

rows through each stage

identity · 8 rows

A pipeline is composition over data: each stage a pure function of the rows before it, the whole chain a single expression you can read top to bottom.

The whole chapter, in one expression

The idea

Describe a result as a series of transformations of data, not as steps that mutate it.

Elixir

A pipe of Enum calls: filter, map, sort, reduce — each pure, each composable.

This is where F2 lands. Purity makes each stage safe to read in isolation; immutability makes every intermediate a value you can hold; higher-order functions let each stage take the behaviour it needs; folds give you map, filter, and reduce; and composition threads them into one pipeline. Change a single stage and only that stage’s output — and everything downstream — responds; nothing else moves. That is the whole argument of functional programming, on one screen.

That completes F2 · Functional Programming — nine modules from pure functions to pipelines. Next chapter: F3 — The Elixir Language. Revisit the F2 overview to see the full journey, or the composition module that leads here.