Merge pull request #166 from justin0022/master

Added compose such that the rightmost function may have any arity.
This commit is contained in:
Angelos Chalaris
2017-12-16 13:51:30 +02:00
committed by GitHub
3 changed files with 10 additions and 11 deletions

View File

@ -6,7 +6,7 @@ If lengths of the argument-arrays vary, `undefined` is used where no value could
```js
const zip = (...arrays) => {
const maxLength = Math.max.apply(null, arrays.map(a => a.length));
const maxLength = Math.max(...arrays.map(x => x.length));
return Array.from({length: maxLength}).map((_, i) => {
return Array.from({length: arrays.length}, (_, k) => arrays[k][i]);
})

View File

@ -1,15 +1,14 @@
### Compose functions
Use the `...rest` operator to gather all function arguments into an array. Return a function which takes
a single argument and uses `Array.reduceRight()` to return the result of applying each function.
Use `Array.reduce()` to perform right-to-left function composition.
The last (rightmost) function can accept one or more arguments; the remaining functions must be unary.
```js
const compose = (...fns) => n => fns.reduceRight((acc, fn) => fn(acc), n);
const compose = (...fns) => fns.reduce((f, g) => (...args) => f(g(...args)));
/*
const addOne = n => n + 1;
const square = n => n * n;
const double = n => n * 2;
compose(addOne, square, double)(2) -> 17
equivalent to: addOne(square(double(2)))
const add5 = x => x + 5
const multiply = (x, y) => x * y
const multiplyAndAdd5 = compose(add5, multiply)
multiplyAndAdd5(5, 2) -> 15
*/
```
```

View File

@ -1,6 +1,6 @@
### Pipe
Use `Array.reduce()` to perform left-to-right function composition.
Use `Array.reduce()` with the spread operator (`...`) to perform left-to-right function composition.
The first (leftmost) function can accept one or more arguments; the remaining functions must be unary.
```js