Created
August 10, 2018 06:46
Star
You must be signed in to star a gist
Curry the hell out of any function (JavaScript)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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