Create a gist now

Instantly share code, notes, and snippets.

@mbjordan /partial.js
Last active Sep 26, 2015

What would you like to do?
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