tweaked List >> query()

pull/95/head
jmoenig 2021-02-04 23:18:25 +01:00
rodzic 4d6bd9a1e8
commit f25aad784d
2 zmienionych plików z 12 dodań i 17 usunięć

Wyświetl plik

@ -8,6 +8,7 @@
### 2021-02-04 ### 2021-02-04
* lists, threads: changed query semantics for table selectors in ITEM OF to rows, columns, planes, etc. * lists, threads: changed query semantics for table selectors in ITEM OF to rows, columns, planes, etc.
* pushed dev version number * pushed dev version number
* lists: tweaked query()
### 2021-02-03 ### 2021-02-03
* new dev version * new dev version

Wyświetl plik

@ -121,6 +121,7 @@ var ListWatcherMorph;
flatten() - answer a concatenated list of columns and atoms flatten() - answer a concatenated list of columns and atoms
ravel() - answer a flat list of all atoms in all sublists ravel() - answer a flat list of all atoms in all sublists
transpose() - answer a 2D list with rows turned into columns transpose() - answer a 2D list with rows turned into columns
query() - answer a part of a list or multidimensionel struct
*/ */
// List instance creation: // List instance creation:
@ -398,16 +399,21 @@ List.prototype.query = function (indices) {
// assumes a 2D argument list where each slot represents // assumes a 2D argument list where each slot represents
// the indices to select from a dimension // the indices to select from a dimension
// e.g. [rows, columns, planes] // e.g. [rows, columns, planes]
var select; var first, select;
if (indices.isEmpty()) { if (indices.isEmpty()) {
return this.map(e => e); return this.map(e => e);
} }
if (indices.quickRank() === 1) { if (indices.rank() === 1) {
return indices.map(i => this.at(i)); return indices.map(i => this.at(i));
} }
select = indices.at(1).isEmpty() ? first = indices.at(1);
this.range(1, this.length()) if (first instanceof List) {
: indices.at(1); select = first.isEmpty() ?
this.range(1, this.length())
: first;
} else {
select = new List([first]);
}
return select.map(i => this.at(i)).map( return select.map(i => this.at(i)).map(
e => e instanceof List? e.query(indices.cdr()) : e e => e instanceof List? e.query(indices.cdr()) : e
); );
@ -493,18 +499,6 @@ List.prototype.rank = function () {
); );
}; };
List.prototype.quickRank = function (quick) {
// answer the number of my dimensions
// 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;
};
List.prototype.shape = function () { List.prototype.shape = function () {
// answer a list of the maximum size for each dimension // answer a list of the maximum size for each dimension
var dim, var dim,