Skip to content

Commit

Permalink
!100 find for list and vector in (liii scala)
Browse files Browse the repository at this point in the history
  • Loading branch information
da-liii committed Jan 10, 2025
1 parent ff498c3 commit 4ead8e9
Show file tree
Hide file tree
Showing 3 changed files with 205 additions and 3 deletions.
23 changes: 22 additions & 1 deletion goldfish/liii/scala.scm
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
(define-library (liii scala)
(import (liii string) (liii vector) (liii list))
(export
option option? option=?
option option? option=? none
case-list case-list? case-list=?
case-vector case-vector? case-vector=?)
(begin
Expand Down Expand Up @@ -55,6 +55,8 @@
(if (procedure? default) (default) default)
value))
)
(define (none) (option '()))

(define-case-class case-list ((data list?))
(define (%collect) data)

Expand Down Expand Up @@ -91,11 +93,21 @@
(let1 r (case-list (scala-take-right data x))
(if (null? xs) r (apply r xs))))

(define (%find pred)
(let loop ((lst data))
(cond
((null? lst) (none))
((pred (car lst)) (option (car lst)))
(else (loop (cdr lst))))))

(define (%count . xs)
(cond ((null? xs) (length data))
((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 Expand Up @@ -176,6 +188,15 @@
(let1 r (case-vector (scala-take-right data x))
(if (null? xs) r (apply r xs))))

(define (%find p)
(let loop ((i 0))
(cond
((>= i (vector-length data)) (none))
((p (vector-ref data i)) (option (vector-ref data i)))
(else (loop (+ i 1))))))
(define (%forall p)
(vector-every p data))

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

Expand Down
148 changes: 146 additions & 2 deletions liii_scala.tmu
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<TMU|<tuple|1.0.5|1.2.9.8>>
<TMU|<tuple|1.0.5|1.2.9.7>>

<style|<tuple|generic|chinese|goldfish|literate|reduced-margins|python>>

Expand Down Expand Up @@ -94,7 +94,7 @@

(export

\ \ option option? option=?
\ \ option option? option=? none

\ \ case-list case-list? case-list=?

Expand Down Expand Up @@ -279,6 +279,14 @@
\;
</scm-chunk>

<paragraph|none><index|none>

<\scm-chunk|goldfish/liii/scala.scm|true|true>
(define (none) (option '()))

\;
</scm-chunk>

<paragraph|case-list>

<\scm-chunk|goldfish/liii/scala.scm|true|true>
Expand Down Expand Up @@ -399,6 +407,48 @@
\;
</scm-chunk>

<paragraph|case-list%find>

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

\ \ \ \ (let loop ((lst data))

\ \ \ \ \ \ (cond

\ \ \ \ \ \ \ \ ((null? lst) (none))

\ \ \ \ \ \ \ \ ((pred (car lst)) (option (car lst)))

\ \ \ \ \ \ \ \ (else (loop (cdr lst))))))

\;
</scm-chunk>

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

\ \ (check ((lst :find (lambda (x) (= x 3))) :get) =\<gtr\> 3)

\ \ (check ((lst :find (lambda (x) (\<gtr\> x 2))) :get) =\<gtr\> 3)

\;

\ \ (check ((lst :find (lambda (x) (\<gtr\> x 10))) :empty?) =\<gtr\> #t)

\;

\ \ (check ((lst :find even?) :get) =\<gtr\> 2)

\;

\ \ (check ((lst :find (lambda (x) (\<less\> x 0))) :empty?) =\<gtr\> #t)

)

\;
</goldfish-chunk>

<paragraph|case-list%count>

<\goldfish-chunk|goldfish/liii/scala.scm|true|true>
Expand All @@ -421,6 +471,34 @@
\;
</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>

<\scm-chunk|goldfish/liii/scala.scm|true|true>
Expand Down Expand Up @@ -681,6 +759,72 @@
\;
</goldfish-chunk>

<paragraph|case-vector%find>

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

\ \ \ \ (let loop ((i 0))

\ \ \ \ \ \ (cond

\ \ \ \ \ \ \ \ ((\<gtr\>= i (vector-length data)) (none))

\ \ \ \ \ \ \ \ ((p (vector-ref data i)) (option (vector-ref data i)))

\ \ \ \ \ \ \ \ (else (loop (+ i 1))))))
</scm-chunk>

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

\ \ (check ((vec :find (lambda (x) (= x 3))) :get) =\<gtr\> 3)

\ \ (check ((vec :find (lambda (x) (\<gtr\> x 2))) :get) =\<gtr\> 3)

\;

\ \ (check ((vec :find (lambda (x) (\<gtr\> x 10))) :empty?) =\<gtr\> #t)

\;

\ \ (check ((vec :find even?) :get) =\<gtr\> 2)

\;

\ \ (check ((vec :find (lambda (x) (\<less\> x 0))) :empty?) =\<gtr\> #t)

)

\;
</scm-chunk>

<paragraph|case-vector%forall>

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

\ \ \ \ (vector-every p data))

\;
</scm-chunk>

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

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

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

\;

(let ((empty-vec (case-vector #())))

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

\;
</scm-chunk>

<paragraph|case-vector%fold>

<\scm-chunk|goldfish/liii/scala.scm|true|true>
Expand Down
37 changes: 37 additions & 0 deletions tests/goldfish/liii/scala-test.scm
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,28 @@
(check (lst :take-right 10 :collect) => '(1 2 3 4 5))
)

(let1 lst (case-list '(1 2 3 4 5))
(check ((lst :find (lambda (x) (= x 3))) :get) => 3)
(check ((lst :find (lambda (x) (> x 2))) :get) => 3)

(check ((lst :find (lambda (x) (> x 10))) :empty?) => #t)

(check ((lst :find even?) :get) => 2)

(check ((lst :find (lambda (x) (< x 0))) :empty?) => #t)
)

(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 Expand Up @@ -116,6 +135,24 @@
(check (vec :take-right 10 :collect) => #(1 2 3 4 5))
)

(let ((vec (case-vector #(1 2 3 4 5))))
(check ((vec :find (lambda (x) (= x 3))) :get) => 3)
(check ((vec :find (lambda (x) (> x 2))) :get) => 3)

(check ((vec :find (lambda (x) (> x 10))) :empty?) => #t)

(check ((vec :find even?) :get) => 2)

(check ((vec :find (lambda (x) (< x 0))) :empty?) => #t)
)

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

(let ((empty-vec (case-vector #())))
(check (empty-vec :forall (lambda (x) (> x 0))) => #t))

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

0 comments on commit 4ead8e9

Please sign in to comment.