forked from coalton-lang/coalton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresult.lisp
172 lines (146 loc) · 4.31 KB
/
result.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
(coalton-library/utils:defstdlib-package #:coalton-library/result
(:use
#:coalton
#:coalton-library/builtin
#:coalton-library/classes
#:coalton-library/optional)
(:local-nicknames
(#:cell #:coalton-library/cell)
(#:iter #:coalton-library/iterator))
(:export
#:ok?
#:err?
#:map-err
#:flatten
#:ok-or-error))
(in-package #:coalton-library/result)
(named-readtables:in-readtable coalton:coalton)
#+coalton-release
(cl:declaim #.coalton-impl/settings:*coalton-optimize-library*)
(coalton-toplevel
;;
;; Result
;;
(declare ok? (Result :a :b -> Boolean))
(define (ok? x)
"Returns TRUE if X is OK"
(match x
((Ok _) True)
((Err _) False)))
(declare err? (Result :a :b -> Boolean))
(define (err? x)
"Returns TRUE if X is ERR"
(match x
((Err _) True)
((Ok _) False)))
(declare map-err ((:a -> :b) -> Result :a :c -> Result :b :c))
(define (map-err f x)
"Map over the ERR case"
(match x
((Err x) (Err (f x)))
((Ok x) (Ok x))))
(declare flatten (Result :a :a -> :a))
(define (flatten x)
(match x
((Ok x) x)
((Err x) x)))
(declare ok-or-error ((Signalable :err) => (Result :err :a) -> :a))
(define (ok-or-error res)
(match res
((Ok elt) elt)
((Err r) (error r))))
;;
;; Instances
;;
(define-instance ((Eq :a) (Eq :b) => Eq (Result :a :b))
(define (== a b)
(match (Tuple a b)
((Tuple (Ok a) (Ok b)) (== a b))
((Tuple (Err a) (Err b)) (== a b))
(_ False))))
(define-instance ((Ord :a) (Ord :b) => Ord (Result :a :b))
(define (<=> a b)
(match (Tuple a b)
((Tuple (Ok a) (Ok b)) (<=> a b))
((Tuple (Err a) (Err b)) (<=> a b))
((Tuple (Err _) (Ok _)) LT)
((Tuple (Ok _) (Err _)) GT))))
(define-instance (Semigroup :b => Semigroup (Result :a :b))
(define (<> a b)
(match (Tuple a b)
((Tuple (Ok x) (Ok y))
(Ok (<> x y)))
((Tuple (Err _) _) a)
(_ b))))
(define-instance (Monoid :b => Monoid (Result :a :b))
(define mempty (Ok mempty)))
(define-instance (Functor (Result :a))
(define (map f x)
(match x
((Ok x) (Ok (f x)))
((Err e) (Err e)))))
(define-instance (Applicative (Result :a))
(define (pure x) (Ok x))
(define (liftA2 f a b)
(match (Tuple a b)
((Tuple (Ok a) (Ok b))
(Ok (f a b)))
((Tuple (Err e) _) (Err e))
((Tuple _ (Err e)) (Err e)))))
(define-instance (Monad (Result :a))
(define (>>= m f)
(match m
((Ok x) (f x))
((Err e) (Err e)))))
(define-instance (Bifunctor Result)
(define (bimap f g res)
(match res
((Ok x) (Ok (g x)))
((Err e) (Err (f e))))))
(define-instance (Into (Result :a :b) (Optional :b))
(define (into res)
(match res
((Ok x) (Some x))
((Err _) None))))
(define-instance (Into (Optional :b) (Result Unit :b))
(define (into opt)
(match opt
((Some x) (Ok x))
((None) (Err Unit)))))
(define-instance (Iso (Result Unit :a) (Optional :a)))
(define-instance (Unwrappable (Result :a))
(define (unwrap-or-else succeed fail res)
(match res
((Ok elt) (succeed elt))
((Err _) (fail)))))
(define-instance (iter:IntoIterator (Result :err :elt) :elt)
(define (iter:into-iter result)
(match result
((Ok x)
(let cell = (cell:new True))
(iter:with-size
(fn ()
(unless (cell:read cell)
(return None))
(cell:write! cell False)
(Some x))
1))
((Err _)
iter:empty))))
(define-instance (iter:FromIterator :container :elt => iter:FromIterator (Result :err :container) (Result :err :elt))
(define (iter:collect! iter)
(let error = (cell:new None))
(let out =
(iter:collect!
(iter:map-while! (fn (x)
(match x
((Ok x) (Some x))
((Err e)
(cell:write! error (Some e))
None)))
iter)))
(match (cell:read error)
((None) (Ok out))
((Some e) (Err e))))))
#+sb-package-locks
(sb-ext:lock-package "COALTON-LIBRARY/RESULT")