Skip to content

Commit

Permalink
!96 take and take-right in (liii list)
Browse files Browse the repository at this point in the history
  • Loading branch information
da-liii committed Jan 10, 2025
1 parent 9792562 commit 22c2080
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 1 deletion.
82 changes: 81 additions & 1 deletion GoldfishScheme.tmu
Original file line number Diff line number Diff line change
Expand Up @@ -8559,7 +8559,87 @@
\;
</scm-chunk>

<subparagraph|count>
<paragraph|case-list%take>

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

\ \ \ \ (typed-define (scala-take (data list?) (n integer?))

\ \ \ \ \ \ (cond ((\<less\> n 0) '())

\ \ \ \ \ \ \ \ \ \ \ \ ((\<gtr\>= n (length data)) data)

\ \ \ \ \ \ \ \ \ \ \ \ (else (take data n))))

\;

\ \ \ \ (let1 r (case-list (scala-take data x))

\ \ \ \ \ \ (if (null? xs) r (apply r xs))))

\;
</scm-chunk>

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

\ \ (check (lst :take -1 :collect) =\<gtr\> '())

\ \ (check (lst :take 0 :collect) =\<gtr\> '())

\ \ (check (lst :take 3 :collect) =\<gtr\> '(1 2 3))

\ \ (check (lst :take 5 :collect) =\<gtr\> '(1 2 3 4 5))

\ \ (check (lst :take 10 :collect) =\<gtr\> '(1 2 3 4 5))

)

\;
</scm-chunk>

<paragraph|case-list%take-right>

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

\ \ \ \ (typed-define (scala-take-right (data list?) (n integer?))

\ \ \ \ \ \ (cond ((\<less\> n 0) '())

\ \ \ \ \ \ \ \ \ \ \ \ ((\<gtr\>= n (length data)) data)

\ \ \ \ \ \ \ \ \ \ \ \ (else (take-right data n))))

\;

\ \ \ \ (let1 r (case-list (scala-take-right data x))

\ \ \ \ \ \ (if (null? xs) r (apply r xs))))

\;
</scm-chunk>

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

\ \ (check (lst :take-right -1 :collect) =\<gtr\> '())

\ \ (check (lst :take-right 0 :collect) =\<gtr\> '())

\ \ (check (lst :take-right 3 :collect) =\<gtr\> '(3 4 5))

\ \ (check (lst :take-right 5 :collect) =\<gtr\> '(1 2 3 4 5))

\ \ (check (lst :take-right 10 :collect) =\<gtr\> '(1 2 3 4 5))

)

\;
</scm-chunk>

<paragraph|case-list%count>

<\goldfish-chunk|goldfish/liii/list.scm|true|true>
\ \ (define (%count . xs)
Expand Down
18 changes: 18 additions & 0 deletions goldfish/liii/list.scm
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,24 @@
(define (%for-each x)
(for-each x data))

(define (%take x . xs)
(typed-define (scala-take (data list?) (n integer?))
(cond ((< n 0) '())
((>= n (length data)) data)
(else (take data n))))

(let1 r (case-list (scala-take data x))
(if (null? xs) r (apply r xs))))

(define (%take-right x . xs)
(typed-define (scala-take-right (data list?) (n integer?))
(cond ((< n 0) '())
((>= n (length data)) data)
(else (take-right data n))))

(let1 r (case-list (scala-take-right data x))
(if (null? xs) r (apply r xs))))

(define (%count . xs)
(cond ((null? xs) (length data))
((length=? 1 xs) (count (car xs) data))
Expand Down
16 changes: 16 additions & 0 deletions tests/goldfish/liii/list-test.scm
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,22 @@
(check-catch 'type-error (flatten '((a) () (b ()) () (c)) 'a))
(check-catch 'type-error (flatten '((a) () (b ()) () (c)) (make-vector 1 1)))

(let ((lst (case-list '(1 2 3 4 5))))
(check (lst :take -1 :collect) => '())
(check (lst :take 0 :collect) => '())
(check (lst :take 3 :collect) => '(1 2 3))
(check (lst :take 5 :collect) => '(1 2 3 4 5))
(check (lst :take 10 :collect) => '(1 2 3 4 5))
)

(let ((lst (case-list '(1 2 3 4 5))))
(check (lst :take-right -1 :collect) => '())
(check (lst :take-right 0 :collect) => '())
(check (lst :take-right 3 :collect) => '(3 4 5))
(check (lst :take-right 5 :collect) => '(1 2 3 4 5))
(check (lst :take-right 10 :collect) => '(1 2 3 4 5))
)

(check ((case-list (list 1 2 3)) :count) => 3)
(check ((case-list (list 1 2 3)) :count (cut > <> 1)) => 2)

Expand Down

0 comments on commit 22c2080

Please sign in to comment.