Ladislav Mecir napsal(a):

...snip...
> Another option is to use a closure ( 
> http://www.fm.tul.cz/~ladislav/rebol/closure.r , which is going to be 
> implemented natively in R3) and the implementation may be: (see, what 
> are the closures good for?)
>
>     collect: closure [
>         {Collects values, returning them as a series.}
>         [throw]
>         emit [word!]  "Word used to collect values"
>         block [block!] "Block to evaluate"
>         /into dest [series!] "Where to append results"
>         /only "Insert series values as series"
>         /local ins
>     ] [
>         ; 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]
>         dest: any [:dest []]
>         ; make the function used to collect values.
>         ins: either only [[insert/only]] [[insert]]
>         set emit func [value [any-type!]] compose [
>             (ins) tail :dest get/any 'value
>             get/any 'value
>         ]
>         do bind/copy block emit
>         head :dest
>     ]
>
> ; test:
> probe collect 'x [
>     probe collect 'y [
>         foreach i [1 2] [
>             y negate i
>             x i
>         ]
>     ]
> ]
>
>   
>>  And you could still mess things up, right (by
>>   using dest/dst as a set-word! in the block?
>>   
>>     
> Not at all, don't worry. It does not have anything in common with the 
> contents of the BLOCK argument.
When I think more about it, there is a compelling reason, why to use 
this implementation: it will be the first useful example of a closure. 
Moreover, it will be faster than the other implementations when using 
native CLOSURE implementation. What do you think?

Side note:

Gregg's COLLECT dialect is one of the few dialects enabling us to create 
new dialects. Other examples of such dialect are: COMPOSE, PARSE, or my 
http://www.fm.tul.cz/~ladislav/rebol/build.r


-L

-- 
To unsubscribe from the list, just send an email to 
lists at rebol.com with unsubscribe as the subject.

Reply via email to