Last active
October 20, 2015 15:53
-
-
Save smonn/19ba11b53a08ecc45d25 to your computer and use it in GitHub Desktop.
curry method for javascript with support for context
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(fn[, context[, arity[, params...]]]) | |
var curry = function (fn, context, arity) { | |
var args = Array.prototype.slice.call(arguments, 0); | |
if (!context) { | |
context = context || undefined; | |
args[1] = context; | |
} | |
if (!arity) { | |
arity = arity || fn.length; | |
args[2] = arity; | |
} | |
if (arity === (args.length - 3)) { | |
return fn.apply(context, args.slice(3)); | |
} | |
return function () { | |
var args2 = Array.prototype.slice.call(arguments, 0); | |
return curry.apply(context, args.concat(args2)); | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment