new RESHAPE primitive

pull/95/head
jmoenig 2021-02-08 08:57:26 +01:00
rodzic f4896e9f69
commit 4a7abe0b7b
5 zmienionych plików z 80 dodań i 8 usunięć

Wyświetl plik

@ -9,6 +9,9 @@
* **Documentation Updates:**
* updated manual (e.g. p.20) with hyper-semantics of ITEM OF, thanks Brian
### 2021-02-06
* lists, objects, threads: new RESHAPE primitive
### 2021-02-06
* simplified private list.range() method
* blocks: changed wordings for list attributes

Wyświetl plik

@ -9,11 +9,11 @@
<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-07"></script>
<script src="src/threads.js?version=2021-02-07"></script>
<script src="src/objects.js?version=2021-01-30"></script>
<script src="src/threads.js?version=2021-02-08"></script>
<script src="src/objects.js?version=2021-02-08"></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-07"></script>
<script src="src/lists.js?version=2021-02-08"></script>
<script src="src/byob.js?version=2020-12-22"></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-07';
modules.lists = '2021-February-08';
var List;
var ListWatcherMorph;
@ -121,6 +121,7 @@ var ListWatcherMorph;
flatten() - answer a concatenated list of columns and atoms
ravel() - answer a flat list of all atoms in all sublists
transpose() - answer a 2D list with rows turned into columns
reshape() - answer a new list formatted to the given dimensions.
query() - answer a part of a list or multidimensionel struct
*/
@ -582,6 +583,60 @@ List.prototype.transpose = function () {
return new List(table);
};
List.prototype.reshape = function (dimensions) {
// answer a new list formatted to fit the given dimensions.
// truncate excess elements, if any.
// pad with (repetitions of) existing elements
var src = this.ravel().itemsArray(),
size = dimensions.isEmpty() ? 0
: dimensions.itemsArray().reduce((a, b) => a * b),
i = 0,
trg;
if (size < src.length) {
trg = src.slice(0, size);
} else {
trg = src.slice();
while (trg.length < size) {
if (i >= src.length) {
i = 0;
}
trg.push(src[i]);
i += 1;
}
}
return new List(trg).folded(dimensions).at(1);
};
List.prototype.folded = function (dimensions) {
// private
var len = dimensions.length(),
trg = this,
i;
if (len == 0) {
return this.map(e => e);
}
for (i = len; i > 0; i -= 1) {
trg = trg.asChunksOf(dimensions.at(i));
}
return trg;
};
List.prototype.asChunksOf = function (size) {
// private
var trg = new List(),
len = this.length(),
sub, i;
for (i = 0; i < len; i += 1) {
if (i % size === 0) {
sub = new List();
trg.add(sub);
}
sub.add(this.at(i + 1));
}
return trg;
};
// List conversion:
List.prototype.asArray = function () {

Wyświetl plik

@ -84,7 +84,7 @@ BlockEditorMorph, BlockDialogMorph, PrototypeHatBlockMorph, BooleanSlotMorph,
localize, TableMorph, TableFrameMorph, normalizeCanvas, VectorPaintEditorMorph,
AlignmentMorph, Process, WorldMap, copyCanvas, useBlurredShadows*/
modules.objects = '2021-February-05';
modules.objects = '2021-February-08';
var SpriteMorph;
var StageMorph;
@ -1391,11 +1391,16 @@ SpriteMorph.prototype.initBlocks = function () {
category: 'lists',
spec: 'append %lists'
},
reportTranspose: {
reportTranspose: { // deprecated
type: 'reporter',
category: 'lists',
spec: 'transpose %l'
},
reportReshape: {
type: 'reporter',
category: 'lists',
spec: 'reshape %l to %mult%n'
},
// HOFs
reportMap: {
@ -2780,6 +2785,7 @@ SpriteMorph.prototype.blockTemplates = function (category) {
blocks.push(block('doForEach'));
blocks.push('-');
blocks.push(block('reportConcatenatedLists'));
blocks.push(block('reportReshape'));
// blocks.push(block('reportTranspose'));
blocks.push('-');
blocks.push(block('doAddToList'));
@ -2957,6 +2963,7 @@ SpriteMorph.prototype.freshPalette = function (category) {
'reportListAttribute',
'reportListIndex',
'reportConcatenatedLists',
'reportReshape',
'reportListContainsItem',
'reportListIsEmpty',
'doForEach',
@ -8940,6 +8947,7 @@ StageMorph.prototype.blockTemplates = function (category) {
blocks.push(block('doForEach'));
blocks.push('-');
blocks.push(block('reportConcatenatedLists'));
blocks.push(block('reportReshape'));
// blocks.push(block('reportTranspose'));
blocks.push('-');
blocks.push(block('doAddToList'));

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-07';
modules.threads = '2021-February-08';
var ThreadManager;
var Process;
@ -1948,13 +1948,19 @@ Process.prototype.reportListItem = function (index, list) {
return list.at(index);
};
// Process - experimental tabular list accessors
// Process - experimental tabular list ops
Process.prototype.reportTranspose = function (list) {
this.assertType(list, 'list');
return list.transpose();
};
Process.prototype.reportReshape = function (list, shape) {
this.assertType(list, 'list');
this.assertType(shape, 'list');
return list.reshape(shape);
};
// Process - other basic list accessors
Process.prototype.reportListAttribute = function (choice, list) {