-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathch6.js
More file actions
107 lines (90 loc) · 2.11 KB
/
ch6.js
File metadata and controls
107 lines (90 loc) · 2.11 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// A Vector Type
function Vector (x, y) {
this.x = x;
this.y = y;
};
Vector.prototype.plus = function(vector) {
return new Vector(this.x + vector.x, this.y + vector.y);
};
Vector.prototype.minus = function(vector) {
return new Vector(this.x - vector.x, this.y - vector.y);
};
Object.defineProperty(Vector.prototype, "length", {
get: function() { return Math.sqrt(this.x * this.x, this.y * this.y) }
});
console.log(new Vector(1, 2).plus(new Vector(2, 3)));
// → Vector{x: 3, y: 5}
console.log(new Vector(1, 2).minus(new Vector(2, 3)));
// → Vector{x: -1, y: -1}
console.log(new Vector(3, 4).length);
// → 5
// Another Cell
function StretchCell (inner, width, height) {
this.inner = inner;
this.width = width;
this.height = height;
}
StretchCell.prototype.minWidth = function() {
return Math.max(this.inner.minWidth(), this.width);
};
StretchCell.prototype.minHeight = function() {
return Math.max(this.inner.minHeight(), this.height);
};
StretchCell.prototype.draw = function(width, height) {
return this.inner.draw(width, height);
}
var sc = new StretchCell(new TextCell("abc"), 1, 2);
console.log(sc.minWidth());
// → 3
console.log(sc.minHeight());
// → 2
console.log(sc.draw(3, 2));
// → ["abc", " "]
// Sequence interface
function logFive(seq) {
for(var i = 0; i < 5; i++) {
if (seq.more()) {
console.log(seq.current());
} else {
return;
}
}
}
function ArraySeq(arr) {
this.position = 0;
this.arr = arr;
}
ArraySeq.prototype.more = function() {
if (this.position >= this.arr.length) {
return false;
}
this.position ++;
return true;
}
ArraySeq.prototype.current = function() {
return this.arr[this.position - 1];
}
function RangeSeq(from, to) {
this.from = from;
this.to = to;
this.position = this.from - 1;
}
RangeSeq.prototype.more = function() {
if (this.position >= this.to) {
return false;
}
this.position ++;
return true;
}
RangeSeq.prototype.current = function() {
return this.position;
}
logFive(new ArraySeq([1, 2]));
// → 1
// → 2
logFive(new RangeSeq(100, 1000));
// → 100
// → 101
// → 102
// → 103
// → 104