Guest User

Untitled

a guest
Feb 25th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. /**
  2. * please install lodash on file path
  3. * The following example should not be confused with curring or _.chain.
  4. * It is for understanding the es6 proposal-pipeline-operator only.
  5. */
  6.  
  7.  
  8.  
  9. const _ = require('lodash');
  10. const double = n => n*2;
  11. const increment = n => n+1;
  12.  
  13.  
  14. // without chaining
  15.  
  16. var result = double(increment(double(10)));
  17.  
  18. console.log(result, '// without chain')
  19.  
  20. let a = double(10); //20
  21. let b = increment(a); //21
  22. var result = double(b); //42
  23.  
  24. console.log(a, b, result, '// without chain, intermediate values')
  25.  
  26.  
  27. // with lodash chain
  28.  
  29. _.mixin({'double': double, 'increment': increment});
  30.  
  31.  
  32. const lodashResult = _(10).double().increment().double().value();
  33.  
  34. console.log(lodashResult, '//lodash chain');
  35.  
  36. // with lodash flow
  37.  
  38. const doubleIncrementDouble = _.flow([double, increment, double]);
  39.  
  40. var flowResult = doubleIncrementDouble(10) //42
  41.  
  42. console.log(flowResult, '//lodash flow')
  43.  
  44.  
  45. // es5 pipe
  46.  
  47. var pipe = function (functionList) {
  48. return function (data) {
  49. return functionList.reduce(function(value, func) {
  50. return func(value)
  51. }, data);
  52. }
  53. }
  54.  
  55.  
  56. const es5Result = pipe([double, increment, double])(10);
  57.  
  58. console.log(es5Result, '// es5 pipe');
  59.  
  60.  
  61. // es6 pipeline operator
  62.  
  63. // const es6Result = 10 |> double |> increment |> double;
Add Comment
Please, Sign In to add comment