-
-
Save asvny/47fc7f11ceabe3f380c9 to your computer and use it in GitHub Desktop.
Auto-currying for the masses
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.prototype._curry = function (f=this) { | |
return (...args) => args.length < f.length | |
? f._curry(args.reduce((g, arg) => g.bind(null, arg), f)) | |
: f.apply(null, args); | |
}; |
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
var adder = function(a,b,c) { | |
return a + b + c; | |
}; | |
var add = adder._curry(); | |
var add3 = add(1)(2) | |
console.log(add(1)(2)(3)) //=> 6 | |
console.log(add(4,5)(6)) //=> 15 | |
console.log(add3(5)) //=> 8 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment