[REBOL] Re: Interlacing blocks
From: alain::goye::free::fr at: 6-Jan-2004 16:39
Hi all,
after NFOREACH, this will look very simplistic, but I needed something to
evaluate a function on ALL elements of a block, and keep ALL the results (in
a block), so I did these.
Can this be useful, or is there a better way ? And also better names ?
These functions are almost 1-liners. There are 4 variants; example of use
are given below:
toall: func [
blk [block!]
fun [function! native!]
/local res
] [
foreach args blk [append [] either attempt repend copy [res: fun] args [res]
[none]]
]
allarg: func [
blk [block!]
body [block!]
/local fun res
] [
fun: make function! [arg] body
foreach args blk [append [] either attempt [res: fun args] [res] [none]]
]
toany: func [
blk [block!]
fun [function! native!]
/local res
] [
foreach args blk [if attempt repend copy [res: fun] args [return res]] none
]
anyarg: func [
blk [block!]
body [block!]
/local fun res
] [
fun: make function! [arg] body
foreach args blk [if attempt [res: fun args] [return res]] none
]
all toall [1 3 5 7 9 12] func [arg] [odd? arg] ; is the same as:
all allarg [1 3 5 7 9 12] [odd? arg]
anyarg [1 3 5 7 9 12] [even? arg]
toall [[2 2][2 3][3 2]] func [arg1 arg2] [arg1 ** arg2] ; is the same as:
allarg [[2 2][2 3][3 2]] [arg/1 ** arg/2]
Alain Goyé.