Last active
January 4, 2021 16:11
-
-
Save r3b/11072099 to your computer and use it in GitHub Desktop.
A simple, tiny partial application function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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