Mailing List Archive: 49091 messages

count function

[1/8] from: maksa::sezampro::yu at: 5-Jul-2001 8:45

Hello, Any suggestions what would the cleanest and most Rebolesque possible 'count' function look like? Returning something like this:
>> count [Joe Jane Joe Simon] Joe >> 2
and:
>> count "ABCSOMESTUFFABDMOREABSTUFFANDTHENABESOME" "AB" >> 4
Regards, Maksa

[2/8] from: chris:langreiter at: 5-Jul-2001 13:03

count: func [x y /local n] [ n: 0 while [not = none find x y] [ n: n + 1 x: next find x y ] n ] should do the trick for count [a b c a z z] 'a count "boomboxxbox" "box"

[3/8] from: arolls:bigpond:au at: 5-Jul-2001 21:52

Here's another way: count: func [s t /local n][ n: 0 while [s: find/tail s t][n: n + 1] ]

[4/8] from: joel:neely:fedex at: 4-Jul-2001 21:10

Hi, Christian and Maksa, Just a couple of additional remarks... Christian Langreiter wrote:
> count: func [x y /local n] [ > n: 0
<<quoted lines omitted: 4>>
> n > ]
If you don't mind a tiny bit of refactoring, I'd offer count: func [x y /local n] [ n: 0 while [found? x: find x y] [ n: n + 1 x: next x ] n ] based on the transformation not = none foo -> not none? foo -> found? foo and eliminating the redundant invocations of FIND, since after passing the test of found? find x y the subsequent x: next find x y would FIND exactly the same occurrence.
>> count [a b c a z z] 'a
== 2
>> count "boomboxxbox" "box"
== 2
>> count [Joe Jane Joe Simon] 'Joe
== 2
>> count "ABCSOMESTUFFABDMOREABSTUFFANDTHENABESOME" "AB"
== 4
> > > > Any suggestions what would the cleanest and most
<<quoted lines omitted: 4>>
> > >> 2 > >
The quoting of JOE into a LIT-WORD! value, as Christian showed with his reply, is probably necessary.
>> count [Joe Jane Joe Simon] Joe
** Script Error: Joe has no value ** Near: count [Joe Jane Joe Simon] Joe In the expression count [Joe Jane Joe Simon] Joe the two arguments are a block of (unevaluated) words and a word (possibly evaluated, depending on the definition of the function). In order for the above to succeed (with non-zero result!) 1) COUNT must defined to allow a WORD! as second argument, as in count: func [x 'y /local n] [ n: 0 while [found? x: find x y] [n: n + 1 x: next x] n ]
>> count "ABCSOMESTUFFABDMOREABSTUFFANDTHENABESOME" "AB"
== 4
>> count [Joe Jane Joe Simon] 'Joe
== 2
>> count [Joe Jane Joe Simon] Joe
== 2
>> count [a b c a z z] 'a
== 2 but then we have trouble with the (presumably common) case of
>> Somebody: 'Joe
== Joe
>> count [Joe Jane Joe Simon] Somebody
== 0 2) The even more unlikely case (given the original argument handling) of
>> Joe: 'Joe
== Joe
>> count [Joe Jane Joe Simon] Joe
== 2 which, I must admit, I've never had occasion to do! ;-) -jn- ------------------------------------------------------------ Programming languages: compact, powerful, simple ... Pick any two! joel'dot'neely'at'fedex'dot'com

[5/8] from: jelinem1:nationwide at: 5-Jul-2001 9:12

