-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathpurity_part_1.js
38 lines (27 loc) · 1.26 KB
/
purity_part_1.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
/*
There are two qualities that make a function "pure":
- The function depends only on the input provided to it to produce a result (and not on any external state).
- The function does not cause any observable side effects, such as modifying a global object or modifying a parameter passed by reference.
*/
const PI = 3.14;
/* Not pure: it uses the global constant */
const calculateArea = (radius) => radius * radius * PI;
/* Pure: it uses only parameters passed to the function */
const calculateArea = (radius, PI) => radius * radius * PI;
/* ------------------------------------------------------ */
let count = 1;
/* Not pure: it uses the global variable and modify the variable */
const increaseCount = (value) => count = count + value;
/* ------------------------------------------------------ */
/* Not pure: it modifies the array - the reverse method reverses the array in place */
const reverseArray = (array) => array.reverse();
/* ------------------------------------------------------ */
/* Not pure: make call to an external API */
async function amountExceedsBuyLimit(amount) {
const limit = await getBuyLimits(); /* assume API call */
return amount > limit;
}
/* Pure */
async function amountExceedsBuyLimit(amount, limit) {
return amount > limit;
}