Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Compose functions in JavaScript
- Order - right to left
- ```
- //compose for multiple functions
- const composeMultiple = (f, g) => (...data) => f(g(...data)) //composing 2 functions
- const composeFunctions = (...fns) => fns.reduce(composeMultiple)
- //or one line
- compose = (...fns) => value => fns.reduceRight((v, f) => f(v), value);
- ```
- # Pipe functions in Javascript
- Order - left to right
- ```
- //pipe multiple functions
- const pipeMultiple = (f, g) => (...data) => g(f(...data)) //composing 2 functions
- const pipeFunctions = (...fns) => fns.reduce(pipeMultiple)
- //or one line
- pipe = (...fns) => value => fns.reduce((v, f) => f(v), value)
- //
- pipeDebug = (...functions) => (value) => {
- debugger;
- return functions
- .reduce((currentValue, currentFunction) => {
- debugger;
- return currentFunction(currentValue);
- }, value)
- }
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement