@@ -17,9 +17,9 @@ Returns the same result given same parameters. It's execution doesn't depend on
17
17
1 ) Impure
18
18
19
19
``` javascript
20
- let number = 1 ;
20
+ const number = 1 ;
21
21
22
- let increment = () => number += 1 ;
22
+ const increment = () => number += 1 ;
23
23
24
24
increment ();
25
25
// 2
@@ -28,7 +28,7 @@ increment();
28
28
2 ) Pure
29
29
30
30
``` javascript
31
- let increment = n => n + 1 ;
31
+ const increment = n => n + 1 ;
32
32
33
33
increment (1 );
34
34
// 2
@@ -40,9 +40,9 @@ Functions that operate on other functions, either by taking them as arguments or
40
40
1 ) Sum
41
41
42
42
``` javascript
43
- let sum = (x , y ) => x + y;
43
+ const sum = (x , y ) => x + y;
44
44
45
- let calculate = (fn , x , y ) => fn (x, y);
45
+ const calculate = (fn , x , y ) => fn (x, y);
46
46
47
47
calculate (sum, 1 , 2 );
48
48
// 3
@@ -51,13 +51,13 @@ calculate(sum, 1, 2);
51
51
2 ) Filter
52
52
53
53
``` javascript
54
- let students = [
54
+ const students = [
55
55
{name: ' Anna' , grade: 6 },
56
56
{name: ' John' , grade: 4 },
57
57
{name: ' Maria' , grade: 9 }
58
58
];
59
59
60
- let isApproved = student => student .grade >= 6 ;
60
+ const isApproved = student => student .grade >= 6 ;
61
61
62
62
students .filter (isApproved);
63
63
// [ { name: 'Anna', grade: 6 }, { name: 'Maria', grade: 9 } ]
@@ -66,7 +66,7 @@ students.filter(isApproved);
66
66
3 ) Map
67
67
68
68
``` javascript
69
- let byName = obj => obj .name ;
69
+ const byName = obj => obj .name ;
70
70
71
71
students .map (byName);
72
72
// [ 'Anna', 'John', 'Maria' ]
@@ -75,15 +75,15 @@ students.map(byName);
75
75
4 ) Chaining
76
76
77
77
``` javascript
78
- let students = [
78
+ const students = [
79
79
{name: ' Anna' , grade: 6 },
80
80
{name: ' John' , grade: 4 },
81
81
{name: ' Maria' , grade: 9 }
82
82
];
83
83
84
- let isApproved = student => student .grade >= 6 ;
84
+ const isApproved = student => student .grade >= 6 ;
85
85
86
- let byName = obj => obj .name ;
86
+ const byName = obj => obj .name ;
87
87
88
88
students .filter (isApproved).map (byName);
89
89
// ['Anna', 'Maria']
@@ -92,7 +92,7 @@ students.filter(isApproved).map(byName);
92
92
5 ) Reduce
93
93
94
94
``` javascript
95
- let totalGrades = students .reduce ((sum , student ) => sum + student .grade , 0 );
95
+ const totalGrades = students .reduce ((sum , student ) => sum + student .grade , 0 );
96
96
97
97
totalGrades
98
98
// 19
@@ -104,7 +104,7 @@ Whenever a function calls itself, creating a loop.
104
104
1 ) Countdown
105
105
106
106
``` javascript
107
- let countdown = num => {
107
+ const countdown = num => {
108
108
if (num > 0 ) {
109
109
console .log (num);
110
110
countdown (num - 1 );
@@ -124,7 +124,7 @@ countdown(5);
124
124
2 ) Factorial
125
125
126
126
``` javascript
127
- let factorial = num => {
127
+ const factorial = num => {
128
128
if (num <= 0 ) {
129
129
return 1 ;
130
130
} else {
@@ -142,9 +142,9 @@ Given a value and a function, unwraps the values to get to its inner value(s), c
142
142
1 ) Adding a value to all the elements in a array
143
143
144
144
``` javascript
145
- let plus1 = num => num + 1 ;
145
+ const plus1 = num => num + 1 ;
146
146
147
- let numbers = [1 , 2 , 3 ];
147
+ const numbers = [1 , 2 , 3 ];
148
148
numbers .map (plus1);
149
149
// [2, 3, 4]
150
150
```
@@ -155,12 +155,12 @@ The composition of two or more functions returns a new function.
155
155
1 ) Combining two functions to generate another one
156
156
157
157
``` javascript
158
- let compose = (f ,g ) => x => f (g (x));
158
+ const compose = (f ,g ) => x => f (g (x));
159
159
160
- let toUpperCase = function (x ) { return x .toUpperCase (); };
161
- let exclaim = function (x ) { return x + ' !' ; };
160
+ const toUpperCase = function (x ) { return x .toUpperCase (); };
161
+ const exclaim = function (x ) { return x + ' !' ; };
162
162
163
- let angry = compose (exclaim, toUpperCase);
163
+ const angry = compose (exclaim, toUpperCase);
164
164
165
165
angry (" stop this" );
166
166
// STOP THIS!
@@ -169,13 +169,13 @@ angry("stop this");
169
169
2 ) Combining three functions to generate another one
170
170
171
171
``` javascript
172
- let compose = (f ,g ) => x => f (g (x));
172
+ const compose = (f ,g ) => x => f (g (x));
173
173
174
- let toUpperCase = function (x ) { return x .toUpperCase (); };
175
- let exclaim = function (x ) { return x + ' !' ; };
176
- let moreExclaim = function (x ) { return x + ' !!!!!' ; };
174
+ const toUpperCase = function (x ) { return x .toUpperCase (); };
175
+ const exclaim = function (x ) { return x + ' !' ; };
176
+ const moreExclaim = function (x ) { return x + ' !!!!!' ; };
177
177
178
- let reallyAngry = compose (exclaim, (toUpperCase, moreExclaim));
178
+ const reallyAngry = compose (exclaim, (toUpperCase, moreExclaim));
179
179
180
180
reallyAngry (" stop this" );
181
181
// STOP THIS!!!!!!
@@ -187,25 +187,25 @@ Extract data from arrays or objects using a syntax that mirrors the construction
187
187
1 ) Select from pattern
188
188
189
189
``` javascript
190
- let foo = () => [1 , 2 , 3 ];
190
+ const foo = () => [1 , 2 , 3 ];
191
191
192
- let [a, b] = foo ();
192
+ const [a , b ] = foo ();
193
193
console .log (a, b);
194
194
// 1 2
195
195
```
196
196
197
197
2 ) Accumulates the rest values
198
198
199
199
``` javascript
200
- let [a, ... b] = [1 , 2 , 3 ];
200
+ const [a , ... b ] = [1 , 2 , 3 ];
201
201
console .log (a, b);
202
202
// 1 [2, 3]
203
203
```
204
204
205
205
3 ) Optional parameters
206
206
207
207
``` javascript
208
- let ajax = function ({ url = " localhost" , port: p = 80 }, ... data ) {
208
+ const ajax = function ({ url = " localhost" , port: p = 80 }, ... data ) {
209
209
console .log (" Url:" , url, " Port:" , p, " Rest:" , data);
210
210
};
211
211
@@ -222,7 +222,7 @@ Taking a function that takes multiple arguments and turning it into a chain of f
222
222
1 ) Currying an Object
223
223
224
224
``` javascript
225
- let student = name => grade => ` Name: ${ name} | Grade: ${ grade} ` ;
225
+ const student = name => grade => ` Name: ${ name} | Grade: ${ grade} ` ;
226
226
227
227
student (" Matt" )(8 );
228
228
// Name: Matt | Grade: 8
@@ -231,10 +231,10 @@ student("Matt")(8);
231
231
2 ) Currying a Sum
232
232
233
233
``` javascript
234
- let add = x => y => x + y;
234
+ const add = x => y => x + y;
235
235
236
- let increment = add (1 );
237
- let addFive = add (5 );
236
+ const increment = add (1 );
237
+ const addFive = add (5 );
238
238
239
239
increment (3 );
240
240
// 4
0 commit comments