-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathCompose-and-Pipe.js
126 lines (110 loc) Β· 3.4 KB
/
Compose-and-Pipe.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* π‘"JavaScript-with-JC"
π Pipe and Compose Implementation
Pipe and Compose functions are higher order functions that are used for writing a well structured and
clean code by avoiding nested function calls.
π Pipe flows from left-to-right, calling each function with the return result of the last one.
π Compose flows from right-to-left, calling each function with the return result of the last one.
π‘Let's take an Example -
*/
// π first function to fetch all players data
const getAllPlayers = (team) => {
// console.log("fetching all players api.....");
const india = ["virat kohli", "rohit sharma", "hardik pandya"];
const pakistan = ["babar azam", "mohammad rizwan", "fakar zaman"];
return team === "india" ? india : pakistan;
};
// π second function to get first player
const getFirstPlayer = (players) => {
return players[0];
};
// π third function to get first name of player
const getFirstName = (player) => {
return player.split(" ")[0];
};
// π fourth function to capitalize first name
const capitalizeName = (firstName) => {
return firstName.toUpperCase();
};
// π final result here you can see we are calling nested functions and
// because of this, our code is not clean and readable.
const playerName = capitalizeName(
getFirstName(getFirstPlayer(getAllPlayers("india")))
);
console.log(playerName); // VIRAT
// π‘ 1) Implementation of pipe using simple for loop
const pipe = function (...functions) {
return (...args) => {
let result;
for (let i = 0; i < functions.length; i++) {
if (i === 0) {
result = functions[i](...args);
} else {
result = functions[i](result);
}
}
return result;
};
};
// π Wrapping all the functions in one pipe ( HOF ) function to avoid nested function calls
let pipedPlayerName = pipe(
getAllPlayers,
getFirstPlayer,
getFirstName,
capitalizeName
)("india");
console.log(pipedPlayerName); // VIRAT
// π‘2) Implementation of pipe using reduce method
const pipeReduce = function (...functions) {
return (...args) => {
return functions.reduce(
(acc, currFunc, index) =>
index === 0 ? currFunc(...acc) : currFunc(acc),
args
);
};
};
let pipeReducePlayerName = pipeReduce(
getAllPlayers,
getFirstPlayer,
getFirstName,
capitalizeName
)("india");
console.log(pipeReducePlayerName); // VIRAT
//π‘3) Implementation of compose using simple for loop
const compose = function (...functions) {
return function (...args) {
let result;
for (let i = functions.length - 1; i >= 0; i--) {
if (i === functions.length - 1) {
result = functions[i](...args);
} else {
result = functions[i](result);
}
}
return result;
};
};
// π Wrapping all the functions in one compose ( HOF ) function to avoid nested function calls
const composedPlayerName = compose(
capitalizeName,
getFirstName,
getFirstPlayer,
getAllPlayers
)("india");
console.log(composedPlayerName); // VIRAT
//π‘4) Implementation of compose using reduceRight method
const composeReduce = function (...functions) {
return (...args) =>
functions.reduceRight(
(acc, currFunc, index, arr) =>
index === arr.length - 1 ? currFunc(...acc) : currFunc(acc),
args
);
};
const composeReducePlayerName = composeReduce(
capitalizeName,
getFirstName,
getFirstPlayer,
getAllPlayers
)("india");
console.log(composeReducePlayerName); // VIRAT