Skip to content

Instantly share code, notes, and snippets.

@ericelliott
Last active October 27, 2020 07:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save ericelliott/f0a8fd662111ea2f569e to your computer and use it in GitHub Desktop.
Save ericelliott/f0a8fd662111ea2f569e to your computer and use it in GitHub Desktop.
Generic Partial Application Function https://jsbin.com/biyupu/edit?html,js,output
// Generic Partial Application Function
// https://jsbin.com/biyupu/edit?html,js,output
// https://gist.github.com/ericelliott/f0a8fd662111ea2f569e
// partialApply(targetFunction: Function, ...fixedArgs: Any[]) =>
// functionWithFewerParams(...remainingArgs: Any[])
const partialApply = (fn, ...fixedArgs) => {
return function (...remainingArgs) {
return fn.apply(this, fixedArgs.concat(remainingArgs));
};
};
test('add10', assert => {
const msg = 'partialApply() should partially apply functions'
const add = (a, b) => a + b;
const add10 = partialApply(add, 10);
const actual = add10(5);
const expected = 15;
assert.equal(actual, expected, msg);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment