jonnifyelixir
F3 · The Elixir Language / F3.03 · functions, modules & the pipe

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

ONE COMPUTATION scores → average → grade NESTED CALL · reads inside-out Portal.grade(   Portal.average([80, 92, 74]) ) innermost call runs first RESULT :b

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

Portal.average Portal.grade [80, 92, 74] |> 82.0 |> :b each step is a function returning a value

      

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.

F3.03.1
Defining functions
def and defp, multiple clauses with guards, arity, anonymous functions, and the capture operator.
F3.03.2
Organising with modules
defmodule, attributes, alias and import, and documentation — the Portal namespace.
F3.03.3
The pipe operator
|> 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.