Skip to content

Instantly share code, notes, and snippets.

@jeffschwartz
Created April 30, 2018 15:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffschwartz/fd0824f38de45000663b40e4d00e132a to your computer and use it in GitHub Desktop.
Save jeffschwartz/fd0824f38de45000663b40e4d00e132a to your computer and use it in GitHub Desktop.
const curry = (fn) => {
var a = [];
var $curry = (...args) => {
var tooManyArgsException = n =>
`too many args exception: curry expected ${fn.length} args; found ${n} args`;
a = [...a, ...args];
// reset if already has fulfilled arity requirement
a = a.length > fn.length ? args : a;
if (a.length > fn.length) {
throw tooManyArgsException(a.length);
}
if (a.length < fn.length) {
return $curry;
}
return fn(...a);
}
return $curry;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment