Skip to content

Commit 4a0e077

Browse files
committed
Remove symbolic links to outer folders
1 parent 34e13ea commit 4a0e077

File tree

181 files changed

+14739
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

181 files changed

+14739
-2
lines changed

docs/code

Lines changed: 0 additions & 1 deletion
This file was deleted.

docs/code/animatevillage.js

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
// test: no
2+
3+
(function() {
4+
"use strict"
5+
6+
let active = null
7+
8+
const places = {
9+
"Alice's House": {x: 279, y: 100},
10+
"Bob's House": {x: 295, y: 203},
11+
"Cabin": {x: 372, y: 67},
12+
"Daria's House": {x: 183, y: 285},
13+
"Ernie's House": {x: 50, y: 283},
14+
"Farm": {x: 36, y: 118},
15+
"Grete's House": {x: 35, y: 187},
16+
"Marketplace": {x: 162, y: 110},
17+
"Post Office": {x: 205, y: 57},
18+
"Shop": {x: 137, y: 212},
19+
"Town Hall": {x: 202, y: 213}
20+
}
21+
const placeKeys = Object.keys(places)
22+
23+
const speed = 2
24+
25+
class Animation {
26+
constructor(worldState, robot, robotState) {
27+
this.worldState = worldState
28+
this.robot = robot
29+
this.robotState = robotState
30+
this.turn = 0
31+
32+
let outer = (window.__sandbox ? window.__sandbox.output.div : document.body), doc = outer.ownerDocument
33+
this.node = outer.appendChild(doc.createElement("div"))
34+
this.node.style.cssText = "position: relative; line-height: 0.1; margin-left: 10px"
35+
this.map = this.node.appendChild(doc.createElement("img"))
36+
this.map.src = "img/village2x.png"
37+
this.map.style.cssText = "vertical-align: -8px"
38+
this.robotElt = this.node.appendChild(doc.createElement("div"))
39+
this.robotElt.style.cssText = `position: absolute; transition: left ${0.8 / speed}s, top ${0.8 / speed}s;`
40+
let robotPic = this.robotElt.appendChild(doc.createElement("img"))
41+
robotPic.src = "img/robot_moving2x.gif"
42+
this.parcels = []
43+
44+
this.text = this.node.appendChild(doc.createElement("span"))
45+
this.button = this.node.appendChild(doc.createElement("button"))
46+
this.button.style.cssText = "color: white; background: #28b; border: none; border-radius: 2px; padding: 2px 5px; line-height: 1.1; font-family: sans-serif; font-size: 80%"
47+
this.button.textContent = "Stop"
48+
49+
this.button.addEventListener("click", () => this.clicked())
50+
this.schedule()
51+
52+
this.updateView()
53+
this.updateParcels()
54+
55+
this.robotElt.addEventListener("transitionend", () => this.updateParcels())
56+
}
57+
58+
59+
updateView() {
60+
let pos = places[this.worldState.place]
61+
this.robotElt.style.top = (pos.y - 38) + "px"
62+
this.robotElt.style.left = (pos.x - 16) + "px"
63+
64+
this.text.textContent = ` Turn ${this.turn} `
65+
}
66+
67+
updateParcels() {
68+
while (this.parcels.length) this.parcels.pop().remove()
69+
let heights = {}
70+
for (let {place, address} of this.worldState.parcels) {
71+
let height = heights[place] || (heights[place] = 0)
72+
heights[place] += 14
73+
let node = document.createElement("div")
74+
let offset = placeKeys.indexOf(address) * 16
75+
node.style.cssText = "position: absolute; height: 16px; width: 16px; background-image: url(img/parcel2x.png); background-position: 0 -" + offset + "px";
76+
if (place == this.worldState.place) {
77+
node.style.left = "25px"
78+
node.style.bottom = (20 + height) + "px"
79+
this.robotElt.appendChild(node)
80+
} else {
81+
let pos = places[place]
82+
node.style.left = (pos.x - 5) + "px"
83+
node.style.top = (pos.y - 10 - height) + "px"
84+
this.node.appendChild(node)
85+
}
86+
this.parcels.push(node)
87+
}
88+
}
89+
90+
tick() {
91+
let {direction, memory} = this.robot(this.worldState, this.robotState)
92+
this.worldState = this.worldState.move(direction)
93+
this.robotState = memory
94+
this.turn++
95+
this.updateView()
96+
if (this.worldState.parcels.length == 0) {
97+
this.button.remove()
98+
this.text.textContent = ` Finished after ${this.turn} turns`
99+
this.robotElt.firstChild.src = "img/robot_idle2x.png"
100+
} else {
101+
this.schedule()
102+
}
103+
}
104+
105+
schedule() {
106+
this.timeout = setTimeout(() => this.tick(), 1000 / speed)
107+
}
108+
109+
clicked() {
110+
if (this.timeout == null) {
111+
this.schedule()
112+
this.button.textContent = "Stop"
113+
this.robotElt.firstChild.src = "img/robot_moving2x.gif"
114+
} else {
115+
clearTimeout(this.timeout)
116+
this.timeout = null
117+
this.button.textContent = "Start"
118+
this.robotElt.firstChild.src = "img/robot_idle2x.png"
119+
}
120+
}
121+
}
122+
123+
window.runRobotAnimation = function(worldState, robot, robotState) {
124+
if (active && active.timeout != null)
125+
clearTimeout(active.timeout)
126+
active = new Animation(worldState, robot, robotState)
127+
}
128+
})()

docs/code/chapter/.keep

Whitespace-only changes.

docs/code/chapter/04_data.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
var journal = [];
2+
3+
function addEntry(events, squirrel) {
4+
journal.push({events, squirrel});
5+
}
6+
7+
function phi(table) {
8+
return (table[3] * table[0] - table[2] * table[1]) /
9+
Math.sqrt((table[2] + table[3]) *
10+
(table[0] + table[1]) *
11+
(table[1] + table[3]) *
12+
(table[0] + table[2]));
13+
}
14+
15+
function tableFor(event, journal) {
16+
let table = [0, 0, 0, 0];
17+
for (let i = 0; i < journal.length; i++) {
18+
let entry = journal[i], index = 0;
19+
if (entry.events.includes(event)) index += 1;
20+
if (entry.squirrel) index += 2;
21+
table[index] += 1;
22+
}
23+
return table;
24+
}
25+
26+
function journalEvents(journal) {
27+
let events = [];
28+
for (let entry of journal) {
29+
for (let event of entry.events) {
30+
if (!events.includes(event)) {
31+
events.push(event);
32+
}
33+
}
34+
}
35+
return events;
36+
}
37+
38+
for (let entry of JOURNAL) {
39+
if (entry.events.includes("peanuts") &&
40+
!entry.events.includes("brushed teeth")) {
41+
entry.events.push("peanut teeth");
42+
}
43+
}
44+
45+
var list = {
46+
value: 1,
47+
rest: {
48+
value: 2,
49+
rest: {
50+
value: 3,
51+
rest: null
52+
}
53+
}
54+
};
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
function repeat(n, action) {
2+
for (let i = 0; i < n; i++) {
3+
action(i);
4+
}
5+
}
6+
7+
function characterScript(code) {
8+
for (let script of SCRIPTS) {
9+
if (script.ranges.some(([from, to]) => code >= from &&
10+
code < to)) {
11+
return script;
12+
}
13+
}
14+
return null;
15+
}
16+
17+
function countBy(items, groupName) {
18+
let counts = [];
19+
for (let item of items) {
20+
let name = groupName(item);
21+
let known = counts.findIndex(c => c.name == name);
22+
if (known == -1) {
23+
counts.push({name, count: 1});
24+
} else {
25+
counts[known].count++;
26+
}
27+
}
28+
return counts;
29+
}
30+
31+
function textScripts(text) {
32+
let scripts = countBy(text, char => {
33+
let script = characterScript(char.codePointAt(0));
34+
return script ? script.name : "none";
35+
}).filter(({name}) => name != "none");
36+
37+
let total = scripts.reduce((n, {count}) => n + count, 0);
38+
if (total == 0) return "No scripts found";
39+
40+
return scripts.map(({name, count}) => {
41+
return `${Math.round(count * 100 / total)}% ${name}`;
42+
}).join(", ");
43+
}

docs/code/chapter/06_objeto.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
function speak(line) {
2+
console.log(`The ${this.type} rabbit says '${line}'`);
3+
}
4+
var whiteRabbit = {type: "white", speak};
5+
var hungryRabbit = {type: "hungry", speak};
6+
7+
8+
var Rabbit = class Rabbit {
9+
constructor(type) {
10+
this.type = type;
11+
}
12+
speak(line) {
13+
console.log(`The ${this.type} rabbit says '${line}'`);
14+
}
15+
}
16+
17+
var killerRabbit = new Rabbit("killer");
18+
var blackRabbit = new Rabbit("black");
19+
20+
Rabbit.prototype.toString = function() {
21+
return `a ${this.type} rabbit`;
22+
};
23+
24+
var toStringSymbol = Symbol("toString");
25+
26+
var Matrix = class Matrix {
27+
constructor(width, height, content = (x, y) => undefined) {
28+
this.width = width;
29+
this.height = height;
30+
this.content = [];
31+
32+
for (let y = 0; y < height; y++) {
33+
for (let x = 0; x < width; x++) {
34+
this.content[y * width + x] = content(x, y);
35+
}
36+
}
37+
}
38+
39+
get(x, y) {
40+
return this.content[y * this.width + x];
41+
}
42+
set(x, y, value) {
43+
this.content[y * this.width + x] = value;
44+
}
45+
}
46+
47+
var MatrixIterator = class MatrixIterator {
48+
constructor(matrix) {
49+
this.x = 0;
50+
this.y = 0;
51+
this.matrix = matrix;
52+
}
53+
54+
next() {
55+
if (this.y == this.matrix.height) return {done: true};
56+
57+
let value = {x: this.x,
58+
y: this.y,
59+
value: this.matrix.get(this.x, this.y)};
60+
this.x++;
61+
if (this.x == this.matrix.width) {
62+
this.x = 0;
63+
this.y++;
64+
}
65+
return {value, done: false};
66+
}
67+
}
68+
69+
Matrix.prototype[Symbol.iterator] = function() {
70+
return new MatrixIterator(this);
71+
};
72+
73+
var SymmetricMatrix = class SymmetricMatrix extends Matrix {
74+
constructor(size, content = (x, y) => undefined) {
75+
super(size, size, (x, y) => {
76+
if (x < y) return content(y, x);
77+
else return content(x, y);
78+
});
79+
}
80+
81+
set(x, y, value) {
82+
super.set(x, y, value);
83+
if (x != y) {
84+
super.set(y, x, value);
85+
}
86+
}
87+
}
88+
89+
var matrix = new SymmetricMatrix(5, (x, y) => `${x},${y}`);

0 commit comments

Comments
 (0)