-
-
Save mzbac/31dbbe5d94b703b76a2596968a79c86f to your computer and use it in GitHub Desktop.
curry
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 | |
* | |
* var curried = curry(function(a, b, c) { | |
* console.log(a+b+c); | |
* }); | |
* | |
* | |
* curried(2)(2)(2); // => 6 | |
* curried(2,2)(2); // => 6 | |
* curried(2,2,2); // => 6 | |
* | |
*/ | |
function curry(fn) { | |
var totalArgsLength = fn.length; | |
var params = []; | |
function genCurried() { | |
var args = [].slice.call(arguments); | |
params = params.concat(args); | |
if (params.length < totalArgsLength) { | |
return genCurried; | |
} | |
else { | |
return fn.apply(this, params); | |
} | |
} | |
return genCurried; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment