refactored list.rank() to avoid JS stack overflows

pull/95/head
jmoenig 2021-02-08 18:37:00 +01:00
rodzic b053beb5f9
commit 2bace8aa59
1 zmienionych plików z 12 dodań i 3 usunięć

Wyświetl plik

@ -496,11 +496,20 @@ List.prototype.ravel = function () {
List.prototype.rank = function () {
// answer the number of my dimensions
// traverse the whole structure for irregularly shaped nested lists
return 1 + Math.max(...this.itemsArray().map(item =>
item instanceof List ? item.rank() : 0)
);
var rank = 1,
len = this.length(),
item, i;
for (i = 1; i <= len; i += 1) {
item = this.at(i);
if (item instanceof List) {
rank = Math.max(rank, 1 + item.rank());
}
}
return rank;
};
List.prototype.shape = function () {
// answer a list of the maximum size for each dimension
var dim,