createBoard imperative version
This commit is contained in:
parent
aace63b1a2
commit
d0802d850b
@ -20,14 +20,31 @@ function evolve1d(state, rules) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// create a 2D board from a 1D CA initial state
|
// create a 2D board from a 1D CA initial state
|
||||||
|
// function createBoard(state, rules, height) {
|
||||||
|
// function createBoardAcc(s, h, acc) {
|
||||||
|
// if (h === 0) return acc;
|
||||||
|
// const newState = evolve1d(s, rules);
|
||||||
|
// const newAcc = acc.concat([s]);
|
||||||
|
// return createBoardAcc(newState, h - 1, newAcc);
|
||||||
|
// }
|
||||||
|
// return createBoardAcc(state, height, []);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// performance "choke point" in full imperative
|
||||||
function createBoard(state, rules, height) {
|
function createBoard(state, rules, height) {
|
||||||
function createBoardAcc(s, h, acc) {
|
var board = [];
|
||||||
if (h === 0) return acc;
|
let prevState = [];
|
||||||
const newState = evolve1d(s, rules);
|
for (let i = 0; i < height; i++) {
|
||||||
const newAcc = acc.concat([s]);
|
let nextState = [];
|
||||||
return createBoardAcc(newState, h - 1, newAcc);
|
if (i == 0) {
|
||||||
|
nextState = evolve1d(state, rules);
|
||||||
|
} else {
|
||||||
|
nextState = evolve1d(prevState, rules);
|
||||||
}
|
}
|
||||||
return createBoardAcc(state, height, []);
|
board = board.concat([nextState]);
|
||||||
|
prevState = nextState;
|
||||||
|
}
|
||||||
|
return board;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find the neighbor of a given cell in a 2D CA board
|
// Find the neighbor of a given cell in a 2D CA board
|
||||||
|
Loading…
Reference in New Issue
Block a user