Interlacing blocks
[1/4] 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é.
[2/4] from: greggirwin:mindspring at: 6-Jan-2004 12:40
Hi Alain,
AG> I needed something to evaluate a function on ALL elements of a
AG> block, and keep ALL the results (in a block), so I did these.
AG> Can this be useful, or is there a better way ? And also better
AG> names ?
Looks kind of like MAP to me.
map: func [fn blk args /local result][
result: copy []
repeat el blk [append/only result fn :el args]
result
]
>> map :odd? [1 3 5 7 9 12] none
== [true true true true true false]
You might also look for old discussions here on the COLLECT functions
some people have done.
-- Gregg
[3/4] from: nitsch-lists:netcologne at: 7-Jan-2004 1:45
Am Montag 05 Januar 2004 06:48 schrieb Henrik Mikael Kristensen:
> Hi
> Interlacing blocks:
<<quoted lines omitted: 3>>
> => d: [a x 1 b y 2 c z 3]
> Is there a function to do this? I wrote my own, but was curious.
If nothing else is at hand,
a: [a b c]
b: [x y z]
c: [1 2 3]
d: copy[]
repeat i length? a[repend d[a/:i b/:i c/:i]]
? d
Its so short i would not write something special.
Otherwise nforeach of course.
-Volker
[4/4] from: alain:goye:free at: 7-Jan-2004 8:37
Thank you Gregg,
this is my answer !
Alain.
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted