aboutsummaryrefslogtreecommitdiff
path: root/data/either.scm
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@hornquist.se>2018-11-10 17:09:57 +0100
committerHugo Hörnquist <hugo@hornquist.se>2018-11-10 17:09:57 +0100
commitd6377ddd8f4a88cd07fdd927f78d7e5a90c13f5d (patch)
treee271c3c9e07c75eac76da0e8ae9767407d967a5b /data/either.scm
parentFurther work. (diff)
downloadscheme-monad-d6377ddd8f4a88cd07fdd927f78d7e5a90c13f5d.tar.gz
scheme-monad-d6377ddd8f4a88cd07fdd927f78d7e5a90c13f5d.tar.xz
Move stuff into modules.
Diffstat (limited to '')
-rw-r--r--data/either.scm28
1 files changed, 28 insertions, 0 deletions
diff --git a/data/either.scm b/data/either.scm
new file mode 100644
index 0000000..d6e0f73
--- /dev/null
+++ b/data/either.scm
@@ -0,0 +1,28 @@
+(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)