Skip to content

Commit

Permalink
!108 forall, exists, count and contains for case-string in (liii scala)
Browse files Browse the repository at this point in the history
  • Loading branch information
da-liii committed Jan 11, 2025
1 parent 223bb41 commit de7d7f9
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 44 deletions.
4 changes: 4 additions & 0 deletions GoldfishScheme.tmu
Original file line number Diff line number Diff line change
Expand Up @@ -10618,6 +10618,10 @@
<\scm-chunk|goldfish/srfi/srfi-13.scm|true|true>
(define (string-count str char/pred? . start+end)

\ \ (when (not (string? str))

\ \ \ \ (type-error "string-count: first parameter must be string"))

\ \ (let ((str-sub (%string-from-range str start+end))

\ \ \ \ \ \ \ \ (criterion (%make-criterion char/pred?)))
Expand Down
15 changes: 12 additions & 3 deletions goldfish/liii/scala.scm
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@
(define (%length)
(string-length data))

(define (%forall pred)
(string-every pred data))

(define (%exists pred)
(string-any pred data))

(define (%contains ch)
(string-any (lambda (x) (equal? x ch)) data))

(define (%count pred?)
(string-count data pred?))

)

(define-case-class case-list ((data list?))
Expand Down Expand Up @@ -155,9 +167,6 @@
((length=? 1 xs) (count (car xs) data))
(else (error 'wrong-number-of-args "case-list%count" xs))))

(define (%forall pred)
(every pred data))

(define (%fold initial f)
(fold f initial data))

Expand Down
2 changes: 2 additions & 0 deletions goldfish/srfi/srfi-13.scm
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@
(loop (+ i 1)))))))

(define (string-count str char/pred? . start+end)
(when (not (string? str))
(type-error "string-count: first parameter must be string"))
(let ((str-sub (%string-from-range str start+end))
(criterion (%make-criterion char/pred?)))
(count criterion (string->list str-sub))))
Expand Down
110 changes: 79 additions & 31 deletions liii_scala.tmu
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,70 @@
\;
</scm-chunk>

<paragraph|case-string%forall>

检查字符串中的所有字符是否都满足给定的谓词。

<\goldfish-chunk|goldfish/liii/scala.scm|true|true>
(define (%forall pred)

\ \ (string-every pred data))

\;
</goldfish-chunk>

<paragraph|case-string%exists>

检查字符串中是否存在至少一个字符满足给定的谓词。

<\goldfish-chunk|goldfish/liii/scala.scm|true|true>
(define (%exists pred)

\ \ (string-any pred data))

\;
</goldfish-chunk>

<paragraph|case-string%contains>

检查字符串中是否包含指定的字符。

<\goldfish-chunk|goldfish/liii/scala.scm|true|true>
(define (%contains ch)

\ \ (string-any (lambda (x) (equal? x ch)) data))

\;
</goldfish-chunk>

<paragraph|case-string%count>

计算字符串中满足给定谓词的字符的数量

<\goldfish-chunk|goldfish/liii/scala.scm|true|true>
(define (%count pred?)

\ \ (string-count data pred?))

\;
</goldfish-chunk>

