Functional programming fundamentals memento
Currying
A function of arity n can be expressed as a sequence of n unary function.
// this:
const curriedSum = (a: number) => (b: number): number => a + b
// is the curried version of this
const sum = (a: number, b: number): number => a + b
Partial application
The main point of currying functions is to achieve partial application.
// this
const inc = curriedSum(1)
// returns a new unary function (b: number) => number
const twelve: number = inc(11)
Composition
const set: number[] = [1, 2, 3]
// this:
set.map(n => inc(n))
// is the same as this:
set.map(inc)
Given dec is a unary function and multiply a curried function of arity 2, we may compose those functions together to achieve trivial operations.
// this resolves as ((n + 1) * 2) - 1
const calculate = (n: number): number => dec(multiply(2)(inc(n)))
// and can be rewritten using a left to right composition utility
const calculateR = (n: number): number => o(dec, multiply(2), inc)(n)
// which is the same as
const calculateR = o(dec, multiply(2), inc)
// which for all n, the following is a tautology
calculate(n) === calculateR(n)