Created
January 29, 2019 08:37
-
-
Save 7cc/51c799972e375fb46c78797eda353b2b to your computer and use it in GitHub Desktop.
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
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Pipeline_operator | |
const double = (n) => n * 2; | |
const increment = (n) => n + 1; | |
// without pipeline operator | |
double(increment(double(double(5)))); // 42 | |
// with pipeline operator | |
5 |> double |> double |> increment |> double; // 42 | |
//====================================== | |
// lodash | |
_.flow([double, double, increment, double])(5); | |
//====================================== | |
// reduce | |
[5, double, double, increment, double].reduce((p, c)=> c(p)); | |
[double, double, increment, double].reduce((p, c)=> c(p), 5); | |
const pipe = (val, ...fns) => fns.reduce((p, c)=> c(p), val); | |
pipe(5, double, double, increment, double); | |
const fpPipe = (...fns) => (val)=> fns.reduce((p, c)=> c(p), val); | |
fpPipe(double, double, increment, double)(5) | |
//====================================== | |
// note it's async! | |
Promise.resolve(5) | |
.then(double) | |
.then(double) | |
.then(increment) | |
.then(double); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment