kopia lustrzana https://github.com/backface/turtlestitch
refactored list.rank() to avoid JS stack overflows
rodzic
b053beb5f9
commit
2bace8aa59
15
src/lists.js
15
src/lists.js
|
@ -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,
|
||||
|
|
Ładowanie…
Reference in New Issue