From 9ee7c9287b99c6ee988f4dc366d92a6edf2f19ed Mon Sep 17 00:00:00 2001 From: jmoenig Date: Tue, 26 Jan 2021 15:56:28 +0100 Subject: [PATCH] experimental "column _ of _" reporter relabelling option for "item _ of _" --- HISTORY.md | 2 ++ snap.html | 2 +- src/objects.js | 14 ++++++++++++-- src/threads.js | 29 +++++++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 3 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index e8e570c0..8768f254 100755 --- a/HISTORY.md +++ b/HISTORY.md @@ -5,6 +5,7 @@ * **Notable Changes:** * hyperized image attribute reporter primitive (monadic) * when constructing a costume from a pixel list handle single values as greyscale + * experimental "column _ of _" reporter relabelling option for "item _ of _" * **Notable Fixes:** * fixed a glitch in the animation library's "sine in-out" easing function * fixed a postMessage glitch in the API, thanks, Bernat! @@ -13,6 +14,7 @@ ### 2021-01-26 * threads: handle single values as greyscale when constructing a costume from a pixel list +* threads, objects experimental "column _ of _" reporter relabelling option for "item _ of _" ### 2021-01-25 * threads: hyperized image attribute reporter primitive (monadic) diff --git a/snap.html b/snap.html index af4a78a4..8135a6d2 100755 --- a/snap.html +++ b/snap.html @@ -10,7 +10,7 @@ - + diff --git a/src/objects.js b/src/objects.js index ecfa9856..4c747d59 100644 --- a/src/objects.js +++ b/src/objects.js @@ -84,7 +84,7 @@ BlockEditorMorph, BlockDialogMorph, PrototypeHatBlockMorph, BooleanSlotMorph, localize, TableMorph, TableFrameMorph, normalizeCanvas, VectorPaintEditorMorph, AlignmentMorph, Process, WorldMap, copyCanvas, useBlurredShadows*/ -modules.objects = '2021-January-05'; +modules.objects = '2021-January-26'; var SpriteMorph; var StageMorph; @@ -1318,6 +1318,12 @@ SpriteMorph.prototype.initBlocks = function () { spec: 'item %idx of %l', defaults: [1] }, + reportTableColumn: { + type: 'reporter', + category: 'lists', + spec: 'column %idx of %l', + defaults: [1] + }, reportCDR: { type: 'reporter', category: 'lists', @@ -1710,7 +1716,11 @@ SpriteMorph.prototype.blockAlternatives = { doShowVar: ['doHideVar'], doHideVar: ['doShowVar'], - // lists - HOFs + // lists + reportListItem: ['reportTableColumn'], + reportTableColumn: ['reportListItem'], + + // HOFs reportMap: ['reportKeep', 'reportFindFirst'], reportKeep: ['reportFindFirst', 'reportMap'], reportFindFirst: ['reportKeep', 'reportMap'], diff --git a/src/threads.js b/src/threads.js index ba47d159..8784a5d8 100644 --- a/src/threads.js +++ b/src/threads.js @@ -2008,6 +2008,35 @@ Process.prototype.reportItems = function (indices, list) { // Process - other basic list accessors +Process.prototype.reportTableColumn = function (index, list) { + // experimental and probably controversial as a primitive, + // because it's so nice and easy to write in Snap! + var col; + + function columns() { + return Math.max(...list.itemsArray().map(row => + row instanceof List ? row.length() : 0) + ); + } + + if (!this.isMatrix(list)) { + throw new Error( + 'expecting ' + 'table' + ' but getting ' + this.reportTypeOf(list) + ); + } + if (index === '') { + return new List(new Array(list.length())); + } + if (this.inputOption(index) === 'any') { + col = this.reportBasicRandom(1, columns()); + return list.map(row => row.at(col)); + } + if (this.inputOption(index) === 'last') { + return list.map(row => row.at(columns())); + } + return list.map(row => row.at(index)); +}; + Process.prototype.reportListLength = function (list) { this.assertType(list, 'list'); return list.length();