Understanding Currying in JavaScript
Chidume Nnamdi
4.6K21

Nice article!

Really liked mathematic explanation.

I think that curry function example

function curry(fn, ...args) {
return (..._arg) => {
return fn(...args, ..._arg);
}
}

not correct, as you explained in article it is partial function, curry function implementation a little bit more complicated, something like:

function curry(fn) {
const arity = fn.length;
return (...args) => {
if (args.length === arity) return fn (...args);
return _curry(fn, arity, args);
};
}
function _curry(fn, expectedArityLength, passedArities) {

return (...args) => {
const _args = [...passedArities, ...args];
  if (expectedArityLength === _args.length) return fn(..._args);
  return _curry(fn, expectedArityLength, _args);
};
}