#+title: currying Currying, in programming, is a [[file:20200604222704-functional_programming.org][functional programming]] technique whereby a [[file:20210521211721-function_programming.org][function]] of =n= arity is reduced to =n= unary functions. The term comes from [[file:20210407163530-λ_calculus.org][lambda calculus]]. #+begin_src js :eval never 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); #+end_src Currying could be implemented in [[file:20200720132835-javascript.org][JavaScript]] as such: #+begin_src js :results output 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); #+end_src #+RESULTS: : true : true : true : true In [[file:20200708123930-haskell.org][Haskell]], all functions are automatically curried: #+begin_src haskell add x y z = x + y + z add10 = add 10 add10 100 2 #+end_src