Skip to content

Instantly share code, notes, and snippets.

@nathan-lapinski
Created July 18, 2019 01:21
Show Gist options
  • Save nathan-lapinski/8bd48cf158fa112c339e051f4483c35b to your computer and use it in GitHub Desktop.
Save nathan-lapinski/8bd48cf158fa112c339e051f4483c35b to your computer and use it in GitHub Desktop.
curry
function curry(fn) {
const arity = fn.length;
return function $curry(...args) {
if (args.length < arity) {
return $curry.bind(null, ...args);
}
return fn.call(null, ...args);
};
}
// some examples
function add(a,b,c) {
return a + b + c;
}
function one(a) {
return a + 1;
}
const cAdd = curry(add);
const cOne = curry(one);
console.log(cOne(1)) // 2
console.log(cAdd(3)(2)(1)) // 6
console.log(cAdd(5,6)(6)) // 17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment