Created
September 27, 2013 07:13
-
-
Save kiddkai/6725125 to your computer and use it in GitHub Desktop.
curry
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
/** | |
* 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