[REBOL] Re: En Masse block operations
From: tim:johnsons-web at: 16-Oct-2001 11:12
On Tue, Oct 16, 2001 at 12:05:21PM -0500, Joel Neely wrote:
> Sounds like a job for MAP to me...
>
> >> blk: [[1 2 3 4] [5 6 7 8] [9 10 11 12] [13 14 15 16 17] "one"]
> == [[1 2 3 4] [5 6 7 8] [9 10 11 12] [13 14 15 16 17] "one"]
>
> ... then ...
>
> map: function [[catch] b [block!] f [function!] /all] [r v] [
> r: make block! length? b
> foreach c b [
> if any [found? v: do [f c] all] [append/only r v]
> ]
> r
> ]
>
> ... and, finally ...
>
> >> map blk func [s][next s]
> == [[2 3 4] [6 7 8] [10 11 12] [14 15 16 17] "ne"]
>
> ... or ...
>
> >> x: map blk func [s] [next s]
> == [[2 3 4] [6 7 8] [10 11 12] [14 15 16 17] "ne"]
> >> x: map x func [s] [next s]
> == [[3 4] [7 8] [11 12] [15 16 17] "e"]
> >> map x func [s] [head s]
> == [[1 2 3 4] [5 6 7 8] [9 10 11 12] [13 14 15 16 17] "one"]
>
Joel: Thanks a lot! But now I have another question:
I see Ryan's opinion not to use 'foreach.....
Could you comment on his opinion on this.
(I've seen references to a foreach "bug" and
I almost never use it myself.)
Regards
Tim