forked from imteekay/functional-programming-learning-path
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomposing_functions.js
More file actions
64 lines (50 loc) · 1.34 KB
/
Copy pathcomposing_functions.js
File metadata and controls
64 lines (50 loc) · 1.34 KB
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
const log = input => console.log(input);
// Function composition is the process of applying a function to the output of another function.
const g = n => n + 1;
const f = n => n * 2;
const doStuff = x => {
const gOutput = g(x);
const fOutput = f(gOutput);
return fOutput;
};
let result = doStuff(20);
log(result); // 42
// A different (better) way to do composition
const doStuffBetter = x => f(g(x));
result = doStuffBetter(20);
log(result); // 42
// debugging the doStuff function
const doStuffWithDebugging = x => {
const gOutput = g(x);
console.log(`after g: ${gOutput}`);
const fOutput = f(gOutput);
console.log(`after f: ${fOutput}`);
return fOutput;
};
doStuffWithDebugging(20);
// after g: 21
// after f: 42
// debugging the doStuffBetter function
const compose = (...fns) => n => fns.reduceRight((acc, fn) => fn(acc), n);
const trace = message => input => {
console.log(`${message} ${input}`);
return input;
};
const doStuffBetterWithDebugging = compose(
trace("after f:"),
f,
trace("after g:"),
g
);
doStuffBetterWithDebugging(20);
// after g: 21
// after f: 42
// If you’re chaining, you’re composing.
const list = [1, 2, 3, 4, 5];
const sumEvenNumbers = list => {
return list
.filter(n => n % 2 == 0)
.map(n => n * 2)
.reduce((previous, current) => previous + current);
};
log(sumEvenNumbers(list)); // 12