Created
November 6, 2013 02:46
-
-
Save ryanseddon/7330082 to your computer and use it in GitHub Desktop.
ES5 vs ES6 function currying
This file contains hidden or 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
function curried(fn) { | |
var args = Array.prototype.slice.call(arguments, 1); | |
return function() { | |
return fn.apply(this, args.concat(Array.prototype.slice.call(arguments, 0))); | |
} | |
} | |
This file contains hidden or 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
function curried(fn, ...args) { | |
return (...nArgs) => fn.apply(this, [...args, ...nArgs]) | |
} |
I believe you need the spread operator on the curry argument, both on the definition and the initial call.
function curried(fn, ...args) {
var curry = (...fnArgs) => {
if(fnArgs.length >= fn.length) {
return fn.apply(this, fnArgs);
}
return (...cArgs) => curry([...fnArgs, ...cArgs]);
}
return curry(...args);
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Turns out this is partial application and actual function currying in ES6 would look like this:
ES5: