Created
December 3, 2013 05:11
-
-
Save stevekane/7764181 to your computer and use it in GitHub Desktop.
Curry example for future reference
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
/** | |
first arg is a function, second is the number of arguments | |
to expect before executing | |
(N.B. The second arg is optional and will default to the arity | |
of the function if not provided) | |
*/ | |
function curry (fn, numArgs) { | |
var numArgs = numArgs || fn.length; | |
function makeCurried (previousArgs) { | |
return function () { | |
var args = previousArgs.concat(Array.prototype.slice.call(arguments)); | |
if (args.length < numArgs) return makeCurried(args); | |
return fn.apply(this, args); | |
} | |
} | |
return makeCurried([]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment