refactored hyper list access

pull/95/head
jmoenig 2021-02-02 17:33:38 +01:00
rodzic 1a27241f94
commit 2613515d07
4 zmienionych plików z 69 dodań i 69 usunięć

Wyświetl plik

@ -21,6 +21,7 @@
### 2021-02-02
* lists: added a few internal - as of now unused - matrix operations
* lists, threads: refactored hyper list access
### 2021-02-01
* lists: refactored some matrix operations

Wyświetl plik

@ -9,7 +9,7 @@
<script src="src/symbols.js?version=2020-10-07"></script>
<script src="src/widgets.js?version=2021-01-05"></script>
<script src="src/blocks.js?version=2021-02-01"></script>
<script src="src/threads.js?version=2021-02-01"></script>
<script src="src/threads.js?version=2021-02-02"></script>
<script src="src/objects.js?version=2021-01-30"></script>
<script src="src/gui.js?version=2021-02-01"></script>
<script src="src/paint.js?version=2020-05-17"></script>

Wyświetl plik

@ -392,7 +392,57 @@ List.prototype.version = function (startRow, rows, startCol, cols) {
return v;
};
// List matrix operations and utilities - very experimental, not yet in use
// List matrix operations and utilities - very experimental
List.prototype.items = function (indices) {
// This. This is it. The pinnacle of my programmer's life.
// After days of roaming about my house and garden,
// of taking showers and rummaging through the fridge,
// of strumming the charango and the five ukuleles
// sitting next to my laptop on my desk,
// and of letting my mind wander far and wide,
// to come up with this design, always thinking
// "What would Brian do?".
// And look, Ma, it's turned out all beautiful! -jens
return makeSelector(
this.rank(),
indices.cdr(),
makeLeafSelector(indices.at(1))
)(this);
function makeSelector(rank, indices, next) {
if (rank === 1) {
return next;
}
return makeSelector(
rank - 1,
indices.cdr(),
makeBranch(
indices.at(1) || new List(),
next
)
);
}
function makeBranch(indices, next) {
return function(data) {
if (indices.isEmpty()) {
return data.map(item => next(item));
}
return indices.map(idx => next(data.at(idx)));
};
}
function makeLeafSelector(indices) {
return function (data) {
if (indices.isEmpty()) {
return data.map(item => item);
}
return indices.map(idx => data.at(idx));
};
}
};
List.prototype.size = function () {
// count the number of all atomic elements
@ -408,8 +458,19 @@ List.prototype.ravel = function () {
return new List(all);
};
List.prototype.rank = function () {
List.prototype.rank = function (quick) {
// answer the number of my dimensions
if (quick) { // only look at the first elements of each dimension
var rank = 0,
cur = this;
while (cur instanceof List) {
rank += 1;
cur = cur.at(1);
}
return rank;
}
// traverse the whole structure for irregularly shaped nested lists
return 1 + Math.max(...this.itemsArray().map(item =>
item instanceof List ? item.rank() : 0)
);

Wyświetl plik

@ -61,7 +61,7 @@ StageMorph, SpriteMorph, StagePrompterMorph, Note, modules, isString, copy, Map,
isNil, WatcherMorph, List, ListWatcherMorph, alert, console, TableMorph, BLACK,
TableFrameMorph, ColorSlotMorph, isSnapObject, newCanvas, Symbol, SVG_Costume*/
modules.threads = '2021-February-01';
modules.threads = '2021-February-02';
var ThreadManager;
var Process;
@ -1932,7 +1932,6 @@ Process.prototype.shadowListAttribute = function (list) {
// Process accessing list elements - hyper dyadic
Process.prototype.reportListItem = function (index, list) {
var rank;
this.assertType(list, 'list');
if (index === '') {
return '';
@ -1943,69 +1942,18 @@ Process.prototype.reportListItem = function (index, list) {
if (this.inputOption(index) === 'last') {
return list.at(list.length());
}
rank = this.rank(index);
if (rank > 0 && this.enableHyperOps) {
if (rank === 1) {
if (index instanceof List && this.enableHyperOps) {
if (index.rank(true) === 1) { // quick - only look at first element
if (index.isEmpty()) {
return list.map(item => item);
}
return index.map(idx => list.at(idx));
}
return this.reportItems(index, list);
return list.items(index);
}
return list.at(index);
};
Process.prototype.reportItems = function (indices, list) {
// This. This is it. The pinnacle of my programmer's life.
// After days of roaming about my house and garden,
// of taking showers and rummaging through the fridge,
// of strumming the charango and the five ukuleles
// sitting next to my laptop on my desk,
// and of letting my mind wander far and wide,
// to come up with this design, always thinking
// "What would Brian do?".
// And look, Ma, it's turned out all beautiful! -jens
return makeSelector(
this.rank(list),
indices.cdr(),
makeLeafSelector(indices.at(1))
)(list);
function makeSelector(rank, indices, next) {
if (rank === 1) {
return next;
}
return makeSelector(
rank - 1,
indices.cdr(),
makeBranch(
indices.at(1) || new List(),
next
)
);
}
function makeBranch(indices, next) {
return function(data) {
if (indices.isEmpty()) {
return data.map(item => next(item));
}
return indices.map(idx => next(data.at(idx)));
};
}
function makeLeafSelector(indices) {
return function (data) {
if (indices.isEmpty()) {
return data.map(item => item);
}
return indices.map(idx => data.at(idx));
};
}
};
// Process - experimental tabular list accessors
Process.prototype.reportTranspose = function (list) {
@ -3737,16 +3685,6 @@ Process.prototype.isMatrix = function (data) {
return data instanceof List && data.at(1) instanceof List;
};
Process.prototype.rank = function(data) {
var rank = 0,
cur = data;
while (cur instanceof List) {
rank += 1;
cur = cur.at(1);
}
return rank;
};
// Process math primtives - arithmetic
Process.prototype.reportSum = function (a, b) {