F1 · Algebra
Composition, f∘g
Feed one function’s output into the next and you have built a third function. Order changes the result; grouping does not; and Elixir’s pipe is this idea written left to right.
F1.01 made functions into values and F1.02 let you evaluate them by substitution. Composition is how those values are joined into larger ones. We define the composite, watch why order matters and grouping is free, and arrive at the pipe — the operator the landing page has been previewing.
Chaining functions
Given f and g, their composite g∘f is the function that applies f first and then g: (g∘f)(x) = g(f(x)). For this to make sense the outputs of f must be valid inputs to g — the range of f sits inside the domain of g.
The order is not a formality. g∘f and f∘g are usually different functions. Choose two below and compare both directions.
- composite
- (g∘f)(x) = g(f(x)) — apply f, then g.
- order
- g∘f is usually not f∘g.
- associative
- (h∘g)∘f = h∘(g∘f) — grouping is free.
- identity
- id(x) = x leaves it unchanged: f∘id = f.
Compose two · order matters
Algebra
(g∘f)(x) = g(f(x)) — the inner function runs first.
Elixir
x |> f.() |> g.() runs f then g — the same chain, read left to right.
A composite is a function in its own right, and swapping the order generally swaps the answer. Hold the order fixed and a useful freedom appears.
Associativity
Composition is associative. With three functions in a row, it makes no difference whether you compose the first pair before the third, or the last pair before the first: (h∘g)∘f = h∘(g∘f). The two groupings name the same function and produce the same value.
That is why a chain of functions needs no parentheses at all. Move the grouping below and watch the running values stay put.
Compose three · grouping is free
Algebra
Associativity means a chain has one meaning, however you bracket it.
Elixir
You may split a long pipe into sub-pipelines and recombine them — the result is unchanged.
Because grouping is free, a chain of functions is unambiguous without a single parenthesis. That is exactly what a pipeline needs.
The pipe
Nested calls read inside-out: in g(f(x)) the first thing to happen is written
in the middle. Elixir’s pipe |> turns the chain around so it reads in the
order the work is done. x |> f() |> g() passes x into
f, then that result into g — the same composite,
spelled forwards.
Three spellings, one function. Toggle the stages and confirm they always agree, and add the identity stage to see the neutral element of composition.
Three notations · one value
Algebra
(g∘f)(x), and id as the neutral element: f∘id = f.
Elixir
x |> f() |> g() is composition in evaluation order; & &1 is identity.
The pipe is not new machinery — it is composition, read in the order things happen. Every pipeline in the course is an instance of g∘f.
What this lands
Composition is the join that turns small functions into programs: apply one, then the next, with order
deciding the result and associativity freeing the grouping. Substitution from F1.02 evaluates the
composite; the single-output rule from F1.01 keeps every stage well defined. From here, F1.07 shows that
reduce composes a function across a whole collection, F2 makes pipelines a way of
working, and the pipe runs through every later chapter — starting with the one on the landing page.
Next: F1.04 — Immutability & binding (planned) — a name fixed to a value, and why that is what lets substitution and composition stay honest. The rest of F1 is on the chapter page.