-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathreduce.js
94 lines (68 loc) · 2.34 KB
/
reduce.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const log = (...args) => console.log(...args);
const arr = [1, 2, 3, 4, 5];
// Building a sum function with reduce
const sumReducer = (acc, value) => acc + value;
log('Sum with Reduce');
let total = arr.reduce(sumReducer, 0);
log(total); // 15
log();
/* -------------------------------------- */
// Building map function with reduce
// API: map(fn, arr);
// fn => function that will process each element in the collection
// arr => the collection to be processed
// [1, 2, 3, 4, 5] => fn => [2, 4, 6, 8, 10]
const map = (fn, arr) => arr.reduce((acc, value) => {
return acc.concat(fn(value));
}, []);
const timesTwo = value => value * 2;
const increment = value => value + 1;
const isEven = value => value % 2 === 0;
let timesTwoMapping = map(timesTwo, arr);
let incrementMapping = map(increment, arr);
let isEvenMapping = map(isEven, arr);
log('Map with Reduce');
log(timesTwoMapping); // [2, 4, 6, 8, 10]
log(incrementMapping); // [2, 3, 4, 5, 6]
log(isEvenMapping); // [false, true, false, true, false]
log();
/* -------------------------------------- */
// Building a filter function with reduce
// API: filter(fn, arr);
// fn => function that will be used to filter each element of the collection
// arr => the collection to be processed
const filter = (fn, arr) => arr.reduce((acc, value) => {
return fn(value) ? acc.concat(value) : acc
}, []);
const moreThan3 = value => value > 3;
let moreThan3Value = filter(moreThan3, arr);
let evenValues = filter(isEven, arr);
log('Filter with Reduce');
log(moreThan3Value); // [4, 5]
log(evenValues); // [2, 4]
log();
/* -------------------------------------- */
// Building a compose function with reduce
// API: compose(fn1, fn2, fn3, ...)
// fn1: function to be composed
const compose = (...fns) => x => {
return fns.reduceRight((composedFns, fn) => fn(composedFns), x)
};
const f = (x) => x + 1;
const g = (x) => x / 2;
const h = (x) => x * 3;
const composedFns = compose(f, g, h);
let finalValue = composedFns(2);
log('Compose with Reduce');
log(finalValue); // 4
log();
/* -------------------------------------- */
// Building a pipe function with reduce
// API: pipe(fn1, fn2, fn3, ...)
// fn1: function to be composed
const pipe = (...fns) => x => fns.reduce((pipedFns, fn) => fn(pipedFns), x);
const pipedFns = pipe(f, g, h);
finalValue = pipedFns(1);
log('Pipe with Reduce');
log(finalValue); // 3
log();