From 6ee0d05968d7b282208e1f09a697542428695f66 Mon Sep 17 00:00:00 2001 From: jmoenig Date: Sat, 27 Apr 2019 08:59:12 +0200 Subject: [PATCH] new "numbers" constructor primitive in List category --- HISTORY.md | 3 ++- src/objects.js | 11 +++++++++++ src/threads.js | 31 +++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/HISTORY.md b/HISTORY.md index eac42012..adc9865e 100755 --- a/HISTORY.md +++ b/HISTORY.md @@ -25,7 +25,7 @@ * new "get graphic effect" reporter * new "get pen attribute" reporter * new "write" command in pen category (used to be "label" in tools) - * new "is empty", "map","keep", "combine" and "for each" primitives in list category + * new "numbers", "is empty", "map","keep", "combine" and "for each" primitives in list category * new "for" loop and "if then else" reporter primitives in the Control category * added "neg", "lg" (log2) and "2^" selectors to monadic function reporter in Operators * added "^" reporter (power of) in the Operators category @@ -73,6 +73,7 @@ ### 2019-04-26 * Lists, Threads, Objects: new "is empty" predicate primitive in List category +* Threads, Objects: new "numbers" constructor primitive in List category ### 2019-04-26 * updated Catalan translation (for new HOF prims) diff --git a/src/objects.js b/src/objects.js index c636dbb3..18f63937 100644 --- a/src/objects.js +++ b/src/objects.js @@ -1280,6 +1280,14 @@ SpriteMorph.prototype.initBlocks = function () { defaults: [1, null, localize('thing')] }, + // numbers - linked + reportNumbers: { + type: 'reporter', + category: 'lists', + spec: 'numbers from %n to %n', + defaults: [1, 10] + }, + // HOFs reportMap: { type: 'reporter', @@ -2429,6 +2437,7 @@ SpriteMorph.prototype.blockTemplates = function (category) { blocks.push('='); blocks.push(block('reportNewList')); + blocks.push(block('reportNumbers')); blocks.push('-'); blocks.push(block('reportCONS')); blocks.push(block('reportListItem')); @@ -2617,6 +2626,7 @@ SpriteMorph.prototype.freshPalette = function (category) { [ 'doDeclareVariables', 'reportNewList', + 'reportNumbers', 'reportCONS', 'reportListItem', 'reportCDR', @@ -7985,6 +7995,7 @@ StageMorph.prototype.blockTemplates = function (category) { blocks.push(block('doDeclareVariables')); blocks.push('='); blocks.push(block('reportNewList')); + blocks.push(block('reportNumbers')); blocks.push('-'); blocks.push(block('reportCONS')); blocks.push(block('reportListItem')); diff --git a/src/threads.js b/src/threads.js index d61293b7..f9663429 100644 --- a/src/threads.js +++ b/src/threads.js @@ -1802,6 +1802,37 @@ Process.prototype.doShowTable = function (list) { new TableDialogMorph(list).popUp(this.blockReceiver().world()); }; +// Process interpolated non-HOF list primitives + +Process.prototype.reportNumbers = function (start, end) { + // answer a new linked list containing an linearly ascending progression + // of integers beginning at start to end. + // this is interpolated so it can handle big ranges of numbers + // without blocking the UI + + var dta; + this.assertType(start, 'number'); + this.assertType(end, 'number'); + if (this.context.aggregation === null) { + this.context.aggregation = { + target : new List(), + end : null, + idx : start + }; + this.context.aggregation.target.isLinked = true; + this.context.aggregation.end = this.context.aggregation.target; + } + dta = this.context.aggregation; + if (dta.idx > end) { + this.returnValueToParentContext(dta.target.cdr()); + return; + } + dta.end.rest = dta.target.cons(dta.idx); + dta.end = dta.end.rest; + dta.idx += 1; + this.pushContext(); +}; + // Process conditionals primitives Process.prototype.doIf = function () {