-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathexamples.lisp
75 lines (53 loc) · 1.43 KB
/
examples.lisp
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
;;;; defdata-examples.lisp
(in-package #:cl-algebraic-data-type)
;;; Maybe
(defdata maybe
(just t)
nothing)
(defun maybe-or-else (m else)
(match maybe m
((just x) x)
(nothing else)))
(defun bind-maybe (f m)
(match maybe m
((just x) (funcall f x))
(nothing nothing)))
(defun inc-maybe (x)
(just (1+ x)))
(defun maybe->string (x)
(just (write-to-string x)))
(string-equal "2" (do-notation bind-maybe
((sum (inc-maybe 1))
(str (maybe->string sum)))
str))
(equal nothing (do-notation bind-maybe
((sum nothing)
(str (maybe->string sum)))
(print str)))
;;; Either
(defdata either
(left t)
(right t))
(defun bind-either (f m)
(match either m
((right x) (funcall f x))
((left b) (left b))))
(defun inc-either (x)
(right (1+ x)))
(defun either->string (x)
(right (write-to-string x)))
(string-equal "2" (do-notation bind-either
((sum (inc-either 1))
(str (either->string sum)))
str))
(equalp (left 1) (do-notation bind-either
((sum (left 1))
(str (either->string sum)))
(print str)))
;;; Point
(defdata (point :mutable t)
(rect float float))
(defvar *origin* (rect 0.0 0.0))
(defun mirror-point! (pt)
(with-data (rect x y) pt
(set-data pt (rect y x))))