[REBOL] Re: A "supercharged" Map function.
From: brett:codeconscious at: 22-Jul-2003 19:32
Don't throw away your MAP-ish things just yet - you may not be expecting
that my version evaluates the series elements, which may or may not please
you :^)
Another version is below that uses do/next to avoid the assumption about the
number of arguments.
Also, here's a couple more samples to try:
map :add [now/date 3 now/date 4 now/date 5]
map :parse [ [1] [integer!] [a 2] [word integer!]]
map: func [
[catch]
{Map a function to a series (evaluates series elements,
unsets filtered by default).}
mapfunc [any-function! block! none!]
"Function or function body block (use VALUE as argument)."
series [series! none!] "Series to map."
/only "Inserts into result using Only refinement."
/filter filterfunc [any-function! block!]
"Single argument function or body (use VALUE as argument)."
/local result emit value pos tmp
] [
; Check arguments for none!
if any [none? :mapfunc none? series] [return none]
if block? :mapfunc [
mapfunc: func [value [any-type!]] mapfunc]
if not filter [
filterfunc: [not unset? get/any 'value]]
if block? :filterfunc [
filterfunc: func [value [any-type!]] filterfunc]
result: make type? series length? series
emit: func [value [any-type!]] compose/deep [
if filterfunc get/any 'value [
(pick [insert/only insert] found? only)
tail result get/any 'value
]
]
series: compose [mapfunc (series)]
while [greater? length? series 1] [
tmp: do/next series
emit tmp/1
remove/part next series tmp/2
]
result
]
Regards,
Brett.