Last active
June 16, 2017 13:04
-
-
Save ZhihaoLau/0ab879cad626899c0cf0c01ae22b70cd to your computer and use it in GitHub Desktop.
[FP] partial application
This file contains hidden or 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
// 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