-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctional.js
126 lines (92 loc) · 2.75 KB
/
functional.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
let states = ["Kansas", "Nebraska", "North Dakota", "South Dakota"];
// Returns a URL-friendly version of a string.
// Example: "North Dakota" -> "north-dakota"
function urlify(string) {
return string.toLowerCase().split(/\s+/).join("-");
}
// urls: Imperative version
function imperativeUrls(elements) {
let urls = [];
elements.forEach(function(element) {
urls.push(urlify(element));
});
return urls;
}
console.log(imperativeUrls(states));
// urls: Functional version
function functionalUrls(elements) {
return elements.map( element => urlify(element));
}
console.log(functionalUrls(states));
//exercise
let baseUrl="https://example.com/";
function functionalFullUrls(elements) {
return elements.map(element => baseUrl+urlify(element));
}
console.log(functionalFullUrls(states));
//singles
function imperativeSingles(elements) {
let singles=[];
elements.forEach(function(element) {
if (element.split(/\s+/).length === 1) {
singles.push(element);
}
});
return singles;
}
console.log(imperativeSingles(states));
// singles: Functional version
function functionalSingles(elements) {
return elements.filter( element => element.split(/\s+/).length === 1);
}
console.log(functionalSingles(states));
//exercises
function functionalDakotas(elements) {
return elements.filter(element => element.toLowerCase().includes("dakota"));
}
console.log(functionalDakotas(states));
function functionalSplits(elements) {
return elements.filter( element => element.split(/\s+/).length === 2);
}
console.log(functionalSplits(states));
let numbers = [1,2,3,4,5,6,7,8,9,10];
//sum: imperative solution
function imperativeSum(elements) {
let total=0;
elements.forEach(function(n) {
total += n;
});
return total;
}
console.log(imperativeSum(numbers));
// sum: Functional solution
function functionalSum(elements) {
return elements.reduce((total, n) => { return total += n; });
}
console.log(functionalSum(numbers));
// lengths: Imperative solution
function imperativeLengths(elements) {
let lengths = {};
elements.forEach(function(element) {
lengths[element]=element.length;
});
return lengths;
}
console.log(imperativeLengths(states));
// lengths: Functional solution
function functionalLength(elements) {
return elements.reduce((lengths, element) => {
lengths[element] = element.length;
return lengths;
}, {});
}
console.log(functionalLength(states));
//exercises
function functionalMultiple(elements) {
return elements.reduce((product, element) => { return product *= element; });
}
console.log(functionalMultiple(numbers));
function functionalLength2(elements) {
return elements.reduce((lengths, element) => { lengths[element] = element.length; return lengths;}, {});
}
console.log(functionalLength2(states));