kopia lustrzana https://github.com/backface/turtlestitch
refactored some matrix operations
rodzic
af4662bed1
commit
4d8fe5b083
|
@ -17,6 +17,9 @@
|
|||
* German
|
||||
* Turkish
|
||||
|
||||
### 2021-01-02
|
||||
* lists: refactored some matrix operations
|
||||
|
||||
### 2021-01-30
|
||||
* threads, objects, lists: renamed experimental "rotate" primitive into "transpose"
|
||||
* objects: added "transpose" to palette for testing
|
||||
|
|
|
@ -13,7 +13,7 @@
|
|||
<script src="src/objects.js?version=2021-01-30"></script>
|
||||
<script src="src/gui.js?version=2021-01-21"></script>
|
||||
<script src="src/paint.js?version=2020-05-17"></script>
|
||||
<script src="src/lists.js?version=2021-01-30"></script>
|
||||
<script src="src/lists.js?version=2021-02-01"></script>
|
||||
<script src="src/byob.js?version=2020-12-22"></script>
|
||||
<script src="src/tables.js?version=2020-10-06"></script>
|
||||
<script src="src/sketch.js?version=2020-07-13"></script>
|
||||
|
|
107
src/lists.js
107
src/lists.js
|
@ -63,7 +63,7 @@ MorphicPreferences, TableDialogMorph, SpriteBubbleMorph, SpeechBubbleMorph,
|
|||
TableFrameMorph, TableMorph, Variable, isSnapObject, Costume, contains, detect,
|
||||
ZERO, WHITE*/
|
||||
|
||||
modules.lists = '2021-January-30';
|
||||
modules.lists = '2021-February-01';
|
||||
|
||||
var List;
|
||||
var ListWatcherMorph;
|
||||
|
@ -210,12 +210,85 @@ List.prototype.clear = function () {
|
|||
this.changed();
|
||||
};
|
||||
|
||||
List.prototype.map = function(callback) {
|
||||
// List utilities
|
||||
|
||||
List.prototype.map = function (callback) {
|
||||
return new List(
|
||||
this.itemsArray().map(callback)
|
||||
);
|
||||
};
|
||||
|
||||
// List matrix operations and utilities - very experimental, not yet in use
|
||||
|
||||
List.prototype.deepMap = function (callback) {
|
||||
return this.map(item => item instanceof List ?
|
||||
item.deepMap(callback)
|
||||
: callback(item));
|
||||
};
|
||||
|
||||
List.prototype.size = function () {
|
||||
// count the number of atomic elements
|
||||
var count = 0;
|
||||
this.deepMap(() => count += 1);
|
||||
return count;
|
||||
};
|
||||
|
||||
List.prototype.rank = function () {
|
||||
var rank = 0,
|
||||
cur = this;
|
||||
while (cur instanceof List) {
|
||||
rank += 1;
|
||||
cur = cur.at(1);
|
||||
}
|
||||
return rank;
|
||||
};
|
||||
|
||||
List.prototype.shape = function () {
|
||||
var shp = [],
|
||||
cur = this;
|
||||
while (cur instanceof List) {
|
||||
shp.push(this.length());
|
||||
cur = cur.at(1);
|
||||
}
|
||||
return new List(shp);
|
||||
};
|
||||
|
||||
List.prototype.width = function () {
|
||||
// answer the maximum length of my direct sub-lists, if any
|
||||
var i, item,
|
||||
width = 0,
|
||||
len = this.length();
|
||||
for (i = 1; i <= len; i += 1) {
|
||||
item = this.at(i);
|
||||
width = Math.max(width, item instanceof List ? item.length() : 0);
|
||||
}
|
||||
return width;
|
||||
};
|
||||
|
||||
List.prototype.transpose = function () {
|
||||
// answer a 2D list where each item has turned into a row,
|
||||
// convert atomic items into lists,
|
||||
// fill ragged columns with atomic values, if any, or empty cells
|
||||
// not yet implemented: higher dimensions exceeding 2D
|
||||
var col, src, i,
|
||||
width = Math.max(this.width(), 1),
|
||||
table = [];
|
||||
|
||||
// convert atomic items into rows
|
||||
src = this.map(row =>
|
||||
row instanceof List ? row : new List(new Array(width).fill(row))
|
||||
);
|
||||
|
||||
// define the mapper function
|
||||
col = (tab, c) => tab.map(row => row.at(c));
|
||||
|
||||
// create the transform
|
||||
for (i = 1; i <= width; i += 1) {
|
||||
table.push(col(src, i));
|
||||
}
|
||||
return new List(table);
|
||||
};
|
||||
|
||||
// List getters (all hybrid):
|
||||
|
||||
List.prototype.length = function () {
|
||||
|
@ -376,36 +449,6 @@ List.prototype.version = function (startRow, rows, startCol, cols) {
|
|||
|
||||
// List conversion:
|
||||
|
||||
List.prototype.transpose = function () {
|
||||
// answer a 2D list where each item has turned into a row,
|
||||
// convert atomic items into lists,
|
||||
// fill ragged columns with atomic values, if any, or empty cells
|
||||
var col, src, i, item,
|
||||
width = 1,
|
||||
len = this.length(),
|
||||
table = [];
|
||||
|
||||
// determine the maximum sublist length
|
||||
for (i = 1; i <= len; i += 1) {
|
||||
item = this.at(i);
|
||||
width = Math.max(width, item instanceof List ? item.length() : 0);
|
||||
}
|
||||
|
||||
// convert atomic items into rows
|
||||
src = this.map(row =>
|
||||
row instanceof List ? row : new List(new Array(width).fill(row))
|
||||
);
|
||||
|
||||
// define the mapper function
|
||||
col = (tab, c) => tab.map(row => row.at(c));
|
||||
|
||||
// create the transform
|
||||
for (i = 1; i <= width; i += 1) {
|
||||
table.push(col(src, i));
|
||||
}
|
||||
return new List(table);
|
||||
};
|
||||
|
||||
List.prototype.asArray = function () {
|
||||
// for use in the evaluator
|
||||
this.becomeArray();
|
||||
|
|
Ładowanie…
Reference in New Issue