map-each
can be used to evaluate some code for every member in a collection, and aggregate the results of the evaluation in a block:
>> values: map-each x [1 2] [
print ["Doing mapping for" x]
x * 10
]
Doing mapping for 1
Doing mapping for 2
== [10 20]
I was building a block of blocks in this way. But I forgot that since blocks aren't evaluated by default, the x
would be left as-is and not get the value I wanted:
>> blocks: map-each x [1 2] [
print ["Doing mapping for" x]
[x * 10]
]
Doing mapping for 1
Doing mapping for 2
== [[x * 10] [x * 10]]
No surprise there. After the evaluation x
has no value--much less the ability to take on many values:
>> probe x
** Script error: x has no value
So it's too late, the evaluation must be done with a REDUCE or COMPOSE inside the body of the map-each. But...
>> reduce first blocks
== [20]
>> reduce second blocks
== [20]
The evaluations of items in the result block don't throw an error, but behave as if x
had the value of the last iteration.
How is it doing this? Should it be doing this?