@@ -5,8 +5,10 @@ const arr = [1, 2, 3, 4, 5];
5
5
// Building a sum function with reduce
6
6
const sumReducer = ( acc , value ) => acc + value ;
7
7
8
+ log ( 'Sum with Reduce' ) ;
8
9
let total = arr . reduce ( sumReducer , 0 ) ;
9
10
log ( total ) ; // 15
11
+ log ( ) ;
10
12
11
13
/* -------------------------------------- */
12
14
@@ -53,6 +55,7 @@ let evenValues = filter(isEven, arr);
53
55
log ( 'Filter with Reduce' ) ;
54
56
log ( moreThan3Value ) ; // [4, 5]
55
57
log ( evenValues ) ; // [2, 4]
58
+ log ( ) ;
56
59
57
60
/* -------------------------------------- */
58
61
@@ -71,4 +74,21 @@ const h = (x) => x * 3;
71
74
const composedFns = compose ( f , g , h ) ;
72
75
let finalValue = composedFns ( 2 ) ;
73
76
77
+ log ( 'Compose with Reduce' ) ;
74
78
log ( finalValue ) ; // 4
79
+ log ( ) ;
80
+
81
+ /* -------------------------------------- */
82
+
83
+ // Building a pipe function with reduce
84
+ // API: pipe(fn1, fn2, fn3, ...)
85
+ // fn1: function to be composed
86
+
87
+ const pipe = ( ...fns ) => x => fns . reduce ( ( pipedFns , fn ) => fn ( pipedFns ) , x ) ;
88
+
89
+ const pipedFns = pipe ( f , g , h ) ;
90
+ finalValue = pipedFns ( 1 ) ;
91
+
92
+ log ( 'Pipe with Reduce' ) ;
93
+ log ( finalValue ) ; // 3
94
+ log ( ) ;
0 commit comments