From 671ea5a330d181d789f6143222b6468356855a81 Mon Sep 17 00:00:00 2001 From: Justin Lee Date: Thu, 14 Dec 2017 14:44:38 -0800 Subject: [PATCH 1/3] Rewrote pipe such that the first function may have any arity. --- snippets/pipe.md | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/snippets/pipe.md b/snippets/pipe.md index c61ec042c..11803210c 100644 --- a/snippets/pipe.md +++ b/snippets/pipe.md @@ -1,8 +1,18 @@ ### Pipe -Use `Array.reduce()` to pass value through functions. +Use `Array.reduce()` to perform left-to-right function composition. The first (leftmost) function can accept one or more arguments; the remaining functions must be unary. ```js -const pipe = (...funcs) => arg => funcs.reduce((acc, func) => func(acc), arg); -// pipe(btoa, x => x.toUpperCase())("Test") -> "VGVZDA==" +const pipe = (...fns) => fns.reduce((f, g) => (...args) => g(f(...args))) +/* +const add5 = (x) => x + 5 +const multiply = (x, y) => x * y + +const multiplyAndAdd5 = pipe( + multiply, + add5 +) + +multiplyAndAdd5(5, 2) -> 15 +*/ ``` From 89cb11bccc1c6cc73cd4e4115e4fb237fad06d28 Mon Sep 17 00:00:00 2001 From: Justin Lee Date: Thu, 14 Dec 2017 14:52:25 -0800 Subject: [PATCH 2/3] removed unnecessary brackets around one parameter function --- snippets/pipe.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/pipe.md b/snippets/pipe.md index 11803210c..9c13f5e27 100644 --- a/snippets/pipe.md +++ b/snippets/pipe.md @@ -5,7 +5,7 @@ Use `Array.reduce()` to perform left-to-right function composition. The first (l ```js const pipe = (...fns) => fns.reduce((f, g) => (...args) => g(f(...args))) /* -const add5 = (x) => x + 5 +const add5 = x => x + 5 const multiply = (x, y) => x * y const multiplyAndAdd5 = pipe( From 0925f657da016ee9ee739a10d7532aef94cb5f6d Mon Sep 17 00:00:00 2001 From: Justin Lee Date: Thu, 14 Dec 2017 14:53:20 -0800 Subject: [PATCH 3/3] Placed parameters of pipe on one line for better readability --- snippets/pipe.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/snippets/pipe.md b/snippets/pipe.md index 9c13f5e27..f46a32148 100644 --- a/snippets/pipe.md +++ b/snippets/pipe.md @@ -8,10 +8,7 @@ const pipe = (...fns) => fns.reduce((f, g) => (...args) => g(f(...args))) const add5 = x => x + 5 const multiply = (x, y) => x * y -const multiplyAndAdd5 = pipe( - multiply, - add5 -) +const multiplyAndAdd5 = pipe(multiply, add5) multiplyAndAdd5(5, 2) -> 15 */