[REBOL] Re: Maplist?
From: ric:getorbital at: 8-Sep-2001 17:37
Let me throw my variation in. I found I often used similar anonymous
functions in map-like situations, so I made a variation that uses a block
representing the body of the function instead:
every: func [
'param [word! block!]
list [series!]
body [block!]
/local result
][
result: copy []
either word? param [
forall list [
set param first list
append result do body
]
][
while [not tail? list][
foreach item param [
set :item first list
list: next list
]
append result do body
]
]
result
]
for example,
>> every [a b] [1 2 3 4 5 6] [a + b]
== [3 7 11]
Similarly, I have 'keep:
keep: func [
'param [word! block!]
list [series!]
body [block!]
/only filter [block!]
/local result
][
result: copy []
either word? param [
if only [make error! "Only works on param of type block!"]
forall list [
set param first list
if do body [append result get param]
]
][
while [not tail? list][
foreach item param [
set :item first list
list: next list
]
if do body [
foreach item param [
either only [
if find filter item [append result get item]
][
append result get item
]
]
]
]
]
result
]
>> keep a [1 2 3 4 5 6] [even? a]
== [2 4 6]
I use them all the time. BTW, any comments or critiques are welcome, as I
am still on the learning curve.
-- Ric