Skip to content

Instantly share code, notes, and snippets.

@alanrsoares
Last active October 9, 2017 20:54
Show Gist options
  • Save alanrsoares/b209f4c3d0e48f4fdf6a12aec7447379 to your computer and use it in GitHub Desktop.
Save alanrsoares/b209f4c3d0e48f4fdf6a12aec7447379 to your computer and use it in GitHub Desktop.
JS Bin - Flow-typed Immutable Matrix data structure // source http://jsbin.com/vuyexi
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
"use strict";
var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; continue _function; } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } };
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var Matrix = (function () {
function Matrix(matrix) {
_classCallCheck(this, Matrix);
this._matrix = [];
this._matrix = matrix;
}
_createClass(Matrix, [{
key: "get",
value: function get(y, x) {
return this._matrix[y][x];
}
}, {
key: "set",
value: function set(y, x, val) {
this._matrix[y][x] = val;
return this;
}
}, {
key: "update",
value: function update(y, x, fn) {
return this.set(y, x, fn(this.get(y, x)));
}
}, {
key: "neighbours",
value: function neighbours(y, x) {
var _this = this;
var coords = [[y - 1, x - 1], [y - 1, x], [y - 1, x + 1], [y, x - 1], [y, x + 1], [y + 1, x - 1], [y + 1, x], [y + 1, x + 1]];
return coords.map(function (p) {
return _this.get.apply(_this, _toConsumableArray(p));
});
}
}, {
key: "toJs",
value: function toJs() {
return this._matrix;
}
}], [{
key: "create",
value: function create(height, width, fill) {
var vec = Array(height).fill().map(function () {
return Array(width).fill(fill);
});
return new Matrix(vec);
}
}]);
return Matrix;
})();
var Grid = (function (_Matrix) {
_inherits(Grid, _Matrix);
function Grid(size) {
_classCallCheck(this, Grid);
_get(Object.getPrototypeOf(Grid.prototype), "constructor", this).call(this, Matrix.create(size, size, false).toJs());
}
return Grid;
})(Matrix);
console.table(Matrix.create(5, 5, 1).set(1, 1, 0).toJs());
console.table(new Grid(3).set(1, 1, true).toJs());
</script>
<script id="jsbin-source-javascript" type="text/javascript">type Vector2D<T> = Array<Array<T>>
class Matrix<T> {
_matrix: Vector2D<T> = []
constructor(matrix: Vector2D<T>) {
this._matrix = matrix
}
get(y: number, x: number): T {
return this._matrix[y][x]
}
set(y: number, x: number, val: T): Matrix<T> {
this._matrix[y][x] = val
return this
}
update(y: number, x: number, fn: (a: T) => T) {
return this.set(y, x, fn(this.get(y, x)))
}
neighbours(y: number, x: number) {
const coords = [
[y - 1, x - 1], [y - 1, x], [y - 1, x + 1],
[y, x - 1], [y, x + 1],
[y + 1, x - 1], [y + 1, x], [y + 1, x + 1]
]
return coords.map(p => this.get(...p))
}
toJs(): Vector2D<T> {
return this._matrix
}
static create(height: number, width: number, fill: T): Matrix<T> {
const vec = Array(height)
.fill()
.map(() => Array(width).fill(fill))
return new Matrix(vec)
}
}
class Grid extends Matrix<boolean> {
constructor(size) {
super(Matrix.create(size, size, false).toJs())
}
}
console.table(Matrix.create(5, 5, 1).set(1, 1, 0).toJs())
console.table(new Grid(3).set(1, 1, true).toJs())
</script></body>
</html>
"use strict";
var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; continue _function; } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } };
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var Matrix = (function () {
function Matrix(matrix) {
_classCallCheck(this, Matrix);
this._matrix = [];
this._matrix = matrix;
}
_createClass(Matrix, [{
key: "get",
value: function get(y, x) {
return this._matrix[y][x];
}
}, {
key: "set",
value: function set(y, x, val) {
this._matrix[y][x] = val;
return this;
}
}, {
key: "update",
value: function update(y, x, fn) {
return this.set(y, x, fn(this.get(y, x)));
}
}, {
key: "neighbours",
value: function neighbours(y, x) {
var _this = this;
var coords = [[y - 1, x - 1], [y - 1, x], [y - 1, x + 1], [y, x - 1], [y, x + 1], [y + 1, x - 1], [y + 1, x], [y + 1, x + 1]];
return coords.map(function (p) {
return _this.get.apply(_this, _toConsumableArray(p));
});
}
}, {
key: "toJs",
value: function toJs() {
return this._matrix;
}
}], [{
key: "create",
value: function create(height, width, fill) {
var vec = Array(height).fill().map(function () {
return Array(width).fill(fill);
});
return new Matrix(vec);
}
}]);
return Matrix;
})();
var Grid = (function (_Matrix) {
_inherits(Grid, _Matrix);
function Grid(size) {
_classCallCheck(this, Grid);
_get(Object.getPrototypeOf(Grid.prototype), "constructor", this).call(this, Matrix.create(size, size, false).toJs());
}
return Grid;
})(Matrix);
console.table(Matrix.create(5, 5, 1).set(1, 1, 0).toJs());
console.table(new Grid(3).set(1, 1, true).toJs());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment