Skip to content

Instantly share code, notes, and snippets.

@7cc
Created January 29, 2019 08:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 7cc/51c799972e375fb46c78797eda353b2b to your computer and use it in GitHub Desktop.
Save 7cc/51c799972e375fb46c78797eda353b2b to your computer and use it in GitHub Desktop.
// 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