Skip to content

Instantly share code, notes, and snippets.

@r3b
Last active January 4, 2021 16:11
Show Gist options
  • Save r3b/11072099 to your computer and use it in GitHub Desktop.
Save r3b/11072099 to your computer and use it in GitHub Desktop.
A simple, tiny partial application function
/*
takes as parameters a method and any number of static arguments.
returns a function that will call the original function with these
arguments, plus any new arguments given
example:
function pow(exponent, base){
return Math.pow(base, exponent);
}
var square=partial(pow, 2);
square(9); // 81
var cube=partial(pow,3);
cube(9); // 729
*/
function partial(method, arg1 /* arg2, arg3, etc */){
var args = Array.prototype.slice.call(arguments);
var fn=args.shift();
return fn.bind(this, args);
}
const partial = (method, ...args) => method.bind(this, args);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment