[REBOL] COLLECTing results
From: brett::codeconscious::com at: 24-Jul-2003 15:44
While developing various versions of Map related functions I had cause to
pause and wonder if I could be more orthogonal with the functionality.
As background, the the main benefit of the Map function is collecting
function results in a block. I felt that my shortcut method method of using
a block to specify the mapping function was neat and followed other Rebol
functions. The shortcut method is used by Repeat, Foreach etc to specify the
body block - so could just add collection of the results? Yes!
So here is Collect - the sole purpose of which is to collect the evaluation
of a result. I haven't thorougly tested it so I've no idea how it will go in
memory usage/garbage collection.
>> repeat i 10 collect [i]
== [1 2 3 4 5 6 7 8 9 10]
>> foreach [a b] [1 2 3 4] collect [a + b]
== [3 7]
collect: func [
{Collects block evaluations, use with For, Repeat, etc.}
block [block!]
/init result "Initialise the result series."
/only "Inserts into result using Only refinement."
] [
if empty? block [return block]
compose [
result: any [result make block! 100]
(pick [insert/only insert] found? only) tail result (block)
result
]
]
Regards,
Brett
---
Website: http://www.codeconscious.com
Rebsite: http://www.codeconscious.com/index.r