<\goldfish-chunk|tests/goldfish/liii/scala-test.scm|true|true>
(let1 str (case-string "Hello, World!")

\ \ (check (str :forall char-alphabetic?) =\<gtr\> #f)

\ \ (check (str :exists char-alphabetic?) =\<gtr\> #t)

\ \ (check (str :contains #\\W) =\<gtr\> #t)

\ \ (check (str :count char-alphabetic?) =\<gtr\> 10)

)

\;
</goldfish-chunk>

<\scm-chunk|goldfish/liii/scala.scm|true|true>
)

Expand Down Expand Up @@ -584,9 +648,19 @@
</goldfish-chunk>

<\goldfish-chunk|tests/goldfish/liii/scala-test.scm|true|true>
(let1 l (case-list '(1 2 3))
(let1 lst (case-list '(1 2 3 4 5))

\ \ (check-true (l :forall positive?)))
\ \ (check (lst :forall (lambda (x) (\<gtr\> x 0))) =\<gtr\> #t)

\ \ (check (lst :forall (lambda (x) (\<gtr\> x 3))) =\<gtr\> #f)

)

\;

(let1 empty-lst (case-list '())

\ \ (check (empty-lst :forall (lambda (x) (\<gtr\> x 0))) =\<gtr\> #t))

\;
</goldfish-chunk>
Expand Down Expand Up @@ -693,33 +767,7 @@
\;
</scm-chunk>

<paragraph|case-list%forall>

<\scm-chunk|goldfish/liii/scala.scm|true|true>
\ \ (define (%forall pred)

\ \ \ \ (every pred data))

\;
</scm-chunk>

<\scm-chunk|tests/goldfish/liii/scala-test.scm|true|true>
(let ((lst (case-list '(1 2 3 4 5))))

\ \ (check (lst :forall (lambda (x) (\<gtr\> x 0))) =\<gtr\> #t)

\ \ (check (lst :forall (lambda (x) (\<gtr\> x 3))) =\<gtr\> #f)

)

\;

(let ((empty-lst (case-list '())))

\ \ \ \ (check (empty-lst :forall (lambda (x) (\<gtr\> x 0))) =\<gtr\> #t))

\;
</scm-chunk>
\;

<subparagraph|fold>

Expand Down Expand Up @@ -929,7 +977,7 @@

<paragraph|case-vector%take-right>

<\scm-chunk|goldfish/liii/scala.scm|true|true>
<\goldfish-chunk|goldfish/liii/scala.scm|true|true>
\ \ (define (%take-right x . xs)

\ \ \ \ (typed-define (scala-take-right (data vector?) (n integer?))
Expand Down Expand Up @@ -961,7 +1009,7 @@
\ \ \ \ \ \ (if (null? xs) r (apply r xs))))

\;
</scm-chunk>
</goldfish-chunk>

<\goldfish-chunk|tests/goldfish/liii/scala-test.scm|true|true>
(let ((vec (case-vector #(1 2 3 4 5))))
Expand Down
24 changes: 14 additions & 10 deletions tests/goldfish/liii/scala-test.scm
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@

(check ((case-string "abc") :length) => 3)

(let1 str (case-string "Hello, World!")
(check (str :forall char-alphabetic?) => #f)
(check (str :exists char-alphabetic?) => #t)
(check (str :contains #\W) => #t)
(check (str :count char-alphabetic?) => 10)
)

(let ((lst (case-list '(1 2 3 4 5))))
(check (lst :take -1 :collect) => '())
(check (lst :take 0 :collect) => '())
Expand All @@ -93,8 +100,13 @@
(check (lst :take-right 10 :collect) => '(1 2 3 4 5))
)

(let1 l (case-list '(1 2 3))
(check-true (l :forall positive?)))
(let1 lst (case-list '(1 2 3 4 5))
(check (lst :forall (lambda (x) (> x 0))) => #t)
(check (lst :forall (lambda (x) (> x 3))) => #f)
)

(let1 empty-lst (case-list '())
(check (empty-lst :forall (lambda (x) (> x 0))) => #t))

(let1 l (case-list '(1 2 3))
(check-true (l :exists even?)))
Expand All @@ -117,14 +129,6 @@
(check ((case-list (list 1 2 3)) :count) => 3)
(check ((case-list (list 1 2 3)) :count (cut > <> 1)) => 2)

(let ((lst (case-list '(1 2 3 4 5))))
(check (lst :forall (lambda (x) (> x 0))) => #t)
(check (lst :forall (lambda (x) (> x 3))) => #f)
)

(let ((empty-lst (case-list '())))
(check (empty-lst :forall (lambda (x) (> x 0))) => #t))

(let ((lst (case-list '(1 2 3 4 5))))
(check (lst :fold 0 +) => 15)
(check (lst :fold '() (lambda (x acc) (cons x acc))) => '(5 4 3 2 1))
Expand Down

0 comments on commit de7d7f9

Please sign in to comment.