[REBOL] Re: count function
From: joel:neely:fedex at: 5-Jul-2001 10:22
Hi, Michael,
Good point!!!
[JELINEM1--nationwide--com] wrote:
> It also depends on how you want to count:
>
> >> count "ABABABABABAABABABABABABABABABABA" "ABA"
> == 8
> - OR -
> >> count "ABABABABABAABABABABABABABABABABA" "ABA"
> == 15
>
Unfortunately, the best I can think of at the moment is
count: func [s t /distinct /local n] [
n: 0
either distinct [
while [s: find/tail s t] [n: n + 1]
][
while [s: find s t] [s: next s n: n + 1]
]
n
]
which behaves as
>> count "ABABABABABAABABABABABABABABABABA" "ABA"
== 15
>> count/distinct "ABABABABABAABABABABABABABABABABA" "ABA"
== 8
The version with WHILE factored out is both slower and uglier IMHO.
count: func [s t /distinct /local n] [
n: 0
while [s: either distinct [find/tail s t] [find s t]] [
if not distinct [s: next s]
n: n + 1
]
n
]
Although it does work
>> count "ABABABABABAABABABABABABABABABABA" "ABA"
== 15
>> count/distinct "ABABABABABAABABABABABABABABABABA" "ABA"
== 8
it seems to me that distributing the /DISTINCT test into WHILE
requires redundant evaluations and also obscures the single
meaning of "find the distinct occurrences" to the casual reader.
-jn-
___________________________________________________________________
The purpose of computing is insight, not numbers!
- R. W. Hamming
joel'dot'neely'at'fedex'dot'com