Skip to content

Instantly share code, notes, and snippets.

@jtjames
Last active August 29, 2015 14:03
Show Gist options
  • Save jtjames/bb3e8a53da968d5b66bb to your computer and use it in GitHub Desktop.
Save jtjames/bb3e8a53da968d5b66bb to your computer and use it in GitHub Desktop.
Partial Application in Javascript
function partial(fn) {
var __slice = Array.prototype.slice,
args = __slice.call(arguments, 1);
return function () {
return fn.apply(this, args.concat(__slice.call(arguments)));
};
}
function multiply(a, b) {
return a * b;
}
var times2 = partial(multiply, 2),
times3 = partial(multiply, 3);
console.assert(times2(2) === 4, "2 * 2 = 4");
console.assert(times2(3) === 6, "2 * 3 = 6");
console.assert(times3(2) === 6, "3 * 2 = 6");
console.assert(times3(3) === 9, "3 * 3 = 9");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment