blob: b0084713f91a765dc563cc82c255a13fa08bfe47 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
(define-module (data optional)
#:use-module (oop goops)
#:use-module (ice-9 match)
#:use-module (control monad)
#:export (nothing just
nothing? just?
return-optional)
;; TODO is this reexport needed?
#:re-export (>>=))
(define-class <optional> ()
(slot #:init-value #f
#:init-keyword #:slot)
(just #:init-value #t
#:init-keyword #:just))
(define (nothing) (make <optional> #:just #f))
(define (just obj) (make <optional>
#:just #t
#:slot obj))
(define (nothing? this)
(not (slot-ref this 'just)))
(define (just? this)
(not (nothing? this)))
(define-method (write (this <optional>) port)
(if (just? this)
(format port "[Just ~s]" (slot-ref this 'slot))
(format port "[Nothing]")))
(define return-optional just)
(define-method (>>= (this <optional>)
(proc <procedure>))
(cond ((nothing? this) (nothing))
((just? this)
(match this
(($ <optional> slot) (proc slot))))))
(define-method (>> (a <optional>)
(b <optional>))
(if (or (nothing? a)
(nothing? b))
(nothing)
b))
|