Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * please install lodash on file path
- * The following example should not be confused with curring or _.chain.
- * It is for understanding the es6 proposal-pipeline-operator only.
- */
- const _ = require('lodash');
- const double = n => n*2;
- const increment = n => n+1;
- // without chaining
- var result = double(increment(double(10)));
- console.log(result, '// without chain')
- let a = double(10); //20
- let b = increment(a); //21
- var result = double(b); //42
- console.log(a, b, result, '// without chain, intermediate values')
- // with lodash chain
- _.mixin({'double': double, 'increment': increment});
- const lodashResult = _(10).double().increment().double().value();
- console.log(lodashResult, '//lodash chain');
- // with lodash flow
- const doubleIncrementDouble = _.flow([double, increment, double]);
- var flowResult = doubleIncrementDouble(10) //42
- console.log(flowResult, '//lodash flow')
- // es5 pipe
- var pipe = function (functionList) {
- return function (data) {
- return functionList.reduce(function(value, func) {
- return func(value)
- }, data);
- }
- }
- const es5Result = pipe([double, increment, double])(10);
- console.log(es5Result, '// es5 pipe');
- // es6 pipeline operator
- // const es6Result = 10 |> double |> increment |> double;
Add Comment
Please, Sign In to add comment