[EMAIL PROTECTED] wrote:
>Here is a fast general map function which handles nested blocks
[skip]
>Any comments or discussion would be welcome.
[skip]
>===============CODE===============
>
>REBOL [
> Title: "Map Function"
> Date: "24-November-1999"
> Author: "Larry Palmiter"
> Purpose: { Maps a function of one arg
>onto a (nested) block of values.
> }
>]
>
>map: func [
> "Maps a function onto elements of a block"
> :f "function (use parens for literal)"
> b [block!] "block of values"
> /deep "maps function into nested blocks"
> /local out
>][
> out: make block! length? b
> if paren? :f [f: f]
> foreach el b [
> either block? el [
> either deep [
> append/only out map/deep f el
> ][
> append/only out el
> ]
> ][
> append out f el
> ]
> ]
>]
All right Larry, how about this?
map: func [
"Maps a function onto elements of a block"
:f [any-function! paren!] "function (use parens for literal)"
b [any-block!] "block of values"
/deep "maps function into nested blocks"
/local out
][
out: make type? :b length? :b
if paren? :f [f: f]
foreach el :b [
either any-block? :el [
append/only :out either deep [map/deep f :el] [:el]
][
append :out f :el
]
]
]
The changes allow you to map other block types and have
function and paren elements. It's shorter too :)
You're going to have to watch that recursion though...
REBOL generally has limited stack space, so deeply nested
blocks or ones with circular references will cause stack
overflows. There are techniques for eliminating recursion,
even ones that can be used here.
Brian