Update and rename compose.md to compose-functions.md

This commit is contained in:
Angelos Chalaris
2017-12-16 13:43:15 +02:00
committed by GitHub
parent fd3ef95ab3
commit 0b844e4b3a

View File

@ -0,0 +1,14 @@
### Compose functions
Use `Array.reduce()` with the spread operator (`...`) to perform right-to-;eft function composition.
The last (rightmost) function can accept one or more arguments; the remaining functions must be unary.
```js
const compose = (...fns) => fns.reduce((f, g) => (...args) => f(g(...args)));
/*
const add5 = x => x + 5
const multiply = (x, y) => x * y
const multiplyAndAdd5 = compose(add5, multiply)
multiplyAndAdd5(5, 2) -> 15
*/
```