70 lines
1.4 KiB
JavaScript
70 lines
1.4 KiB
JavaScript
import * as Vector from Geometry.Vector;
|
|
|
|
return {
|
|
at: at,
|
|
asAFunction: asAFunction,
|
|
column: column,
|
|
generate: generate,
|
|
iter: iter,
|
|
make: make,
|
|
map: map,
|
|
row: row,
|
|
set: set,
|
|
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++) {
|
|
result[row].push(f(Vector.make(row, column)));
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
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});}
|
|
);
|
|
}
|
|
|
|
function iter(grid, f) {
|
|
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);}
|
|
)});
|
|
}
|
|
|
|
function square(size, value) {
|
|
var generator = value instanceof Function ? value : function() {return value;};
|
|
return map(make(size, size), function(_, cell) {return generator(cell);});
|
|
}
|
|
|
|
function at(grid, vector) {
|
|
return grid[vector.row][vector.column];
|
|
}
|
|
|
|
function asAFunction(grid) {
|
|
return function (vector) {return at(grid, vector);};
|
|
}
|
|
|
|
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];});
|
|
}
|