F3 · Syntax & flow · module 3
Functions, modules & the pipe
A function is the unit of work in Elixir: it takes arguments and returns a value, with no objects
and no mutation in sight. A module is the namespace that groups related functions under one
name. And the pipe operator |> is the glue — it threads a value through a
sequence of functions so a nested call reads as a left-to-right pipeline.
The running example is a learning Portal: a learner’s quiz
scores, turned into an average and then a letter grade. Three angles open the module — what a function
is, how a module groups functions, and how |> composes them.
Two ways to write one call
Portal.grade(Portal.average(scores))
The nested call reads inside-out: the innermost runs first.
A value, transformed
Take a learner’s scores, average them, grade the average. Each step is a function; the functions live in a module; the pipe strings them together. Select an angle to see the same pipeline three ways.
The angle · select one
role: map an input to an output
expression: Portal.average([80, 92, 74]) #=> 82.0
No state changes hands; each function takes a value and returns a new one. The module names where those functions live, and the pipe is what lets you read the chain in the order it runs.
Three deep dives
In order: how functions are defined and matched, how modules organise them, and how the pipe composes them into a pipeline.
def and defp, multiple clauses with guards, arity, anonymous functions, and the capture operator.defmodule, attributes, alias and import, and documentation — the Portal namespace.|> threads a value as the first argument, turning a nested call into a readable pipeline.F3.02 · a pattern picks a value apart
The match operator binds variables by shape.
F3.03 · functions, modules & the pipe
Function clauses match their arguments the same way; modules group the functions; |> composes them.
Start with defining functions, then
organising with modules, then
the pipe operator. Next module:
F3.04 — Enumerables & streams, where you
pipe a learner’s progress through the Enum toolkit.
References
Primary sources for this lesson, and where it connects in the course.
Sources
- Modules and functions — Elixir documentation — defining and grouping functions.
Kernel— Elixir documentation —def,defp, and the pipe.