Skip to content

Commit eb9c356

Browse files
committed
Optimize examples
1 parent 3bd1995 commit eb9c356

File tree

7 files changed

+34
-32
lines changed

7 files changed

+34
-32
lines changed

JavaScript/1-context.js

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

3-
function hash() {
3+
const hash = () => {
44
const data = {};
55
let counter = 0;
6-
return function(key, value) {
6+
return (key, value) => {
77
data[key] = value;
88
counter++;
99
console.dir({ counter });
1010
return data;
1111
};
12-
}
12+
};
1313

1414
const h1 = hash();
1515
h1('name', 'Marcus');

JavaScript/2-chain.js

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

3-
function hash() {
3+
const hash = () => {
44
const data = {};
55
Object.defineProperty(data, 'add', {
66
enumerable: false,
@@ -10,7 +10,7 @@ function hash() {
1010
}
1111
});
1212
return data;
13-
}
13+
};
1414

1515
console.dir(
1616
hash()

JavaScript/3-closure.js

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

3-
function add(x) {
4-
return function(y) {
5-
const z = x + y;
6-
console.log(x + '+' + y + '=' + z);
7-
return z;
8-
};
9-
}
10-
11-
add(3)(6);
3+
const add = x => y => {
4+
const z = x + y;
5+
console.log(x + '+' + y + '=' + z);
6+
return z;
7+
};
8+
9+
// const add = x => y => x + y;
10+
11+
const res = add(3)(6);
12+
13+
console.log(res);

JavaScript/4-closure-recursive.js

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

3-
function add(x) {
4-
return function(y) {
5-
const z = x + y;
6-
console.log(x + '+' + y + '=' + z);
7-
return add(z);
8-
};
9-
}
3+
const add = x => y => {
4+
const z = x + y;
5+
console.log(x + '+' + y + '=' + z);
6+
return add(z);
7+
};
108

119
// const add = x => y => add(x + y);
1210

@@ -16,4 +14,8 @@ const a3 = a2(3);
1614
const a4 = a1(1);
1715
const a5 = a2(10);
1816

19-
add(1)(4)(8)(8);
17+
console.log(a1, a2, a3, a4, a5);
18+
19+
const res = add(1)(4)(8)(8);
20+
21+
console.log(res);

JavaScript/5-logger.js

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

3-
function logger(level) {
3+
const logger = level => {
44
const color = logger.colors[level] || logger.colors.info;
5-
return function(s) {
5+
return s => {
66
const date = new Date().toISOString();
77
console.log(color + date + '\t' + s);
88
};
9-
}
9+
};
1010

1111
logger.colors = {
1212
warning: '\x1b[1;33m',

JavaScript/6-functor.js

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

3-
function add(x) {
4-
const f = function(y) {
3+
const add = x => {
4+
const f = y => {
55
const z = x + y;
66
console.log(x + '+' + y + '=' + z);
77
return add(z);
88
};
9-
f.map = function(fn) {
10-
return fn(x);
11-
};
9+
f.map = fn => fn(x);
1210
return f;
1311
}
1412

Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
'use strict';
22

3-
function add(x) {
3+
const add = x => {
44
const f = y => add(x + y);
55
f.map = fn => fn(x);
66
return f;
7-
}
7+
};
88

99
add(2)(7)(1).map(console.log);

0 commit comments

Comments
 (0)