Last active
December 22, 2015 13:28
-
-
Save SinisterMinister/6478895 to your computer and use it in GitHub Desktop.
Flexible for currying a function.
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
/** | |
* Helper for currying a function. | |
* | |
* ### Usage | |
* | |
* ```javascript | |
* // Some function you want to partially apply | |
* function someFunc(a, b, c, d) {return arguments;} | |
* | |
* // Curry the function | |
* var curriedFunc = curry(someFunc, null, 'a', 'b'); | |
* | |
* // Returns `['a', 'b', 'c', 'd']` | |
* curriedFunc('c', 'd'); | |
* ``` | |
* | |
* @param {Function} fn Function to be curried | |
* @param {mixed} context Context the function will be called in | |
* @param {...number} var_args Arguments to partially apply to the function to be called | |
* @return {function} Function with partially applied arguments | |
*/ | |
api.curry = function (fn, context) { | |
// Container for the arguments to call the function with | |
var baseArgs = []; | |
// Get the arguments to be partially applied | |
for (var i = 2, l = arguments.length; i < l; i++) { | |
baseArgs.push(arguments[i]); | |
} | |
// Return a wrapper function | |
return function () { | |
var args = baseArgs.slice(0); | |
// Get the args to call the function with and add them to the args array | |
for (var i = 0, l = arguments.length; i < l; i++) { | |
args.push(arguments[i]); | |
} | |
// Call the function with the provided context and arguments | |
return fn.apply(context, args); | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment