Created
June 21, 2012 11:14
-
-
Save spion/2965175 to your computer and use it in GitHub Desktop.
JavaScript currying function
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
function curry(fn, args, len) { | |
var fnStr = fn.toString(), | |
argStart = fnStr.indexOf('(') + 1; argnames = fnStr.substr(argStart, fnStr.indexOf(')') - argStart).split(/[ ,]+/); | |
len = len?len:argnames.length | |
return function() { | |
var a = [], i = 0; | |
for (var k = 0; k < len; ++k) | |
if (args.hasOwnProperty(k)) | |
a.push(args[k]) | |
else if (args.hasOwnProperty(argnames[k])) | |
a.push(args[argnames[k]]) | |
else a.push(arguments[i++]) | |
return fn.apply(this, a); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment