📕 subnode [[@ryan/20210607204228 currying]]
in 📚 node [[20210607204228-currying]]
Currying, in programming, is a functional programmingtechnique whereby a
functionof n
arity is reduced to n
unary functions. The term comes from
lambda calculus
const add = (x, y, z) => x + y + z; const curriedAdd = curry(add); curriedAdd(1)(2)(3) === curriedAdd(1, 2)(3) === curriedAdd(1)(2, 3) === curriedAdd(1, 2, 3);
Currying could be implemented in JavaScriptas such:
function curry(func) { const arity = func.length; return function wrapper(...args) { if (args.length >= arity) { return func(...args); } else { return function(...otherArgs) { return wrapper(...args.concat(otherArgs)); }; } }; } const add = (x, y, z) => x + y + z; const curriedAdd = curry(add); console.log(curriedAdd(1)(2)(3) === 6); console.log(curriedAdd(1, 2)(3) === 6); console.log(curriedAdd(1)(2, 3) === 6); console.log(curriedAdd(1, 2, 3) === 6);
true true true true
In Haskell all functions are automatically curried:
add x y z = x + y + z add10 = add 10 add10 100 2
📖 stoas
- public document at doc.anagora.org/20210607204228-currying
- video call at meet.jit.si/20210607204228-currying