Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
What's new
2
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Switch to GitLab Next
Sign in / Register
Toggle navigation
Menu
Open sidebar
hi i am boris
Red mezz warehouse
Commits
b56fa0a5
Commit
b56fa0a5
authored
Nov 25, 2021
by
hi i am boris
Browse files
FEAT: enhanced count
parent
50afa331
Changes
1
Hide whitespace changes
Inline
Side-by-side
count.red
View file @
b56fa0a5
...
...
@@ -14,24 +14,63 @@ Red [
]
#include %new-apply.red
count: function [
"Count occurrences of
X in
S (using `=` by default)"
s [series!]
x
[any-type!]
"Count occurrences of
VALUE in SERIE
S (using `=` by default)"
s
eries
[series!]
value
[any-type!]
/case "Use strict comparison (`==`)"
/same "Use sameness comparison (`=?`)"
/only "Treat series and typesets as single values"
/head "Count the number of subsequent values at series' head"
/tail "Count the number of subsequent values at series' tail" ;@@ doesn't work for strings! #3339
][
n: 0
system/words/case [
same [while [s: find/tail/same s :x][n: n + 1]]
case [while [s: find/tail/case s :x][n: n + 1]]
true [while [s: find/tail s :x][n: n + 1]]
]
match: head or tail
unless tail: not reverse: tail [series: system/words/tail series]
n: 0 while [series: apply find 'local] [n: n + 1]
n
]
{
Toomas's version without apply: (and /case /same)
count: func [series item /only /head /tail /local cnt pos with-only without][
cnt: 0
set [ with-only without ] case [
tail [[[find/reverse/match/only series item] [find/reverse/match series item]]]
head [[[find/match/tail/only series item] [find/match/tail series item]]]
true [[[find/tail/only series item] [find/tail series item]]]
]
if tail [series: system/words/tail series]
while [all [pos: either only with-only without series: pos]] [cnt: cnt + 1]
cnt
]
}
; probe count/head "aaabbc" "a"
; probe count/head "aaabbc" "aa"
; probe count/head "aaabbc" "aaa"
; probe count/tail [1 2 2 3 3 3] 3
comment {
;; limited and ugly but fast version
count: function [
"Count occurrences of X in S (using `=` by default)"
s [series!]
x [any-type!]
/case "Use strict comparison (`==`)"
/same "Use sameness comparison (`=?`)"
][
n: 0
system/words/case [
same [while [s: find/tail/same s :x][n: n + 1]]
case [while [s: find/tail/case s :x][n: n + 1]]
true [while [s: find/tail s :x][n: n + 1]]
]
n
]
;; `compose` involves too much memory pressure
count: function [
"Count occurrences of X in S (using `=` by default)"
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment