Skip to content

Commit

Permalink
!104 get and contains for case-hash-table 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 fd63b60 commit 587aa78
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
6 changes: 6 additions & 0 deletions goldfish/liii/scala.scm
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,12 @@
data)
(case-hash-table r))))

(define (%get k)
(option (hash-table-ref/default data k '())))

(define (%contains k)
(hash-table-contains? data k))

)

(define (box x)
Expand Down
42 changes: 41 additions & 1 deletion liii_scala.tmu
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@
\;
</scm-chunk>

<paragraph|case-list%find>
<paragraph|case-list%find><typehint|((pred procedure?)) =\<gtr\> option?>

<\scm-chunk|goldfish/liii/scala.scm|true|true>
\ \ (define (%find pred)
Expand Down Expand Up @@ -1077,6 +1077,46 @@
\ \ \ \ \ \
</goldfish-chunk>

<paragraph|case-hash-table%get><typehint|((k any?)) =\<gtr\> option?>

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

\ \ (option (hash-table-ref/default data k '())))

\;
</goldfish-chunk>

<\goldfish-chunk|tests/goldfish/liii/scala-test.scm|true|true>
(let1 ht (box (hash-table 'a 1 'b 2 'c 3))

\ \ (check ((ht :get 'a) :get) =\<gtr\> 1)

\ \ (check ((ht :get 'd) :empty?) =\<gtr\> #t))

\;
</goldfish-chunk>

<paragraph|case-hash-table%contains><typehint|((k any?)) =\<gtr\> boolean?>

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

\ \ (hash-table-contains? data k))

\;
</goldfish-chunk>

<\goldfish-chunk|tests/goldfish/liii/scala-test.scm|true|true>
(let1 ht (box (hash-table 'a 1 'b 2 'c 3))

\ \ (check-true (ht :contains 'a))

\ \ (check-false (ht :contains 'd)))

\;
</goldfish-chunk>

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

Expand Down
8 changes: 8 additions & 0 deletions tests/goldfish/liii/scala-test.scm
Original file line number Diff line number Diff line change
Expand Up @@ -188,5 +188,13 @@
(check (r 'b) => 3)
(check (r 'c) => 4)))

(let1 ht (box (hash-table 'a 1 'b 2 'c 3))
(check ((ht :get 'a) :get) => 1)
(check ((ht :get 'd) :empty?) => #t))

(let1 ht (box (hash-table 'a 1 'b 2 'c 3))
(check-true (ht :contains 'a))
(check-false (ht :contains 'd)))

(check-report)

0 comments on commit 587aa78

Please sign in to comment.