Skip to content

Instantly share code, notes, and snippets.

@mtavkhelidze
Created August 10, 2018 06:46
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save mtavkhelidze/e2adc60d157c3f5083ffb8aa61900425 to your computer and use it in GitHub Desktop.
Curry the hell out of any function (JavaScript)
/**
* Curry the hell out of any function.
*
* @param f function
* @returns function
*/
function curry(f) {
const arity = f.length;
// preserve original `this` in case we're curring a class method;
const self = this;
return function _() {
const args1 = [...arguments];
console.log(0, args1)
if (args1.length >= arity) {
return f.apply(self, args1);
}
return function() {
const args2 = [...arguments];
console.log(1, args2);
return _.apply(self, [...args1, ...args2]);
};
};
}
const add = curry((a, b, c) => a + b + c);
const addOne = add(1);
const addThree = addOne(2);
console.log(addThree(10));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment