interface GridInterface<A> {
get : (location: GridLocation) => A;
- map : (f : (location: GridLocation) => A) => Grid<A>;
+ map : (f : (location: GridLocation) => A) => void;
moore_neighbors: (origin : GridLocation) => Array<GridLocation>;
print : (toString : (A: A) => string) => void;
};
};
map(f : (location: GridLocation) => A) {
- const cells = [];
- for (let r = 0; r < this.rows; r++) {
- cells[r] = [];
- for (let k = 0; k < this.columns; k++) {
- const location = {r: r, k: k};
- cells[r][k] = f(location);
- }
- };
- const init = ({r, k}) => cells[r][k];
- const grid = new Grid({rows: this.rows, columns: this.columns, init: init});
- return grid
+ this.cells =
+ this.cells.map((row, r) => row.map((_, k) => f({r: r, k: k})));
};
private print_border(): void {
switch (i)
{ case 0 : return "Dead"
; case 1 : return "Alive"
- ; default: throw("No known State for integer: " + i)
+ ; default: throw new RangeError("No known State for integer: " + i)
}
};
switch (t)
{ case "Dead" : return " "
; case "Alive": return "o"
- ; default : throw("Illegal member of Life.State.T: " + t)
+ ; default : throw new TypeError("Illegal member of Life.State.T: " + t)
}
};
switch (t)
{ case "Dead" : return false
; case "Alive": return true
- ; default : throw("Illegal member of Life.State.T: " + t)
+ ; default : throw new TypeError("Illegal member of Life.State.T: " + t)
}
};
};
next() : void {
- const grid = this.grid.map(
+ this.grid.map(
(location) => {
const neighbor_locations = this.grid.moore_neighbors(location);
const neighbor_states = neighbor_locations.map((l) => this.grid.get(l));
return state_1
}
);
- this.grid = grid
};
print() : void {