/
result.gleam
280 lines (264 loc) Β· 5.89 KB
/
result.gleam
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import gleam/list
/// Result represents the result of something that may succeed or not.
/// `Ok` means it was successful, `Error` means it was not successful.
///
pub type Result(success, error) =
Result(success, error)
/// Nil is a type used to represent the absence of something, similar to null
/// or undefined in other languages.
///
/// Unlike some other languages values cannot be implicitly nil.
///
pub type Nil =
Nil
/// Checks whether the result is an Ok value.
///
/// ## Examples
///
/// > is_ok(Ok(1))
/// True
///
/// > is_ok(Error(Nil))
/// False
///
pub fn is_ok(result: Result(a, e)) -> Bool {
case result {
Error(_) -> False
Ok(_) -> True
}
}
/// Checks whether the result is an Error value.
///
/// ## Examples
///
/// > is_error(Ok(1))
/// False
///
/// > is_error(Error(Nil))
/// True
///
pub fn is_error(result: Result(a, e)) -> Bool {
case result {
Ok(_) -> False
Error(_) -> True
}
}
/// Updates a value held within the Ok of a result by calling a given function
/// on it.
///
/// If the result is an Error rather than OK the function is not called and the
/// result stays the same.
///
/// ## Examples
///
/// > map(over: Ok(1), with: fn(x) { x + 1 })
/// Ok(2)
///
/// > map(over: Error(1), with: fn(x) { x + 1 })
/// Error(1)
///
pub fn map(over result: Result(a, e), with fun: fn(a) -> b) -> Result(b, e) {
case result {
Ok(x) -> Ok(fun(x))
Error(e) -> Error(e)
}
}
/// Updates a value held within the Error of a result by calling a given function
/// on it.
///
/// If the result is Ok rather than Error the function is not called and the
/// result stays the same.
///
/// ## Examples
///
/// > map_error(over: Error(1), with: fn(x) { x + 1 })
/// Error(2)
///
/// > map_error(over: Ok(1), with: fn(x) { x + 1 })
/// Ok(1)
///
pub fn map_error(
over result: Result(a, e),
with fun: fn(e) -> f,
) -> Result(a, f) {
case result {
Ok(x) -> Ok(x)
Error(error) -> Error(fun(error))
}
}
/// Merges a nested Result into a single layer.
///
/// ## Examples
///
/// > flatten(Ok(Ok(1)))
/// Ok(1)
///
/// > flatten(Ok(Error(""))
/// Error("")
///
/// > flatten(Error(Nil))
/// Error(Nil)
///
pub fn flatten(result: Result(Result(a, e), e)) -> Result(a, e) {
case result {
Ok(x) -> x
Error(error) -> Error(error)
}
}
/// Updates a value held within the Ok of a result by calling a given function
/// on it, where the given function also returns a result. The two results are
/// then merged together into one result.
///
/// If the result is an Error rather than OK the function is not called and the
/// result stays the same.
///
/// This function is the equivalent of calling `map` followed by `flatten`, and
/// it is useful for chaining together multiple functions that may fail.
///
/// ## Examples
///
/// > then(Ok(1), fn(x) { Ok(x + 1) })
/// Ok(2)
///
/// > then(Ok(1), fn(x) { Ok(#("a", x)) })
/// Ok(#("a", 1))
///
/// > then(Ok(1), fn(x) { Error("Oh no") })
/// Error("Oh no")
///
/// > then(Error(Nil), fn(x) { Ok(x + 1) })
/// Error(Nil)
///
pub fn then(
result: Result(a, e),
apply fun: fn(a) -> Result(b, e),
) -> Result(b, e) {
case result {
Ok(x) -> fun(x)
Error(e) -> Error(e)
}
}
/// Extracts the Ok value from a result, returning a default value if the result
/// is an Error.
///
/// ## Examples
///
/// > unwrap(Ok(1), 0)
/// 1
///
/// > unwrap(Error(""), 0)
/// 0
///
pub fn unwrap(result: Result(a, e), or default: a) -> a {
case result {
Ok(v) -> v
Error(_) -> default
}
}
/// Extracts the Ok value from a result, evaluating the default function if the result
/// is an Error.
///
/// ## Examples
///
/// > lazy_unwrap(Ok(1), fn() { 0 })
/// 1
///
/// > lazy_unwrap(Error(""), fn() { 0 })
/// 0
///
pub fn lazy_unwrap(result: Result(a, e), or default: fn() -> a) -> a {
case result {
Ok(v) -> v
Error(_) -> default()
}
}
/// Transforms any error into Error(Nil)
///
/// ## Examples
///
/// > nil_error(Error(1))
/// Error(Nil)
///
/// > nil_error(Ok(1))
/// Ok(1)
///
pub fn nil_error(result: Result(a, e)) -> Result(a, Nil) {
map_error(result, fn(_) { Nil })
}
/// Returns the first value if it is Ok, otherwise return the second value.
///
/// ## Examples
///
/// > or(Ok(1), Ok(2))
/// Ok(1)
///
/// > or(Ok(1), Error("Error 2"))
/// Ok(1)
///
/// > or(Error("Error 1"), Ok(2))
/// Ok(2)
///
/// > or(Error("Error 1"), Error("Error 2"))
/// Error("Error 2")
///
pub fn or(first: Result(a, e), second: Result(a, e)) -> Result(a, e) {
case first {
Ok(_) -> first
Error(_) -> second
}
}
/// Returns the first value if it is Ok, otherwise evaluates the given function for a fallback value.
///
/// ## Examples
///
/// > or(Ok(1), Ok(2))
/// Ok(1)
///
/// > or(Ok(1), Error("Error 2"))
/// Ok(1)
///
/// > or(Error("Error 1"), Ok(2))
/// Ok(2)
///
/// > or(Error("Error 1"), Error("Error 2"))
/// Error("Error 2")
///
pub fn lazy_or(
first: Result(a, e),
second: fn() -> Result(a, e),
) -> Result(a, e) {
case first {
Ok(_) -> first
Error(_) -> second()
}
}
/// Combines a list of results into a single result.
/// If all elements in the list are Ok then returns an Ok holding the list of values.
/// If any element is Error then returns the first error.
///
/// ## Examples
/// > all([Ok(1), Ok(2)])
/// Ok([1, 2])
///
/// > all([Ok(1), Error("e")])
/// Error("e")
pub fn all(results: List(Result(a, e))) -> Result(List(a), e) {
list.try_map(results, fn(x) { x })
}
pub fn replace_error(result: Result(a, e1), error: e2) -> Result(a, e2) {
result
|> map_error(fn(_) { error })
}
/// Given a list of results
/// Return only the values inside Ok
///
/// ## Examples
///
/// ```
/// > values([Ok(1), None, Ok(3)])
/// [1, 3]
/// ```
///
pub fn values(results: List(Result(a, e))) -> List(a) {
list.filter_map(results, fn(r) { r })
}