grid_utils
Source: functions.
Property
Property
emptygrid
[Global] Creates a grid of size width
by height
filled with fill
.
Fill defaults to null
.
Parameters
Name | Type | Optional | Description |
---|---|---|---|
width |
number |
|
The width of the grid |
height |
number |
|
The height of the grid |
fill |
T |
Yes |
The value to fill the grid with Defaults to |
- Returns
-
Array of Array of T
Methods
antidiagonals(grid) → Array of Array of T
Gets diagonals of a grid in the other direction to grid_utils.diagonals (so top left to bottom right). Assumes the grid is rectangular.
Example
antidiagonals([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]) // [[3], [2, 6], [1, 5, 9], [4, 8], [7]]
Parameter
Name | Type | Optional | Description |
---|---|---|---|
grid |
Array of Array of T |
|
The grid to get diagonals from |
- Returns
-
Array of Array of T
diagonals(grid) → Array of Array of T
Gets diagonals of a grid (top right to bottom left). Assumes the grid is rectangular.
Example
diagonals([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]) // [[1], [2, 4], [3, 5, 7], [6, 8], [9]]
Parameter
Name | Type | Optional | Description |
---|---|---|---|
grid |
Array of Array of T |
|
The grid to get diagonals from |
- Returns
-
Array of Array of T
digitgrid(str) → Array of Array of number
Uses grid_utils.grid then maps each cell to a number
Parameter
Name | Type | Optional | Description |
---|---|---|---|
str |
string |
|
The string to create a grid from |
- Returns
-
Array of Array of number
gfind(grid, fn) → T
Returns the first cell in a grid (searching top to bottom, left to right)
where the function fn
is truthy for that cell. fn
also receives the
position of each cell and a reference to the grid.
Parameters
Name | Type | Optional | Description |
---|---|---|---|
grid |
Array of Array of T |
|
The grid to search |
fn |
function(T, Vec, Array of Array of T) |
|
The function to search with |
- Returns
-
T
gmap(grid, fn) → Array of Array of K
Maps a function over each cell in a grid
Parameters
Name | Type | Optional | Description |
---|---|---|---|
grid |
Array of Array of T |
|
The grid to map over |
fn |
function(T, Vec, Array of Array of T) |
|
The function to map |
- Returns
-
Array of Array of K
gpr(grid) → Array of Array of T
Alias for grid_utils.gprint
Parameter
Name | Type | Optional | Description |
---|---|---|---|
grid |
Array of Array of T |
|
- Returns
-
Array of Array of T
gprint(grid) → Array of Array of T
[Global] Prints a grid and returns it. If the shortest cell is only 1 character long then the grid will be printed without spaces between cells.
Parameter
Name | Type | Optional | Description |
---|---|---|---|
grid |
Array of Array of T |
|
- Returns
-
Array of Array of T
grid(str) → Array of Array of string
Calls with grid_utils.gridd with an empty delimiter
Parameter
Name | Type | Optional | Description |
---|---|---|---|
str |
string |
|
The string to create a grid from |
- Returns
-
Array of Array of string
gridd(str, delim) → Array of Array of string
Creates a grid from a string where each row is separated by a newline and
each cell is seperated by delim
.
Parameters
Name | Type | Optional | Description |
---|---|---|---|
str |
string |
|
The string to create a grid from |
delim |
string |
|
The delimiter to split cells by |
- Returns
-
Array of Array of string
gset(grid, vec, value) → Array of Array of T
Sets value at the given vector in a grid. If the cell doesn't exist, it will be created.
Parameters
Name | Type | Optional | Description |
---|---|---|---|
grid |
Array of Array of T |
|
The grid to set the value in |
vec |
|
The vector to set the value at |
|
value |
T |
|
The value to set |
- Returns
-
Array of Array of T
The modified grid
nrotate(grid[, times]) → Array of Array of T
Rotates a grid 90 degrees clockwise times
times
Parameters
Name | Type | Optional | Description |
---|---|---|---|
grid |
Array of Array of T |
|
The grid to rotate |
times |
number |
Yes |
The number of times to rotate Defaults to |
- Returns
-
Array of Array of T
rotate(grid) → Array of Array of T
Rotates a grid 90 degrees clockwise once (alias for grid_utils.nrotate(grid, 1)
)
Parameter
Name | Type | Optional | Description |
---|---|---|---|
grid |
Array of Array of T |
|
The grid to rotate |
- Returns
-
Array of Array of T
vecswhere(grid, fn) → Array of Vec
Finds Vecs in a grid which satisfy the given function
Parameters
Name | Type | Optional | Description |
---|---|---|---|
grid |
Array of Array of T |
|
The grid to search |
fn |
function(T, Vec, Array of Array of T) |
|
The function to filter with |
- Returns
-
Array of Vec
vecwhere(grid, fn) → Vec
Returns the first vector in a grid (searching top to bottom, left to right)
where the function fn
is truthy for that cell. fn
also receives the
position of each cell and a reference to the grid.
Parameters
Name | Type | Optional | Description |
---|---|---|---|
grid |
Array of Array of T |
|
The grid to search |
fn |
function(T, Vec, Array of Array of T) |
|
The function to search with |
- Returns