Script Library: 1213 scripts
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

Discussion posts for script collect.r

1
greggirwin
10-Jan-2006 21:34
I've had this function for quite a while, and various forms of it 
to boot. There are versions that don't use a set-word! syntax, versions 
that use an internal function as the replacement code in the block, 
and more. This (set-word!) is the syntax I've found I prefer.


The basic idea is simple, anywhere in the block the WORD parameter 
appears as a set-word!, the set-word! will be changed to a bit of 
code that "collects" the value assigned there. It's proven quite 
flexible and easy to use for me, though I was initially concerned 
about overriding the meaning of set-words this way. I think the semantic 
meaning maps well to its use here, as long as you're aware going 
in that it's a dialected function--a very light dialect to be sure, 
but a dialect none-the-less.

I'm sure there are other solutions, but I'm very happy not writing:

    result: copy []
    foreach ...
        append result value
    result


all over the place any more. I think it also makes the code much 
clearer, because it says you're collecting values, and whereever 
you see the target word, you know it's being collected.


I'd like to see something like this included in REBOL, but it doesn't 
have to be this one. The original collector idea came from Brett 
Handley, then Romano Paolo Tenca added his touch to it, and I put 
quite a different spin on it after that.