Skip to content

Instantly share code, notes, and snippets.

@ZhihaoLau
Last active June 16, 2017 13:04
Show Gist options
  • Save ZhihaoLau/0ab879cad626899c0cf0c01ae22b70cd to your computer and use it in GitHub Desktop.
Save ZhihaoLau/0ab879cad626899c0cf0c01ae22b70cd to your computer and use it in GitHub Desktop.
[FP] partial application
// Reference: https://github.com/getify/Functional-Light-JS/blob/master/ch3.md#some-now-some-later
function partial(fn, ...presetArgs) {
return function(...laterArgs) {
return fn(...presetArgs, ...laterArgs);
};
}
// // Or:
// const partial =
// (fn, ...presetArgs) =>
// (...laterArgs) =>
// fn(...presetArgs, ...laterArgs)
// Usage:
function add (a, b) {
return a + b;
}
const addThree = partial(add, 3);
const ret = [1, 2, 3].map(addThree); // [3, 4, 5]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment