Advertisement
vit134

compose

Nov 4th, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     compose(fn1, fn2, fn3, ...)(1)
  3. */
  4.  
  5. const fn1 = function(val) { console.log(val, '+ 1 = ', val + 1); return val + 1 };
  6. const fn2 = function(val) { console.log(val, '+ 2 = ', val + 2); return val + 2 };
  7. const fn3 = function(val) { console.log(val, '+ 3 = ', val + 3); return val + 3 };
  8. const fn4 = function(val) { console.log(val, '+ 4 = ', val + 4); return val + 4 };
  9.  
  10. // ES6
  11. const compose1 = (...fns) => val => fns.reduce((arg, fn) => fn(arg), val);
  12.  
  13. // ES5
  14. var compose = function() {
  15.     var fns = arguments;
  16.     return function(val) {
  17.         var res = val;
  18.        
  19.         for (var i = 0; i < fns.length; i++) {
  20.             res = fns[i](res)
  21.         }
  22.        
  23.         return res;
  24.     }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement