On 5/13/06, Gregg Irwin <[EMAIL PROTECTED]> wrote:
>
> Hi Volker,
>
> VN> How about this?
> VN> collector: func [args [block!] init [series!] code [block!]] [
> VN> args: union args [/local out emit]
> VN> func args compose [
> VN> out: copy (reduce [init])
> VN> emit: func [val] [
> VN> repend out val
> VN> ]
> VN> (code)
> VN> out
> VN> ]
> VN> ]
> VN> "explicit"
> VN> double: collector [series] [] [
> VN> foreach num series [
> VN> emit 2 * num
> VN> ]
> VN> ]
> VN> probe double [1 2 3 4]
> VN> "inline"
> VN> probe do collector [] [] [foreach num [1 2 3 4] [emit 2 * num]]
>
> IIRC that's close to what Brett and Romano did when this topic came up
> initially (I believe it was Brett who posted the first
> implementation). Theirs, I think, returned a block you could use as a
> function body, or evaluate.
>
> Things I like about it:
>
> By creating a COLLECTOR func factory, you make it clear that you're
> creating something designed to collect values.
>
> "emit <val>" reads well. I haven't looked at the new one long enough
> to lose my taste for the set-word! syntax in my dialected version.
> With the one we've been discussing, you could always say:
>
> collect emit [...]
>
> but that doesn't read well, because you have two active words
> (verbs) together, and it's nicer if the word arg is a noun,
> describing what it is you want to collect.
I dislike the explicit word and prefer 'emit and 'out hardwired.
'collect is a very typical job, and emitting and having an output-series is=
too.
Thats why i like pack of shortcuts.
Yesterday i wrote this code
add-checksums: collector [files snap-list] [] [
foreach file files [
emit [file snap-list/:file]
]
]
The explicit version looked like
add-checksums: func [files snap-list] [
out: copy[]
foreach file files [
repend out [file snap-list/:file]
]
out
]
I think this short things is where it really shines.
In more complex cases coding without 'collect does not bother me.
Although i can have nested names by
add-checksums: collector [files snap-list] [
outer-out: out outer-emit: :emit
foreach file files [
outer-emit [file snap-list/:file]
]
out
]
if i really want that.
Your case is different, because you not only want some calback, but
also a different syntax: this set-words, and following the 'for*
style.
>
> Things I don't care for:
>
> The fact that it returns a function means it's not as accessible to
> new users, and not as convenient as a direct call (e.g. DO
> collector). It also isn't an active name--one that implies
> action--though that's not a killer issue. That is, I like this:
>
> COLLECT [...]
>
> better than
>
> DO COLLECTOR [...]
>
Agreed. Could be a shortcut around a shortcut :)
>
> Having the args and init blocks means more work, and remembering
> what they mean (e.g. the INIT block looks like an empty
> "placeholder" block to me in this line, like an empty locals block
> for FUNCTION:
>
> probe do collector [] [] [foreach num [1 2 3 4] [emit 2 * num]]
>
Agreed. Maybe it should be hardwired instead. I thought giving a
template is a good idea, mainly to allow a string for output too. I
guess i drop that arg.
>
> You have to know about the internal "emit" word, or add it as an
> arg, to avoid conflict. Have to watch not to conflict with OUT as
> well. Hmmm, the other one has the same problem with 'dst doesn't it?
> Ladislav, is there a reason you don't reuse 'dest, rather than
> creating 'dst? And you could still mess things up, right (by
> using dest/dst as a set-word! in the block?
>
You have to know about the existence of 'collect too. ;)
Or the special meaning of /local.
There are some things which are not hardwired, but a very strong convention=
.
'emit and 'out are that for me.
>
> Are there other REBOL funcs that work similarly?
>
Not out of my head. We have no collectors.
I think that is why we are writing 'collect now,
its the first of its kind. :)
> Would we want to create shortcuts, like FUNC/HAS/DOES?
I dont use them. I stick to "func[]" instead if "does".
Most of the time i add an argument later and have to switch to 'func anyway=
.
Or the other way around, i remove one and dont like to change to 'does now=
.
>
> Thanks for posting Volker, the more we think about this, the better it
> will eventually be.
Yup, that was the post-reason. Its surely not the final version.
BTW one thing to keep in mind: closures.
Which are nice for async things like beer.
Could look like
callback: collector[val] out: copy [][
emit ['got val]
callback: collector[val] out [
emit ['got val 'too]
]
]
[call 'callback a few times from 'awake]
if msg-complete[
res: out]
]
>
> -- 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.