-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote_data.gleam
322 lines (304 loc) · 6.93 KB
/
remote_data.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
//// A type that represents a remote data fetch
import gleam/list
import gleam/option.{type Option, None, Some}
pub type RemoteData(a, error) {
NotAsked
Loading
Failure(error)
Success(a)
}
/// Map a function over the success value of a RemoteData
///
/// ## Examples
/// ```gleam
/// map(over: Success(42), with: fn(x) { x * 2 })
/// // -> Success(84)
/// ```
///
/// ```gleam
/// map(over: Failure("error"), with: fn(x) { x * 2 })
/// // -> Failure("error")
/// ```
pub fn map(
over data: RemoteData(a, error),
with mapper: fn(a) -> b,
) -> RemoteData(b, error) {
case data {
NotAsked -> NotAsked
Loading -> Loading
Failure(error) -> Failure(error)
Success(a) -> Success(mapper(a))
}
}
/// Map a function over the success value of two RemoteData values
///
/// ## Examples
/// ```gleam
/// map_2(over: Success(42), over_2: Success(2), with: fn(a, b) { a + b })
/// // -> Success(44)
/// ```
///
/// ```gleam
/// map_2(over: Failure("error"), over_2: Success(2), with: fn(a, b) { a + b })
/// // -> Failure("error")
/// ```
pub fn map2(
over data1: RemoteData(a, error),
over2 data2: RemoteData(b, error),
with mapper: fn(a, b) -> c,
) -> RemoteData(c, error) {
case data1, data2 {
Success(a), Success(b) -> Success(mapper(a, b))
Failure(error), _ | _, Failure(error) -> Failure(error)
NotAsked, _ | _, NotAsked -> NotAsked
Loading, _ | _, Loading -> Loading
}
}
/// The same as `map2`, but with three RemoteData values
/// Check `map2` for more details
pub fn map3(
over data1: RemoteData(a, error),
over_2 data2: RemoteData(b, error),
over_3 data3: RemoteData(c, error),
with mapper: fn(a, b, c) -> d,
) -> RemoteData(d, error) {
case data1, data2, data3 {
Success(a), Success(b), Success(c) -> Success(mapper(a, b, c))
Failure(error), _, _ | _, Failure(error), _ | _, _, Failure(error) ->
Failure(error)
NotAsked, _, _ | _, NotAsked, _ | _, _, NotAsked -> NotAsked
Loading, _, _ | _, Loading, _ | _, _, Loading -> Loading
}
}
/// Map a function over the error value of a RemoteData
///
/// ## Examples
/// ```gleam
/// map_error(over: Success(42), with: fn(_) { "error" })
/// // -> Success(42)
/// ```
///
/// ```gleam
/// map_error(over: Failure(42), with: fn(_) { "error" })
/// // -> Failure("error")
/// ```
pub fn map_error(
over data: RemoteData(a, error),
with mapper: fn(error) -> error_b,
) -> RemoteData(a, error_b) {
case data {
NotAsked -> NotAsked
Loading -> Loading
Failure(error) -> Failure(mapper(error))
Success(a) -> Success(a)
}
}
/// Chain a function that returns a RemoteData over the success value of a RemoteData
///
/// ## Examples
/// ```gleam
/// and_then(over: Success(42), with: fn(x) { Success(x * 2) })
/// // -> Success(84)
/// ```
///
/// ```gleam
/// and_then(over: Failure("error"), with: fn(x) { Success(x * 2) })
/// // -> Failure("error")
/// ```
///
/// ```gleam
/// and_then(over: Success(42), with: fn(x) { Failure("error") })
/// // -> Failure("error")
/// ```
pub fn try(
over data: RemoteData(a, error),
with mapper: fn(a) -> RemoteData(b, error),
) -> RemoteData(b, error) {
case data {
NotAsked -> NotAsked
Loading -> Loading
Failure(error) -> Failure(error)
Success(a) -> mapper(a)
}
}
/// Unwrap a RemoteData, providing a default value if the data is not Success
/// ## Examples
/// ```gleam
/// unwrap(data: Success(42), or: 0)
/// // -> 42
/// ```
///
/// ```gleam
/// unwrap(data: Failure("error"), or: 0)
/// // -> 0
/// ```
pub fn unwrap(data: RemoteData(a, error), or default: a) -> a {
case data {
Success(a) -> a
_ -> default
}
}
/// Convert a RemoteData to an Option
/// ## Examples
/// ```gleam
/// to_option(data: Success(42))
/// // -> Some(42)
/// ```
///
/// ```gleam
/// to_option(data: Failure("error"))
/// // -> None
/// ```
pub fn to_option(data: RemoteData(a, error)) -> Option(a) {
case data {
Success(a) -> Some(a)
_ -> None
}
}
/// Convert an Option to a RemoteData
/// ## Examples
/// ```gleam
/// from_option(option: Some(42), or: "error")
/// // -> Success(42)
/// ```
///
/// ```gleam
/// from_option(option: None, or: "error")
/// // -> Failure("error")
/// ```
pub fn from_option(option: Option(a), or error: error) -> RemoteData(a, error) {
case option {
Some(a) -> Success(a)
None -> Failure(error)
}
}
/// Convert a RemoteData to a Result
/// If the data is NotAsked or Loading, it will be converted to an Error with the provided error
/// ## Examples
/// ```gleam
/// to_result(data: Success(42), or: "error")
/// // -> Ok(42)
/// ```
///
/// ```gleam
/// to_result(data: Failure("error"), or: "another error")
/// // -> Error("error")
/// ```
///
/// ```gleam
/// to_result(data: Loading, or: "another error")
/// // -> Error("another error")
/// ```
pub fn to_result(
data: RemoteData(a, error),
or error: error,
) -> Result(a, error) {
case data {
Success(a) -> Ok(a)
Failure(error) -> Error(error)
_ -> Error(error)
}
}
/// Convert a Result to a RemoteData
/// ## Examples
/// ```gleam
/// from_result(result: Ok(42))
/// // -> Success(42)
/// ```
///
/// ```gleam
/// from_result(result: Error("error"))
/// // -> Failure("error")
/// ```
pub fn from_result(result: Result(a, error)) -> RemoteData(a, error) {
case result {
Ok(a) -> Success(a)
Error(error) -> Failure(error)
}
}
/// Convert a list of RemoteData to a RemoteData of a list
/// ## Examples
/// ```gleam
/// from_list([Success(42), Success(43)])
/// // -> Success([42, 43])
/// ```
///
/// ```gleam
/// from_list([Success(42), Failure("error")])
/// // -> Failure("error")
/// ```
pub fn from_list(
data_list: List(RemoteData(a, error)),
) -> RemoteData(List(a), error) {
use acc, data <- list.fold(data_list, Success([]))
use acc_value, data_value <- map2(acc, data)
list.append(acc_value, [data_value])
}
/// Check if a RemoteData is a Success
/// ## Examples
/// ```gleam
/// is_not_asked(Success(42))
/// // -> False
/// ```
///
/// ```gleam
/// is_not_asked(NotAsked)
/// // -> True
/// ```
pub fn is_not_asked(data: RemoteData(_, _)) -> Bool {
case data {
NotAsked -> True
_ -> False
}
}
/// Check if a RemoteData is a Success
/// ## Examples
/// ```gleam
/// is_loading(Success(42))
/// // -> True
/// ```
///
/// ```gleam
/// is_loading(Failure("error"))
/// // -> False
/// ```
pub fn is_loading(data: RemoteData(_, _)) -> Bool {
case data {
Loading -> True
_ -> False
}
}
/// Check if a RemoteData is a Success
/// ## Examples
/// ```gleam
/// is_failure(Success(42))
/// // -> True
/// ```
///
/// ```gleam
/// is_failure(Failure("error"))
/// // -> False
/// ```
pub fn is_failure(data: RemoteData(_, _)) -> Bool {
case data {
Failure(_) -> True
_ -> False
}
}
/// Check if a RemoteData is a Success
/// ## Examples
/// ```gleam
/// is_success(Success(42))
/// // -> True
/// ```
///
/// ```gleam
/// is_success(Failure("error"))
/// // -> False
/// ```
pub fn is_success(data: RemoteData(_, _)) -> Bool {
case data {
Success(_) -> True
_ -> False
}
}