Skip to content

Commit ff6adc2

Browse files
committed
Improve examples
1 parent cfae217 commit ff6adc2

4 files changed

Lines changed: 24 additions & 30 deletions

File tree

JavaScript/2-extended.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
const curry = (fn, x) => (...args) => fn(x, ...args);
44

5+
// Projection
6+
57
const projection = (meta, obj) => (
68
Object.keys(meta).reduce((hash, key) => (
79
hash[key] = meta[key].reduce(
@@ -10,14 +12,6 @@ const projection = (meta, obj) => (
1012
), {})
1113
);
1214

13-
const upper = (s) => (
14-
typeof(s) === 'string' ? s.toUpperCase() : ''
15-
);
16-
17-
const age = (year) => (
18-
new Date().getFullYear() - new Date(year + '').getFullYear()
19-
);
20-
2115
// Dataset
2216

2317
const persons = [
@@ -32,10 +26,13 @@ const persons = [
3226

3327
const md = {
3428
name: ['name'],
35-
place: ['city', upper, s => '<' + s + '>'],
36-
age: ['born', age]
29+
place: ['city', s => '<' + s.toUpperCase() + '>'],
30+
age: ['born', year => (
31+
new Date().getFullYear() - new Date(year + '').getFullYear()
32+
)]
3733
};
3834

35+
// Usage
3936

4037
const p1 = curry(projection, md);
4138
const data = persons.map(p1);

JavaScript/3-cached.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
// Projection
4+
35
const projection = (meta) => {
46
const keys = Object.keys(meta);
57
return obj => keys.reduce((hash, key) => (
@@ -9,14 +11,6 @@ const projection = (meta) => {
911
), {});
1012
};
1113

12-
const upper = (s) => (
13-
typeof(s) === 'string' ? s.toUpperCase() : ''
14-
);
15-
16-
const age = (year) => (
17-
new Date().getFullYear() - new Date(year + '').getFullYear()
18-
);
19-
2014
// Dataset
2115

2216
const persons = [
@@ -31,8 +25,10 @@ const persons = [
3125

3226
const md = {
3327
name: ['name'],
34-
place: ['city', upper, s => '<' + s + '>'],
35-
age: ['born', age]
28+
place: ['city', s => '<' + s.toUpperCase() + '>'],
29+
age: ['born', year => (
30+
new Date().getFullYear() - new Date(year + '').getFullYear()
31+
)]
3632
};
3733

3834
// Usage

JavaScript/4-imperative.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
// Projection
4+
35
const projection = (meta) => {
46
const keys = Object.keys(meta);
57
return obj => {
@@ -14,10 +16,6 @@ const projection = (meta) => {
1416
};
1517
};
1618

17-
const age = (year) => (
18-
new Date().getFullYear() - new Date(year + '').getFullYear()
19-
);
20-
2119
// Dataset
2220

2321
const persons = [
@@ -33,8 +31,10 @@ const persons = [
3331
const md = {
3432
name: ['name'],
3533
place: ['city', s => '<' + s.toUpperCase() + '>'],
36-
born: ['born'],
37-
age: ['born', age]
34+
age: ['born', year => (
35+
new Date().getFullYear() -
36+
new Date(year + '').getFullYear()
37+
)]
3838
};
3939

4040
// Usage

JavaScript/5-optimized.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
// Projection
4+
35
const projection = (meta) => {
46
const keys = Object.keys(meta);
57
return obj => {
@@ -33,10 +35,9 @@ const md = {
3335
place: ['city', s => '<' + s.toUpperCase() + '>'],
3436
born: ['born'],
3537
age: ['born', year => (
36-
new Date().getFullYear() -
37-
new Date(year + '').getFullYear()
38-
)
39-
]
38+
new Date().getFullYear() -
39+
new Date(year + '').getFullYear()
40+
)]
4041
};
4142

4243
// Usage

0 commit comments

Comments
 (0)