Currying
This is a technique which involves converting a function which takes multiple arguments into a series of smaller cascaded functions which each take a single argument and return a function, except for the last cascaded function which returns the final result.
So essentially this is the transformation of a function that can be called as f(a, b, c)
into a function that can be called as f(a)(b)(c)
.
Example with javascript
Uncurried adder function taking multiple arguments:
const adder = (a, b) => a + b;
adder(99, 1) === 100;
Several examples of curried versions:
const es6CurriedAdder = (a) => (b) => adder(a, b);
es6CurriedAdder(99)(1) === 100;
function oldSchoolCurriedAdder(a) {
return adder.bind(null, a);
}
oldSchoolCurriedAdder(99)(1) === 100;
const hybridCurriedAdder = (a) => adder.bind(null, a);
hybridCurriedAdder(99)(1) === 100;
Relationship to partial application
Partial application can be considered the binding of one or more arguments of a curried function
const partiallyApplied = es6CurriedAdder(99);
partiallyApplied(1) === 100;
References
https://javascript.info/currying-partials
https://towardsdatascience.com/javascript-currying-vs-partial-application-4db5b2442be8