Functions and pattern matching

A function in Elixir is a set of clauses, and a clause is a pattern: the arguments decide which body runs. This part follows that idea from a first def to pattern matching, case and cond, the guards a clause may use, with for a run of steps, recursion and the pipe that makes a chain read top to bottom.

The chapter is the heart of the language. A reader who understands why a clause can match and fail, and why a recursive function is a loop, will find the rest of the course familiar.

3.1 Functions def, arity, anonymous functions, the capture shorthand, and a function passed as a value Open 3.2 Defaults and clauses default arguments, several clauses for one name, and the order they are tried in Open 3.3 Pattern matching a match on a structure, destructuring, and the MatchError of a shape that does not fit Open 3.4 case a value against several patterns, a guard on one clause, and the crash of no match Open 3.5 cond a chain of conditions, the catch-all clause, and when case reads better Open 3.6 with chains a run of matches that stops at the first failure, else, and the ok tuple it reads Open 3.7 Recursion replaces loops a recursive function, the accumulator, and the tail call the compiler keeps flat Open 3.8 Guards the functions a guard may call, a guard that replaces a body check, and the error of a bad one Open 3.9 The pipe operator in Elixir the value on the left as the first argument, a chain read top to bottom, and the trap of a wrong arity Open

Lessons