jonnifyelixir
F2 / F2.06 / currying

F2.06 · part 3 of 3

Partial application & currying

Partial application fixes some of a function’s arguments and returns a function awaiting the rest. Currying takes it to the limit: a function of many arguments becomes a chain of one-argument functions, each capturing its argument and returning the next.

Elixir does not curry automatically — a two-argument function wants both at once. But closures let you build either pattern by hand. Fix one argument and you have specialised the function; nest a function per argument and you have curried it. Both are capture, applied to arguments instead of free variables.

Specialise by fixing

From a general multiply, fixing the first argument produces a named specialist: fix it at 2 and you have double; at 3, triple. The fixed factor is captured; the new function takes only the value to scale.

Choose the fixed factor and the input.

partial application
fixing some arguments, returning a function for the rest.
currying
turning an n-argument function into n nested one-argument functions.
arity reduction
each fixed argument lowers the arity of what remains.
point-free
defining a function by combining others, naming no arguments.

fix the factor · from a general multiply

4
general fn a, b -> a * b end fix a fn b -> 2 * b end apply input 8 double.(4) = 2 * 4 = 8


      
double · factor fixed at 2 · input 4 → 8

Idea

One general function plus a fixed argument yields a family of named specialists.

Elixir

The capture form &multiply.(2, &1) writes the same partial in one line.

Fixing an argument specialises a function. double and triple are one multiply with the factor captured.

Currying by hand

Currying nests a function per argument. fn a -> fn b -> fn c -> a + b + c end end end takes one argument at a time: each application captures its argument and returns the next function, until the last supplies the result.

Apply the arguments one at a time.

curried.(1).(2).(3) · one argument at a time

0 of 3
captured so far call curried returns a function awaiting a


      
nothing applied yet · curried awaits its first argument, a

F2.06.1 · environment

Each step captures one argument; the captures stack up across the chain.

Elixir

Real Elixir rarely curries by hand — multi-argument functions and the pipe are clearer.

Currying is capture in a chain: one argument per step, each remembered, until the final call returns a value.

Where this goes

That completes F2.06. A closure carries the environment it was defined in, capturing values by value and keeping them stable under immutability. Function factories, partial application, and currying are all that one mechanism turned toward different ends, and the & operator writes the small functions compactly. F2.07 leaves functions for data: algebraic data types, where tagged tuples and structs model the shapes a value can take.

Next: F2.07 — Algebraic data types — sum and product types, and tagged tuples. Back to the F2.06 hub or the F2 overview.