Merge pull request #36 from conblem/pipe

Added Pipe snippet
This commit is contained in:
Angelos Chalaris
2017-12-12 18:15:59 +02:00
committed by GitHub
2 changed files with 18 additions and 0 deletions

View File

@ -35,6 +35,7 @@
* [Last of list](#last-of-list) * [Last of list](#last-of-list)
* [Measure time taken by function](#measure-time-taken-by-function) * [Measure time taken by function](#measure-time-taken-by-function)
* [Object from key value pairs](#object-from-key-value-pairs) * [Object from key value pairs](#object-from-key-value-pairs)
* [Pipe](#pipe)
* [Powerset](#powerset) * [Powerset](#powerset)
* [Random number in range](#random-number-in-range) * [Random number in range](#random-number-in-range)
* [Randomize order of array](#randomize-order-of-array) * [Randomize order of array](#randomize-order-of-array)
@ -337,6 +338,15 @@ const objectFromPairs = arr => arr.reduce((a,b) => (a[b[0]] = b[1], a), {});
// objectFromPairs([['a',1],['b',2]]) -> {a: 1, b: 2} // objectFromPairs([['a',1],['b',2]]) -> {a: 1, b: 2}
``` ```
### Pipe
Use `Array.reduce()` to pass value through functions.
```js
const pipe = (...funcs) => arg => funcs.reduce((acc, func) => func(acc), arg);
// pipe(btoa, x => x.toUpperCase())("Test") -> "VGVZDA=="
```
### Powerset ### Powerset
Use `reduce()` combined with `map()` to iterate over elements and combine into an array containing all combinations. Use `reduce()` combined with `map()` to iterate over elements and combine into an array containing all combinations.

8
snippets/pipe.md Normal file
View File

@ -0,0 +1,8 @@
### Pipe
Use `Array.reduce()` to pass value through functions.
```js
const pipe = (...funcs) => arg => funcs.reduce((acc, func) => func(acc), arg);
// pipe(btoa, x => x.toUpperCase())("Test") -> "VGVZDA=="
```