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.
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 [...]
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]]
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?
Are there other REBOL funcs that work similarly?
Would we want to create shortcuts, like FUNC/HAS/DOES?
Thanks for posting Volker, the more we think about this, the better it
will eventually be.
-- Gregg
--
To unsubscribe from the list, just send an email to
lists at rebol.com with unsubscribe as the subject.