aboutsummaryrefslogtreecommitdiff
path: root/monad/either.scm
blob: c597a6099bdc1ad38ff6094c3725811363f2b922 (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
;;; TODO UNFINISHED

(define-module (data either)
  #:use-module (oop goops)
  #:use-module (ice-9 match))

(define-class <either> ()
  (slot #:init-keyword #:slot)
  (dir #:init-keyword #:dir #:init-value 'left))

(define (left val)
  "Error values"
  (make <either> #:slot val #:dir 'left))

(define (right val)
  "Good values"
  (make <either> #:slot val #:dir 'right))

(define-method (write (this <either>) port)
  (format port "[~a ~s]"
          (slot-ref this 'dir)
          (slot-ref this 'slot)))

(define-method (>>= (this <either>)
                    (proc <procedure>))
  (case (slot-ref this 'dir)
    ((left) this)
    ((right) (match this (($ <either> slot) (proc slot))))))

(define return-either right)