Last active
September 26, 2015 21:01
-
-
Save mbjordan/03ad754135035c9e1362 to your computer and use it in GitHub Desktop.
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
function partial(fn) { // `fn` is the original function | |
// `args_a` are the arguments (barring `fn`) of the first call. | |
var args_a = Array.prototype.slice.call(arguments, 1); | |
// Now, we return a new function, with the first set of arguments already applied. | |
return function partialApplicator() { | |
// `args_b` are the arguments applied at the second call | |
var args_b = Array.prototype.slice.call(arguments); | |
// Now, concatenate both Arrays and apply them to the original function | |
return fn.apply(undefined, args_a.concat(args_b)); | |
}; | |
} |
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
function simpleAddition(a, b) { | |
return a + b; | |
} | |
var add20to = partial(simpleAddition, 20); | |
add20to(100); // -> 120 | |
add20to(230); // -> 250 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment