new "index of (element) in (list)" primitive

pull/95/head
jmoenig 2020-04-25 18:53:18 +02:00
rodzic 9e57540ce0
commit 69179a8cb8
3 zmienionych plików z 40 dodań i 4 usunięć

Wyświetl plik

@ -62,7 +62,7 @@ CellMorph, ArrowMorph, MenuMorph, snapEquals, localize, isString,
MorphicPreferences, TableDialogMorph, SpriteBubbleMorph, SpeechBubbleMorph,
TableFrameMorph, TableMorph, Variable, isSnapObject, Costume, contains*/
modules.lists = '2020-April-24';
modules.lists = '2020-April-25';
var List;
var ListWatcherMorph;
@ -95,7 +95,8 @@ var ListWatcherMorph;
length() - number of slots
at(index) - element present in specified slot
contains(element) - <bool>
isEmpty(element) - <bool>
isEmpty() - <bool>
indexOf(element) - index of element's first occurrence, 0 if none
conversion:
-----------
@ -253,6 +254,27 @@ List.prototype.isEmpty = function () {
return !this.contents.length;
};
List.prototype.indexOf = function (element) {
var pair = this,
idx = 1,
i, len;
while (pair.isLinked) {
if (snapEquals(pair.first, element)) {
return idx;
}
pair = pair.rest;
idx += 1;
}
// in case I'm arrayed
len = pair.contents.length;
for (i = 0; i < len; i += 1) {
if (snapEquals(pair.contents[i], element)) {
return idx + i;
}
}
return 0;
};
// List table (2D) accessing (for table morph widget):
List.prototype.isTable = function () {

Wyświetl plik

@ -84,7 +84,7 @@ BlockEditorMorph, BlockDialogMorph, PrototypeHatBlockMorph, BooleanSlotMorph,
localize, TableMorph, TableFrameMorph, normalizeCanvas, VectorPaintEditorMorph,
HandleMorph, AlignmentMorph, Process, XML_Element, WorldMap, copyCanvas*/
modules.objects = '2020-April-20';
modules.objects = '2020-April-25';
var SpriteMorph;
var StageMorph;
@ -1282,6 +1282,12 @@ SpriteMorph.prototype.initBlocks = function () {
category: 'lists',
spec: 'is %l empty?'
},
reportListIndex: {
type: 'reporter',
category: 'lists',
spec: 'index of %s in %l',
defaults: [localize('thing')]
},
doAddToList: {
type: 'command',
category: 'lists',
@ -2627,6 +2633,7 @@ SpriteMorph.prototype.blockTemplates = function (category) {
blocks.push(block('reportCDR'));
blocks.push('-');
blocks.push(block('reportListLength'));
blocks.push(block('reportListIndex'));
blocks.push(block('reportListContainsItem'));
blocks.push(block('reportListIsEmpty'));
blocks.push('-');
@ -2814,6 +2821,7 @@ SpriteMorph.prototype.freshPalette = function (category) {
'reportListItem',
'reportCDR',
'reportListLength',
'reportListIndex',
'reportListContainsItem',
'reportListIsEmpty',
'doForEach',
@ -8677,6 +8685,7 @@ StageMorph.prototype.blockTemplates = function (category) {
blocks.push(block('reportCDR'));
blocks.push('-');
blocks.push(block('reportListLength'));
blocks.push(block('reportListIndex'));
blocks.push(block('reportListContainsItem'));
blocks.push(block('reportListIsEmpty'));
blocks.push('-');

Wyświetl plik

@ -61,7 +61,7 @@ StageMorph, SpriteMorph, StagePrompterMorph, Note, modules, isString, copy, Map,
isNil, WatcherMorph, List, ListWatcherMorph, alert, console, TableMorph, Color,
TableFrameMorph, ColorSlotMorph, isSnapObject, newCanvas, Symbol, SVG_Costume*/
modules.threads = '2020-April-24';
modules.threads = '2020-April-25';
var ThreadManager;
var Process;
@ -1834,6 +1834,11 @@ Process.prototype.reportListLength = function (list) {
return list.length();
};
Process.prototype.reportListIndex = function(element, list) {
this.assertType(list, 'list');
return list.indexOf(element);
};
Process.prototype.reportListContainsItem = function (list, element) {
this.assertType(list, 'list');
return list.contains(element);