Skip to content

Commit 48b4396

Browse files
committed
Simple examples
1 parent 9315a5f commit 48b4396

File tree

5 files changed

+172
-0
lines changed

5 files changed

+172
-0
lines changed

‎JavaScript/1-identifiers.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'use strict';
2+
3+
// Arguments instead of mutable variables
4+
5+
const { PI, sqrt } = Math;
6+
const square = x => x * x;
7+
8+
// Imperative
9+
10+
const truncatedConeVolume = ({ height, r1, r2 }) => {
11+
const k = PI / 3;
12+
const sr1 = square(r1);
13+
const sr2 = square(r2);
14+
return k * height * (sr1 + r1 * r2 + sr2);
15+
};
16+
17+
const truncatedConeArea = ({ height, r1, r2 }) => {
18+
const sr1 = square(r1);
19+
const sr2 = square(r2);
20+
const l = sqrt(square(height) + square(r2 - r1));
21+
return PI * (sr1 + sr2 + l * (r1 + r2));
22+
};
23+
24+
{
25+
const cone = { height: 7, r1: 10, r2: 15 };
26+
cone.v = truncatedConeVolume(cone);
27+
cone.s = truncatedConeArea(cone);
28+
console.dir(cone);
29+
}
30+
31+
// Functional
32+
33+
const volume = (height, r1, r2) => (PI * height / 3) *
34+
(square(r1) + r1 * r2 + square(r2));
35+
36+
const area = (height, r1, r2) => PI * (
37+
square(r1) + square(r2) +
38+
sqrt(square(height) + square(r2 - r1)) * (r1 + r2)
39+
);
40+
41+
const cone = (height, r1, r2, volume, area) => ({
42+
height, r1, r2, volume, area
43+
});
44+
45+
const calcCone = ({ height, r1, r2 }) => cone(
46+
height, r1, r2, volume(height, r1, r2), area(height, r1, r2)
47+
);
48+
49+
{
50+
const cone = calcCone({ height: 7, r1: 10, r2: 15 });
51+
console.dir(cone);
52+
}

‎JavaScript/2-condition.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
3+
// Calls instead of if statement
4+
5+
const person = {
6+
name: 'Marcus Aurelius',
7+
born: 121,
8+
city: 'Roma',
9+
};
10+
11+
// Imperative
12+
13+
{
14+
let output = `${person.born} `;
15+
if (person.born < 0) {
16+
output += 'BC';
17+
} else {
18+
output += 'AD';
19+
}
20+
output = `${person.name} was born in ${output}`;
21+
console.dir(output);
22+
}
23+
24+
// Functional
25+
26+
{
27+
const era = year => (year < 0 ? 'BC' : 'AD');
28+
const { name, born } = person;
29+
const output = `${name} was born in ${born} ${era(born)}`;
30+
console.dir(output);
31+
}

‎JavaScript/3-iteration.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use strict';
2+
3+
// Call or recursion instead of for loops
4+
5+
const numbers = [2, 7, -1, -5, 8];
6+
7+
// Imperative
8+
9+
for (let i = 0; i < numbers.length; i++) {
10+
const n = numbers[i];
11+
console.log(`Item ${i} is ${n}`);
12+
}
13+
console.log();
14+
15+
// Loop function
16+
17+
const loop = (min, max, fn) => {
18+
for (let i = 0; i < max; i++) fn(i);
19+
};
20+
21+
loop(0, numbers.length, i => {
22+
const n = numbers[i];
23+
console.log(`Item ${i} is ${n}`);
24+
});
25+
26+
console.log();
27+
28+
// Recursion
29+
30+
const iterate = (arr, fn, i = 0) => {
31+
if (i === arr.length) return;
32+
fn(arr[i], i);
33+
iterate(arr, fn, ++i);
34+
};
35+
36+
iterate(numbers, (n, i) => {
37+
console.log(`Item ${i} is ${n}`);
38+
});
39+
40+
console.log();
41+
42+
// forEach
43+
44+
numbers.forEach((n, i) => {
45+
console.log(`Item ${i} is ${n}`);
46+
});

‎JavaScript/4-arguments.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
// Single argument instead of multiple
4+
5+
// Imperative
6+
7+
const max = (a, b) => {
8+
if (a > b) return a;
9+
else return b;
10+
};
11+
console.log(max(3, 5));
12+
13+
// Functional
14+
15+
const maxc = a => b => (a > b ? a : b);
16+
console.log(maxc(3)(5));

‎JavaScript/5-instance.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
// Closure context instead of object context
4+
5+
// Imperative
6+
7+
const marcus = {
8+
name: 'Marcus Aurelius',
9+
born: 121,
10+
get era() {
11+
return this.born < 0 ? 'BC' : 'AD';
12+
},
13+
toString() {
14+
return `${this.name} was born in ${this.born} ${this.era}`;
15+
},
16+
};
17+
18+
console.log(`${marcus}`);
19+
20+
// Functional
21+
22+
const era = year => (year < 0 ? 'BC' : 'AD');
23+
const person = (name, born) => () => `${name} was born in ${born} ${era(born)}`;
24+
25+
const marcus2 = person('Marcus Aurelius', 121, 'Roma');
26+
27+
console.log(marcus2());

0 commit comments

Comments
 (0)