On 5/11/06, Gregg Irwin <[EMAIL PROTECTED]> wrote:
>
> Ladislav,
>
> Thanks for the comments and catching the 'yield reference I missed in
> the doc string.
>
> collect: func [
> [throw]
> {Collects values, returning them as a block.}
> 'emit "Word used to collect values"
> block [any-block!] "Block to evaluate"
> /into dest [series!] "Where to append results"
> /only "Insert series values as series"
> ] [
> ; Create a new context containing just one word (the EMIT
> ; argument) and set the 'emit word to refer to the new context
> ; word. Note the care taken to suppress possible conflicts and
> ; undesired evaluations.
> emit: reduce [emit]
> emit: first use emit reduce [emit]
> use [dst] copy/deep [
> ; copy/deep lets us use just as the fallback value [] here.
> dst: any [:dest []]
> ; The only difference in the function bodies is insert[/only]=
.
> set emit func [value [any-type!]] either only [
> [insert/only tail :dst get/any 'value get/any 'value]
> ][
> [insert tail :dst get/any 'value get/any 'value]
> ]
> do bind/copy block emit
> head :dst
> ]
> ]
>
>
> GS> Hmm, but this is a different matter, being able to address
> GS> different output blocks at the same time. So we need to figure out
> GS> if it's more useful to be able to do the above, or to be able to
> GS> do:
>
> GS> do-something: does [
> GS> repeat i 10 [emit i]
> GS> ]
>
> GS> collect [do-something]
>
> I see the value in both. Some things, like CGI scripts, that build up
> a single output buffer benefit from the global approach, but it's not
> nearly as convenient for use in other functions that need to build up
> a local result and return it, which was my main target.
>
> I also like the context you get by specifying the "word" you want to
> collect, rather than saying 'collect one place, but knowing that 'emit,
> used somewhere else, applies to it. Localized thinking I guess.
>
Local use is more important IHMO. As you said, its for this
fn: func [series /local res] [
res: copy []
foreach val series [append res val]
res
]
which occurs very often.
The global version can be done with a few explicit lines, defining
'emit by hand.
After all, 'collect is a shortcut to save a few lines each time, it
does no magic things.
> -- Gregg
>
> --
> To unsubscribe from the list, just send an email to
> lists at rebol.com with unsubscribe as the subject.
>
>
--=20
-Volker
"Any problem in computer science can be solved with another layer of
indirection. But that usually will create another problem." David
Wheeler
--
To unsubscribe from the list, just send an email to
lists at rebol.com with unsubscribe as the subject.