From f7d2695866a5194856ef791acb0fd98ab8aae0bb Mon Sep 17 00:00:00 2001 From: conblem Date: Tue, 12 Dec 2017 17:01:01 +0100 Subject: [PATCH 1/2] Pipe example --- snippets/pipe.md | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 snippets/pipe.md diff --git a/snippets/pipe.md b/snippets/pipe.md new file mode 100644 index 000000000..16f3e59d6 --- /dev/null +++ b/snippets/pipe.md @@ -0,0 +1,8 @@ +### Pipe + +Use `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==" +``` From eba53699898da07e029e6e87596a798643dd4bfb Mon Sep 17 00:00:00 2001 From: conblem Date: Tue, 12 Dec 2017 17:03:14 +0100 Subject: [PATCH 2/2] Use full name for reduce --- README.md | 10 ++++++++++ snippets/pipe.md | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4c8aa9c5a..4233f6171 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,7 @@ * [Last of list](#last-of-list) * [Measure time taken by function](#measure-time-taken-by-function) * [Object from key value pairs](#object-from-key-value-pairs) +* [Pipe](#pipe) * [Powerset](#powerset) * [Random number in range](#random-number-in-range) * [Randomize order of array](#randomize-order-of-array) @@ -327,6 +328,15 @@ const objectFromPairs = arr => arr.reduce((a,b) => (a[b[0]] = b[1], a), {}); // 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 Use `reduce()` combined with `map()` to iterate over elements and combine into an array containing all combinations. diff --git a/snippets/pipe.md b/snippets/pipe.md index 16f3e59d6..c61ec042c 100644 --- a/snippets/pipe.md +++ b/snippets/pipe.md @@ -1,6 +1,6 @@ ### Pipe -Use `reduce()` to pass value through functions. +Use `Array.reduce()` to pass value through functions. ```js const pipe = (...funcs) => arg => funcs.reduce((acc, func) => func(acc), arg);