[REBOL] Re: Block/array manipulation
From: lmecir:mbox:vol:cz at: 16-Oct-2001 21:48
Hi Joel,
> Joel Neely wrote:
> >
> > stuffstats: func [b [block!] /local result where] [
> > result: copy []
> > foreach item b [
> > either found? where: find result item [
> > change next where 1 + second where
> > ][
> > append result reduce [item 1]
> > ]
> > ]
> > result
> > ]
> >
>
> Of course, the above version only works if the original block
> excludes just the right combination of small integers... :-/
>
> This version corrects for that deficiency:
>
> >> stuff: [1 2 3 2 1 4 1 2 3 1]
> == [1 2 3 2 1 4 1 2 3 1]
>
> stuffstats: func [b [block!] /local result where] [
> result: copy []
> foreach item b [
> either found? where: find/skip result item 2 [
> change next where 1 + second where
> ][
> append result reduce [item 1]
> ]
> ]
> result
> ]
>
> as in
>
> >> stuffstats stuff
> == [1 4 2 3 3 2 4 1]
>
> Finally, a version that avoids the data type issue and give the
> results back in a nice order:
>
> stuffstats: func [b [block!] /local c i j result] [
> result: copy []
> c: sort copy b
> while [not tail? c] [
> i: first c
> j: 0
> while [all [not tail? c i = first c]] [
> j: j + 1
> c: next c
> ]
> append result reduce [i j]
> ]
> result
> ]
>
Isn't the latter solution much faster than the former one?
Cheers
Ladislav