-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.js
85 lines (69 loc) · 1.45 KB
/
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
console.log(22);
console.log("p");
console.log(1 + 3);
// 1.1.2, introduce wrapper to avoid corruption
(function(){
var pi = 3.1415,
radius = 10;
circumference = 2 * pi * radius;
console.log(circumference);
})();
try {
console.log(p);
} catch (e) {
console.log(e);
}
// exercise 1.31 - Higher Order Functions
(function() {
function sum_range (x, y, term, next) {
function iter (a, result) {
return a > y ? result : iter( next(a), result + term(a));
}
return iter (x, 0);
}
function identity(x) {
return x;
}
function increment1(x) {
return x + 1;
}
console.log(sum_range(1, 10, identity, increment1));
})();
// exercise 1.42
(function() {
function inc(x) {
return function() {
return x + 1;
}
}
function square(x) {
return x * x;
}
function compose(fn_one, fn_two) {
return function(x) {
return fn_one(fn_two(x)());
}
}
console.log(inc(4)())
console.log(compose(square, inc)(5))
console.log(compose(square, inc)(6))
console.log(compose(function(x) { return x * x }, function(x) { return function() { return x + 1}})(6))
})();
(function() {
console.log('\n');
console.log('compose');
function compose(fnx, fny) {
return function(x) {
return fnx(fny(x));
}
}
function square(x) {
return x * x;
}
function identity(x) {
return x;
}
console.log(compose(identity, square)(6));
console.log(compose(square, identity)(6));
console.log('\n');
})();