support for ranges of indices using zero and negative numbers inside index-lists in "item of"

pull/95/head
jmoenig 2021-02-15 17:48:18 +01:00
rodzic 8e83539536
commit ecb25fe473
3 zmienionych plików z 37 dodań i 9 usunięć

Wyświetl plik

@ -5,6 +5,7 @@
* **New Features:**
* new "reshape" primitive for lists
* list operations as dropdown menu of new "length of list" block
* support for ranges of indices using zero and negative numbers inside index-lists in "item of"
* **Notable Changes:**
* 2D lists inside ITEM OF now have the right order of dimensions (rows, columns, planes, etc.)
* changed "length of list" to become a general list operations primitive
@ -30,6 +31,7 @@
* blocks: took out "transpose" from "length" dropdown
* German translation update
* removed "reverse" block from the "frequency distribution analysis" library
* support for ranges of indices using zero and negative numbers inside index-lists in "item of"
### 2021-02-14
* lists: fixed transcription typos in strideTranspose(), thanks, Brian!

Wyświetl plik

@ -13,7 +13,7 @@
<script src="src/objects.js?version=2021-02-11"></script>
<script src="src/gui.js?version=2021-02-04"></script>
<script src="src/paint.js?version=2020-05-17"></script>
<script src="src/lists.js?version=2021-02-14"></script>
<script src="src/lists.js?version=2021-02-15"></script>
<script src="src/byob.js?version=2021-02-13"></script>
<script src="src/tables.js?version=2020-10-06"></script>
<script src="src/sketch.js?version=2020-07-13"></script>

Wyświetl plik

@ -63,7 +63,7 @@ MorphicPreferences, TableDialogMorph, SpriteBubbleMorph, SpeechBubbleMorph,
TableFrameMorph, TableMorph, Variable, isSnapObject, Costume, contains, detect,
ZERO, WHITE*/
modules.lists = '2021-February-14';
modules.lists = '2021-February-15';
var List;
var ListWatcherMorph;
@ -408,24 +408,50 @@ List.prototype.query = function (indices) {
return this.map(e => e);
}
if (indices.rank() === 1) {
return indices.map(i => this.at(i));
return this.rangify(indices).map(i => this.at(i));
}
first = indices.at(1);
if (first instanceof List) {
select = first.isEmpty() ?
this.range(this.length())
: first;
this.range(1, this.length())
: this.rangify(first);
} else {
select = new List([first]);
select = this.rangify(new List([first]));
}
return select.map(i => this.at(i)).map(
e => e instanceof List? e.query(indices.cdr()) : e
);
};
List.prototype.range = function (upTo) {
// private - answer a list of integers from 1 up to the given ceiling
return new List([...Array(upTo)].map((e, i) => i + 1));
List.prototype.rangify = function (indices) {
// private
var result = [],
len = this.length(),
current = 0,
start, end;
indices.itemsArray().forEach(idx => {
idx = +idx;
if (idx > 0) {
result.push(idx);
current = idx;
} else {
end = len + idx;
if (current !== end) {
start = current < end ? current + 1 : current - 1;
this.range(start, end).itemsArray().forEach(
num => result.push(num)
);
}
}
});
return new List(result);
};
List.prototype.range = function (start, end) {
// private - answer a list of integers from start to the given end
return new List([...Array(Math.abs(end - start) + 1)].map((e, i) =>
start < end ? start + i : start - i
));
};
List.prototype.items = function (indices) {