-
Notifications
You must be signed in to change notification settings - Fork 7
/
result.clj
177 lines (141 loc) · 5.01 KB
/
result.clj
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
173
174
175
176
(ns org.purefn.kurosawa.result
"Option monad style exception handling.")
;;------------------------------------------------------------------------------
;; Success
;;------------------------------------------------------------------------------
(deftype Success [value]
Object
(equals [self other]
(if (instance? Success other)
(= value (.-value ^Success other))
false)))
(alter-meta! #'->Success assoc :private true)
(defn succeed
"A successfully computed value."
[value]
(Success. value))
;;------------------------------------------------------------------------------
;; Failure
;;------------------------------------------------------------------------------
(deftype Failure [ex]
Object
(equals [self other]
(if (instance? Failure other)
(= ex (.-ex ^Failure other))
false)))
(alter-meta! #'->Failure assoc :private true)
(defn fail
"The reason (Exception) for the failure."
[ex]
(Failure. ex))
;;------------------------------------------------------------------------------
;; Functions
;;------------------------------------------------------------------------------
(defn success?
"Whether the value is a `Success`."
[result]
(cond
(instance? Failure result) false
(instance? Success result) true
:default nil))
(defn failure?
"Whether the value is a `Failure`."
[result]
(cond
(instance? Failure result) true
(instance? Success result) false
:default nil))
(defn result?
"Whether the value is a `Success` or `Failure`."
[result]
(or (failure? result)
(success? result)))
(defn success
"Returns the value wrapped by the `Success`, or nil."
[result]
(when (instance? Success result)
(.-value ^Success result)))
(defn failure
"Returns the exception wrapped by the `Failure`, or nil."
[result]
(when (instance? Failure result)
(.-ex ^Failure result)))
(defn result
"Returns the value wrapped by the `Success`, the exception wrapped by
the `Failure`, or nil."
[result]
(or (failure result)
(success result)))
(defn attempt
"Attempt a computation which may fail by throwing Exceptions.
Apply the function `f` to the given arguments `args`.
- If successful, returns the resulting value wrapped in a `Success`.
- If an exception occurs, returns the exception wrapped in a `Failure`."
[f & args]
(try (let [result (apply f args)]
(if (result? result)
result
(succeed result)))
(catch Exception e# (fail e#))))
(defn proceed
"Proceed with a computation after a previous `Success`.
If the `result` is:
- a `Failure`, return it as-is.
- a `Success`, return the results of applying the function `f` to the value
wrapped in the `Success` along with any additional `args` provided."
[result f & args]
(if (failure? result)
result
(apply attempt f (success result) args)))
(defn proceed-all
"Proceed with a computation after a multiple previous `Success`s.
In addition to the `result`, one or more of the additional arguments `args`
may also be a Result type. The computation will only proceed if all of these
Result values are a `Success`. If any is a `Failure`, the first encountered
will be returned as-is. Otherwise, all values wrapped in a `Success` will
be extracted and the results of applying the function `f` to these values
will be returned."
[result f & args]
(let [as (concat [result] args)]
(if-let [fr (some (fn [r] (when (failure? r) r)) as)]
fr
(->> as
(map (fn [r] (if (success? r) (success r) r)))
(apply attempt f)))))
(defn recover
"Recover from a previous `Failure`.
If the `result` is:
- a `Failure`, return the result of applying the function `f` to the
exception wrapped in the `Failure` along with any additional arguments
`args` provided.
- a `Success`, return it as-is."
[result f & args]
(if (failure? result)
(apply attempt f (failure result) args)
result))
(defn branch
"Continue computation after a previous `Success` or `Failure`.
If `result` is:
- a `Failure`, return the result of applying the function `f` to the
exception wrapped in the Failure.
- a `Success`, return the result of applying the `g` function to the value
wrapped in the `Success`."
[result f g]
(if (failure? result)
(attempt f (failure result))
(attempt g (success result))))
(defn flatten-results
"Convert a sequence of Results into a Result containing a sequence.
- If any of the elements of the sequence are a `Failure`, then the values
for each of the failures will be returned in a seq inside a single Failure
object. Otherwise a `Success` will be returned containing the unwrapped
(all successful) elements."
[rs]
(if-let [fr (some failure? rs)]
(->> (filter failure? rs)
(map failure)
(fail))
(succeed (map success rs))))
;;------------------------------------------------------------------------------
;; Specs
;;------------------------------------------------------------------------------