-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPipe-and-compose.js
120 lines (83 loc) · 2.76 KB
/
Pipe-and-compose.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
/*
In JavaScript, compose and pipe are two functional programming concepts that
allow functions to be combined and executed in a specific order.
Conclusion:
Compose is useful when you want to apply functions from right to left, usually seen in functional programming paradigms.
Pipe is the opposite, where functions are applied from left to right, and often feels more intuitive in JavaScript.
*/
// Pipe and Compose
const add2 = (x) => x + 2;
const subtract1 = (x) => x - 1;
const multiply5 = (x) => x * 5;
// inside - outside and right-left
const ans = add2(subtract1(multiply5(5)));
const dividedBy = (divisor, num) => num / divisor;
console.log(ans);
// Compose Function
// by using reduce we can implement compose function
// Reduce is HOC, because it takes a function as argument and return a function
const compose = (...args) => {
return (val) => {
return args.reduceRight((prev, currFun) => {
return currFun(prev);
}, val)
}
}
const compResult = compose(
add2,
subtract1,
multiply5,
(x) => dividedBy(2, x)
)(5);
console.log(compResult);
// Pipe Function moves from left to right
const pipe = (...args) => {
return (val) => {
return args.reduce((prev, currFun) => {
return currFun(prev);
}, val);
}
}
const divBy = (divisor) => (num) => num / divisor; // currying
const dividedBy2 = divBy(2); // partially applied.
const pipeResult = pipe(
add2,
subtract1,
multiply5,
dividedBy2
)(5);
console.log(pipeResult);
//////////////////////////////////////////////////////////////////
const lorem = "Lorem ipsum dolor sit amet consectetur adipisicing elit. Commodi ex dolores nostrum esse voluptate cupiditate cum qui, molestias doloribus, eaque explicabo animi magnam."
const wordsList = (lorem) => lorem.split(" ");
const getLength = (wordsList) => wordsList.length;
console.log(getLength(wordsList(lorem)));
const stringPipe = pipe(
wordsList,
getLength
);
console.log("StringPipe: ", stringPipe(lorem));
//////////////////////////////////////////////////////////////////
// let's create a palindrome check with pipe function
let str1 = "ABBA ABBA";
let str2 = "ACID DICA"
let str3 = "ANK IT";
const lowerCaseConvertor = (string) => string.toLowerCase();
const split = (string) => string.split("");
const join = (string) => string.join('');
const reverse = (string) => string.reverse();
const fwdPipe = pipe(
lowerCaseConvertor,
split,
join
);
const bwdPipe = pipe(
lowerCaseConvertor,
split,
reverse,
join
);
console.log(">>>>>>>>>>>>>>>>>> Palindrome checking <<<<<<<<<<<<<<<<<")
console.log(fwdPipe(str1),bwdPipe(str1), fwdPipe(str1) === bwdPipe(str1))
console.log(fwdPipe(str2),bwdPipe(str2),fwdPipe(str2) === bwdPipe(str2))
console.log(fwdPipe(str3),bwdPipe(str3),fwdPipe(str3) === bwdPipe(str3))