Skip to content

Instantly share code, notes, and snippets.

@behnood-eghbali
Forked from ericelliott/partial-apply.js
Created October 27, 2020 07:08
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 behnood-eghbali/1833c16558f92ef3f8567577bdba30c3 to your computer and use it in GitHub Desktop.
Save behnood-eghbali/1833c16558f92ef3f8567577bdba30c3 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