Skip to content

Instantly share code, notes, and snippets.

@rachelcarmena
Created August 5, 2019 16:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rachelcarmena/196b9f8b1c6b078b65fb1328f09c0269 to your computer and use it in GitHub Desktop.
Save rachelcarmena/196b9f8b1c6b078b65fb1328f09c0269 to your computer and use it in GitHub Desktop.
A possible curry function in JS
const curry = fn => {
const arity = fn.length;
const _curry = (...args) =>
args.length === arity
? fn(...args)
: arg => _curry(...args, arg);
return _curry();
}
// My first version before refactoring:
const curry = fn => {
const arity = fn.length;
const _curry = array => {
// I call the function when having all the arguments
if (array.length == arity)
return fn(...array);
// I create a function with only one argument
else
return arg => _curry(array.concat(arg));
}
return _curry([]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment