Skip to content

Instantly share code, notes, and snippets.

@mbjordan
Last active September 26, 2015 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbjordan/03ad754135035c9e1362 to your computer and use it in GitHub Desktop.
Save mbjordan/03ad754135035c9e1362 to your computer and use it in GitHub Desktop.
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));
};
}
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