It also depends on how you want to count:
>> count "ABABABABABAABABABABABABABABABABA" "ABA"
== 8 - OR -
>> count "ABABABABABAABABABABABABABABABABA" "ABA"
== 15 - Michael Jelinek From: Joel Neely <[joel--neely--fedex--com]>@inet01.prod.fedex.com on 07/04/2001 09:10 PM Please respond to [rebol-list--rebol--com] Sent by: [jn--inet01--prod--fedex--com] To: [rebol-list--rebol--com] cc: Subject: [REBOL] Re: count function Hi, Christian and Maksa, Just a couple of additional remarks... Christian Langreiter wrote:
> count: func [x y /local n] [ > n: 0
<<quoted lines omitted: 4>>
> n > ]
If you don't mind a tiny bit of refactoring, I'd offer count: func [x y /local n] [ n: 0 while [found? x: find x y] [ n: n + 1 x: next x ] n ] based on the transformation not = none foo -> not none? foo -> found? foo and eliminating the redundant invocations of FIND, since after passing the test of found? find x y the subsequent x: next find x y would FIND exactly the same occurrence.
>> count [a b c a z z] 'a
== 2
>> count "boomboxxbox" "box"
== 2
>> count [Joe Jane Joe Simon] 'Joe
== 2
>> count "ABCSOMESTUFFABDMOREABSTUFFANDTHENABESOME" "AB"
== 4
> > > > Any suggestions what would the cleanest and most
<<quoted lines omitted: 4>>
> > >> 2 > >
The quoting of JOE into a LIT-WORD! value, as Christian showed with his reply, is probably necessary.
>> count [Joe Jane Joe Simon] Joe
** Script Error: Joe has no value ** Near: count [Joe Jane Joe Simon] Joe In the expression count [Joe Jane Joe Simon] Joe the two arguments are a block of (unevaluated) words and a word (possibly evaluated, depending on the definition of the function). In order for the above to succeed (with non-zero result!) 1) COUNT must defined to allow a WORD! as second argument, as in count: func [x 'y /local n] [ n: 0 while [found? x: find x y] [n: n + 1 x: next x] n ]
>> count "ABCSOMESTUFFABDMOREABSTUFFANDTHENABESOME" "AB"
== 4
>> count [Joe Jane Joe Simon] 'Joe
== 2
>> count [Joe Jane Joe Simon] Joe
== 2
>> count [a b c a z z] 'a
== 2 but then we have trouble with the (presumably common) case of
>> Somebody: 'Joe
== Joe
>> count [Joe Jane Joe Simon] Somebody
== 0 2) The even more unlikely case (given the original argument handling) of
>> Joe: 'Joe
== Joe
>> count [Joe Jane Joe Simon] Joe
== 2 which, I must admit, I've never had occasion to do! ;-) -jn- ------------------------------------------------------------ Programming languages: compact, powerful, simple ... Pick any two! joel'dot'neely'at'fedex'dot'com

[6/8] from: jelinem1:nationwide at: 5-Jul-2001 9:33

I liked Anton's solution for 'count so well that I copied it and searched for agood place to put it into my shell.r - only to find that I already had a function to do this. Not as efficient (I've timed them for the case of searching strings) but takes a different approach: occur: function [ "Count the number of times the given value occurs in the given series" some-series [series!] some-value ][c][ c: 0 parse/all some-series [any [thru some-value (c: 1 + c)]] c ] - Michael Jelinek From: "Anton" <[arolls--bigpond--net--au]>@rebol.com on 07/05/2001 06:52 AM Please respond to [rebol-list--rebol--com] Sent by: [rebol-bounce--rebol--com] To: <[rebol-list--rebol--com]> cc: Subject: [REBOL] Re: count function Here's another way: count: func [s t /local n][ n: 0 while [s: find/tail s t][n: n + 1] ]

[7/8] from: joel:neely:fedex at: 5-Jul-2001 10:09

Hi, Anton, Anton wrote:
> Here's another way: > > count: func [s t /local n][ > n: 0 > while [s: find/tail s t][n: n + 1] > ] >
I realize that the subject of zero is highly sensitive ;-) but the above only returns a count if T actually appears in S.
>> count: func [s t /local n][
[ n: 0 [ while [s: find/tail s t][n: n + 1] [ ]
>> count "ABCABCABC" "AB"
== 3 but
>> count "ABCABCABC" "D"
== none which is why I prefer to return N explicitly. -jn- ___________________________________________________________________ The purpose of computing is insight, not numbers! - R. W. Hamming joel'dot'neely'at'fedex'dot'com

[8/8] 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

Notes
• Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted