Walkers
What's a walker?
In gridl a grid can be traversed with a so called walker. A walker is a generator function that iterates from one cell to the next by calling the walker's next()
method.
import {createWalker} from "gridl/core";
const grid = createGridFromArray2D([
[1, 1, 1],
[1, 1, 1],
]);
const walker = createWalker(grid);
walker.next(); // => {value: {index: 0, position: {x: 0, y: 0}}, done: false}
walker.next(); // => {value: {index: 1, position: {x: 1, y: 0}}, done: false}
walker.next(); // => {value: {index: 2, position: {x: 2, y: 0}}, done: false}
walker.next(); // => {value: {index: 3, position: {x: 0, y: 1}}, done: false}
walker.next(); // => {value: {index: 4, position: {x: 1, y: 1}}, done: false}
walker.next(); // => {value: {index: 5, position: {x: 2, y: 1}}, done: false}
walker.next(); // => {value: undefined, done: true}
Traversing in a different order
The order in which the walker traverses the grid is determined by the specified walk
function, which by default is walkWENS(). WENS stands for West, East, North, South, which means that the grid is traversed from West to East and from North to South. A walk
function takes a cell index (positive integer between 0 and the number of cells minus 1) and calculates the associated position on the grid.
import {createWalker, walkSNWE} from "gridl/core";
const grid = createGridFromArray2D([
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[9, 10, 11]
]);
const walker = createWalker(grid, walkSNWE);
// traversing order using walkSNWE would be:
// [
// [3, 7, 11],
// [2, 6, 10],
// [1, 5, 9],
// [0, 4, 8],
// ]
Functions that use walkers
Walkers are used by all iterating functions such as forEachCell, reduceGrid, findCell, findPosition, etc. This allows you to traverse the grid in a very flexible way.
import {createWalker, walkSNWE} from "gridl/core";
import {forEachCell} from "gridl/sideEffects";
const grid = createGridFromArray2D([
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[9, 10, 11]
]);
const arr = [];
forEachCell(grid, (cellValue) => {
arr.push(cellValue);
}, walkSNWE);
// => arr is: [9, 6, 3, 0, 10, 7, 4, 1, 11, 8, 5, 2]