Skip to content

Commit 8afc854

Browse files
committed
First pass over chapter 7
1 parent 73b01f0 commit 8afc854

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

07_robot.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ The array of strings isn't very easy to work with. What we're interested in is t
4848
function buildGraph(edges) {
4949
let graph = Object.create(null);
5050
function addEdge(from, to) {
51-
if (graph[from] == null) {
52-
graph[from] = [to];
53-
} else {
51+
if (from in graph) {
5452
graph[from].push(to);
53+
} else {
54+
graph[from] = [to];
5555
}
5656
}
5757
for (let [from, to] of edges.map(r => r.split("-"))) {
@@ -84,9 +84,7 @@ To be able to simulate this process, we must define a virtual world that can des
8484

8585
If you're thinking in terms of ((object-oriented programming)), your first impulse might be to start defining objects for the various elements in the world: a ((class)) for the robot, one for a parcel, maybe one for places. These could then hold properties that describe their current ((state)), such as the pile of parcels at a location, which we could change when updating the world.
8686

87-
This is wrong.
88-
89-
At least, it usually is. The fact that something sounds like an object does not automatically mean that it should be an object in your program. Reflexively writing classes for every concept in your application tends to leave you with a collection of interconnected objects that each have their own internal, changing state. Such programs are often hard to understand and thus easy to break.
87+
This is wrong. At least, it usually is. The fact that something sounds like an object does not automatically mean that it should be an object in your program. Reflexively writing classes for every concept in your application tends to leave you with a collection of interconnected objects that each have their own internal, changing state. Such programs are often hard to understand and thus easy to break.
9088

9189
{{index [state, in objects]}}
9290

@@ -490,9 +488,9 @@ When a value is added to the group, you can create a new group with a copy of th
490488

491489
The class's ((constructor)) can take such an array as argument and store it as the instance's (only) property. This array is never updated.
492490

493-
{{index "static method"}}
491+
{{index "static property"}}
494492

495-
To add a property (`empty`) to a constructor that is not a method, you have to add it to the constructor after the class definition, as a regular property.
493+
To add the `empty` property to the constructor, you can declare it as a static property.
496494

497495
You need only one `empty` instance because all empty groups are the same and instances of the class don't change. You can create many different groups from that single empty group without affecting it.
498496

code/solutions/07_3_persistent_group.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
class PGroup {
2+
#members;
23
constructor(members) {
3-
this.members = members;
4+
this.#members = members;
45
}
56

67
add(value) {
78
if (this.has(value)) return this;
8-
return new PGroup(this.members.concat([value]));
9+
return new PGroup(this.#members.concat([value]));
910
}
1011

1112
delete(value) {
1213
if (!this.has(value)) return this;
13-
return new PGroup(this.members.filter(m => m !== value));
14+
return new PGroup(this.#members.filter(m => m !== value));
1415
}
1516

1617
has(value) {
17-
return this.members.includes(value);
18+
return this.#members.includes(value);
1819
}
19-
}
2020

21-
PGroup.empty = new PGroup([]);
21+
static empty = new PGroup([]);
22+
}
2223

2324
let a = PGroup.empty.add("a");
2425
let ab = a.add("b");

0 commit comments

Comments
 (0)