Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. # Compose functions in JavaScript
  2.  
  3. Order - right to left
  4.  
  5. ```
  6.  
  7. //compose for multiple functions
  8. const composeMultiple = (f, g) => (...data) => f(g(...data)) //composing 2 functions
  9. const composeFunctions = (...fns) => fns.reduce(composeMultiple)
  10.  
  11. //or one line
  12. compose = (...fns) => value => fns.reduceRight((v, f) => f(v), value);
  13.  
  14. ```
  15.  
  16. # Pipe functions in Javascript
  17.  
  18. Order - left to right
  19.  
  20. ```
  21.  
  22. //pipe multiple functions
  23. const pipeMultiple = (f, g) => (...data) => g(f(...data)) //composing 2 functions
  24. const pipeFunctions = (...fns) => fns.reduce(pipeMultiple)
  25.  
  26. //or one line
  27. pipe = (...fns) => value => fns.reduce((v, f) => f(v), value)
  28.  
  29. //
  30. pipeDebug = (...functions) => (value) => {
  31. debugger;
  32. return functions
  33. .reduce((currentValue, currentFunction) => {
  34. debugger;
  35. return currentFunction(currentValue);
  36. }, value)
  37. }
  38.  
  39. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement