Guest User

Untitled

a guest
Oct 22nd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. const compose = (...fns ) =>
  2.  
  3. fns.reduce( ( f, g, i ) => (...args ) => {
  4.  
  5. console.log( '\ni:', i )
  6. console.log( 'args:', ...args )
  7. console.log( 'f:', f )
  8. console.log( 'g:', g )
  9.  
  10. return f( g(...args ) )
  11. } )
  12.  
  13. const add1 = ( x ) => x + 1
  14. const add4 = ( x ) => x + 1
  15. const add7 = ( x ) => x + 7
  16. const times2 = ( x ) => x * 2
  17. const times5 = ( x ) => x * 5
  18.  
  19. compose( add4, times2, add1, add7, times5 )( 2 )
  20.  
  21. // i: 4
  22. // args: 2
  23. // f: (...args) => {
  24.  
  25. // console.log('\ni:', i);
  26. // console.log('args:', ...args);
  27. // console.log('f:', f);
  28. // console.log('g:', g);
  29.  
  30. // return f(g(...args));
  31. // }
  32. // g: x => x * 5
  33.  
  34. // i: 3
  35. // args: 10
  36. // f: (...args) => {
  37.  
  38. // console.log('\ni:', i);
  39. // console.log('args:', ...args);
  40. // console.log('f:', f);
  41. // console.log('g:', g);
  42.  
  43. // return f(g(...args));
  44. // }
  45. // g: x => x + 7
  46.  
  47. // i: 2
  48. // args: 17
  49. // f: (...args) => {
  50.  
  51. // console.log('\ni:', i);
  52. // console.log('args:', ...args);
  53. // console.log('f:', f);
  54. // console.log('g:', g);
  55.  
  56. // return f(g(...args));
  57. // }
  58. // g: x => x + 1
  59.  
  60. // i: 1
  61. // args: 18
  62. // f: x => x + 1
  63. // g: x => x * 2
  64. // => 37
Add Comment
Please, Sign In to add comment