Skip to content

Commit

Permalink
!293 Add rich-list%reduce-option in (liii lang)
Browse files Browse the repository at this point in the history
  • Loading branch information
da-liii committed Mar 5, 2025
1 parent 1107def commit eb6eb16
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 33 deletions.
70 changes: 52 additions & 18 deletions GoldfishLang.tmu
Original file line number Diff line number Diff line change
Expand Up @@ -3975,46 +3975,52 @@

<section|高阶函数>

\;

<\scm-chunk|goldfish/liii/lang.scm|true|true>
\ \ (define (%map x . xs)
<paragraph|rich-list%map>

\ \ \ \ (let1 r (rich-list (map x data))
<\goldfish-chunk|goldfish/liii/lang.scm|true|true>
(chained-define (%map x)

\ \ \ \ \ \ (if (null? xs) r (apply r xs))))
\ \ (rich-list (map x data)))

\ \
\;
</goldfish-chunk>

\ \ (define (%flat-map x . xs)
<paragraph|rich-list%flat-map>

\ \ \ \ (let1 r (rich-list (flat-map x data))
<\goldfish-chunk|goldfish/liii/lang.scm|true|true>
(chained-define (%flat-map x)

\ \ \ \ \ \ (if (null? xs) r (apply r xs))))
\ \ (rich-list (flat-map x data)))

\ \
\;
</goldfish-chunk>

\ \ (define (%filter x . xs)
<paragraph|rich-list%filter>

\ \ \ \ (let1 r (rich-list (filter x data))
<\goldfish-chunk|goldfish/liii/lang.scm|true|true>
(chained-define (%filter x)

\ \ \ \ \ \ (if (null? xs) r (apply r xs))))
\ \ (rich-list (filter x data)))

\;
</goldfish-chunk>

\ \ (define (%for-each x)
<paragraph|rich-list%for-each>

<\scm-chunk|goldfish/liii/lang.scm|true|true>
(define (%for-each x)

\ \ \ \ (for-each x data))
\ \ (for-each x data))

\;
</scm-chunk>

<paragraph|rich-list%reverse>

<\goldfish-chunk|goldfish/liii/lang.scm|true|true>
\ \ (chained-define (%reverse)
(chained-define (%reverse)

\ \ \ \ (rich-list (reverse data)))
\ \ (rich-list (reverse data)))

\ \ \ \
</goldfish-chunk>
Expand Down Expand Up @@ -4543,6 +4549,34 @@
\;
</goldfish-chunk>

<paragraph|rich-list%reduce-option>

<\goldfish-chunk|goldfish/liii/lang.scm|true|true>
(define (%reduce-option f)

\ \ (if (null? data)

\ \ \ \ \ \ (none)

\ \ \ \ \ \ (option (reduce f '() data))))

\;
</goldfish-chunk>

<\goldfish-chunk|tests/goldfish/liii/lang-test.scm|true|true>
(check ($ '() :reduce-option +) =\<gtr\> (none))

\;

(check ($ '(1 2 3) :reduce-option +) =\<gtr\> (option 6)) \

(check ($ '(2 3 4) :reduce-option *) =\<gtr\> (option 24)) \

(check ($ '(5) :reduce-option (lambda (x y) (+ x y 10))) =\<gtr\> (option 5))

\;
</goldfish-chunk>

<section|转换器>

<paragraph|rich-list%to-string>
Expand Down
32 changes: 17 additions & 15 deletions goldfish/liii/lang.scm
Original file line number Diff line number Diff line change
Expand Up @@ -768,23 +768,20 @@
(define (%contains elem)
(%exists (lambda (x) (equal? x elem))))

(define (%map x . xs)
(let1 r (rich-list (map x data))
(if (null? xs) r (apply r xs))))

(define (%flat-map x . xs)
(let1 r (rich-list (flat-map x data))
(if (null? xs) r (apply r xs))))

(define (%filter x . xs)
(let1 r (rich-list (filter x data))
(if (null? xs) r (apply r xs))))
(chained-define (%map x)
(rich-list (map x data)))

(define (%for-each x)
(for-each x data))
(chained-define (%flat-map x)
(rich-list (flat-map x data)))

(chained-define (%filter x)
(rich-list (filter x data)))

(define (%for-each x)
(for-each x data))

(chained-define (%reverse)
(rich-list (reverse data)))
(chained-define (%reverse)
(rich-list (reverse data)))

(define (%take x . xs)
(typed-define (scala-take (data list?) (n integer?))
Expand Down Expand Up @@ -887,6 +884,11 @@
(value-error "rich-list%reduce: empty list is not allowed to reduce")
(reduce f '() data)))

(define (%reduce-option f)
(if (null? data)
(none)
(option (reduce f '() data))))

(define (%to-string)
(object->string data))

Expand Down
6 changes: 6 additions & 0 deletions tests/goldfish/liii/lang-test.scm
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,12 @@
(check ($ '(2 3 4) :reduce *) => 24)
(check ($ '(5) :reduce (lambda (x y) (+ x y 10))) => 5)

(check ($ '() :reduce-option +) => (none))

(check ($ '(1 2 3) :reduce-option +) => (option 6))
(check ($ '(2 3 4) :reduce-option *) => (option 24))
(check ($ '(5) :reduce-option (lambda (x y) (+ x y 10))) => (option 5))

(check (object->string ($ '(1 2 3))) => "(1 2 3)")

(let1 l (rich-list (list 1 2 3))
Expand Down

0 comments on commit eb6eb16

Please # to comment.