kopia lustrzana https://github.com/backface/turtlestitch
Experimental “ForEach” primitive (hidden in dev mode)
rodzic
17b6ae839b
commit
ad1fe34d1e
13
objects.js
13
objects.js
|
@ -125,7 +125,7 @@ PrototypeHatBlockMorph*/
|
|||
|
||||
// Global stuff ////////////////////////////////////////////////////////
|
||||
|
||||
modules.objects = '2014-December-03';
|
||||
modules.objects = '2014-December-04';
|
||||
|
||||
var SpriteMorph;
|
||||
var StageMorph;
|
||||
|
@ -1151,6 +1151,13 @@ SpriteMorph.prototype.initBlocks = function () {
|
|||
category: 'lists',
|
||||
spec: 'map %repRing over %l'
|
||||
},
|
||||
doForEach: {
|
||||
dev: true,
|
||||
type: 'command',
|
||||
category: 'lists',
|
||||
spec: 'for %upvar in %l %cs',
|
||||
defaults: [localize('each item')]
|
||||
},
|
||||
|
||||
// Code mapping - experimental
|
||||
doMapCodeOrHeader: { // experimental
|
||||
|
@ -2063,6 +2070,8 @@ SpriteMorph.prototype.blockTemplates = function (category) {
|
|||
blocks.push(txt);
|
||||
blocks.push('-');
|
||||
blocks.push(block('reportMap'));
|
||||
blocks.push('-');
|
||||
blocks.push(block('doForEach'));
|
||||
}
|
||||
|
||||
/////////////////////////////////
|
||||
|
@ -5198,6 +5207,8 @@ StageMorph.prototype.blockTemplates = function (category) {
|
|||
blocks.push(txt);
|
||||
blocks.push('-');
|
||||
blocks.push(block('reportMap'));
|
||||
blocks.push('-');
|
||||
blocks.push(block('doForEach'));
|
||||
}
|
||||
|
||||
/////////////////////////////////
|
||||
|
|
22
threads.js
22
threads.js
|
@ -83,7 +83,7 @@ ArgLabelMorph, localize, XML_Element, hex_sha512*/
|
|||
|
||||
// Global stuff ////////////////////////////////////////////////////////
|
||||
|
||||
modules.threads = '2014-December-03';
|
||||
modules.threads = '2014-December-04';
|
||||
|
||||
var ThreadManager;
|
||||
var Process;
|
||||
|
@ -1679,6 +1679,26 @@ Process.prototype.reportMap = function (reporter, list) {
|
|||
}
|
||||
};
|
||||
|
||||
Process.prototype.doForEach = function (upvar, list, script) {
|
||||
// perform a script for each element of a list, assigning the
|
||||
// current iteration's element to a variable with the name
|
||||
// specified in the "upvar" parameter, so it can be referenced
|
||||
// within the script. Uses the context's - unused - fourth
|
||||
// element as temporary storage for the current list index
|
||||
|
||||
if (isNil(this.context.inputs[3])) {this.context.inputs[3] = 1; }
|
||||
var index = this.context.inputs[3];
|
||||
this.context.outerContext.variables.addVar(upvar);
|
||||
this.context.outerContext.variables.setVar(
|
||||
upvar,
|
||||
list.at(index)
|
||||
);
|
||||
if (index > list.length()) {return; }
|
||||
this.context.inputs[3] += 1;
|
||||
this.pushContext();
|
||||
this.evaluate(script, new List(), true);
|
||||
};
|
||||
|
||||
// Process interpolated primitives
|
||||
|
||||
Process.prototype.doWait = function (secs) {
|
||||
|
|
Ładowanie…
Reference in New Issue