2022-08-10 21:12:39 +02:00
|
|
|
import * as Vector from Geometry.Vector;
|
|
|
|
|
2022-08-05 19:40:04 +02:00
|
|
|
return {
|
2022-09-01 21:51:48 +02:00
|
|
|
make: make,
|
2022-08-10 21:12:39 +02:00
|
|
|
at: at,
|
2022-09-01 21:51:48 +02:00
|
|
|
asAFunction: asAFunction,
|
2022-08-10 21:12:39 +02:00
|
|
|
column: column,
|
2022-08-05 19:40:04 +02:00
|
|
|
generate: generate,
|
|
|
|
iter: iter,
|
2022-08-10 21:12:39 +02:00
|
|
|
row: row,
|
|
|
|
set: set,
|
2022-08-05 19:40:04 +02:00
|
|
|
square: square
|
|
|
|
};
|
|
|
|
|
|
|
|
function generate(width, height, f) {
|
|
|
|
var result = [];
|
|
|
|
for(var row = 0; row < height; row++) {
|
|
|
|
result[row] = [];;
|
|
|
|
for(var column = 0; column < width; column++) {
|
2022-08-10 21:12:39 +02:00
|
|
|
result[row].push(f(Vector.make(row, column)));
|
2022-08-05 19:40:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-09-01 21:51:48 +02:00
|
|
|
function make(width, height) {
|
|
|
|
if(width instanceof Object && width.row != undefined) {
|
|
|
|
height = width.row;
|
|
|
|
width = width.column;
|
|
|
|
}
|
|
|
|
return Array.from({length: height}).map(
|
|
|
|
function() {return Array.from({length: width});}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-08-05 19:40:04 +02:00
|
|
|
function iter(grid, f) {
|
2022-09-01 21:51:48 +02:00
|
|
|
map(grid, f);
|
|
|
|
}
|
|
|
|
|
|
|
|
function map(grid, f) {
|
|
|
|
return grid.map(function(rowArray, row) {return rowArray.map(
|
|
|
|
function(value, column) {return f(value, Vector.make(row, column), grid);}
|
|
|
|
)});
|
2022-08-05 19:40:04 +02:00
|
|
|
}
|
|
|
|
|
2022-08-10 21:12:39 +02:00
|
|
|
function square(size, value) {
|
2022-09-01 21:51:48 +02:00
|
|
|
var generator = value instanceof Function ? value : function() {return value;};
|
|
|
|
return map(make(size, size), function(_, cell) {return generator(cell);});
|
2022-08-10 21:12:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function at(grid, vector) {
|
|
|
|
return grid[vector.row][vector.column];
|
|
|
|
}
|
|
|
|
|
2022-09-01 21:51:48 +02:00
|
|
|
function asAFunction(grid) {
|
|
|
|
return function (vector) {return at(grid, vector);};
|
|
|
|
}
|
|
|
|
|
2022-08-10 21:12:39 +02:00
|
|
|
function set(grid, vector, value) {
|
|
|
|
return grid[vector.row][vector.column] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
function row(grid, n) {
|
|
|
|
return grid[n];
|
|
|
|
}
|
|
|
|
|
|
|
|
function column(grid, n) {
|
|
|
|
return grid.map(function(row) {return row[n];});
|
2022-08-05 19:40:04 +02:00
|
|
|
}
|