Created
October 10, 2015 16:30
-
-
Save koozdra/4f1edda2009293a85cf2 to your computer and use it in GitHub Desktop.
Javascript Curry
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 function and accepts lists of parameters | |
function curry(fn) { | |
// begin with an empty array of collected arguments | |
var partialArgs = []; | |
var f = function() { | |
// append all passed in arguments to the arguments collection | |
Array.prototype.push.apply(partialArgs, arguments); | |
// if we have matched or exceeded the argument length of the containing function | |
// call the function with the required length (extra parameters are ignored) | |
if (partialArgs.length >= fn.length){ | |
// TODO: is 'this' appropriate here? | |
fn.apply(this, Array.prototype.slice.call(partialArgs, 0, fn.length)); | |
} | |
// return the function to collect more arguments | |
return f; | |
}; | |
// call and return the accumulator function | |
return f(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment