@grain/stdlib 0.4.0 → 0.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/result.gr CHANGED
@@ -1,89 +1,235 @@
1
- export let isOk = (v) => {
2
- match (v) {
3
- Ok(_) => true,
4
- _ => false
5
- }
1
+ /**
2
+ * @module Result: Utilities for working with the Result data type.
3
+ *
4
+ * The Result type is an enum that represents the possibility of a success case (with the `Ok` variant),
5
+ * or an error case (with the `Err` variant). Use a Result as the return type of a function that may return an error.
6
+ *
7
+ * @example import Result from "result"
8
+ *
9
+ *
10
+ * @example let success = Ok((x) => 1 + x) // Creates a successful Result containing (x) => 1 + x
11
+ * @example let failure = Err("Something bad happened") // Creates an unsuccessful Result containing "Something bad happened"
12
+ *
13
+ * @since v0.2.0
14
+ */
15
+
16
+ /**
17
+ * @section Values: Functions for working with the Result data type.
18
+ */
19
+
20
+ /**
21
+ * Checks if the Result is the `Ok` variant.
22
+ *
23
+ * @param result: The result to check
24
+ * @returns `true` if the Result is the `Ok` variant or `false` otherwise
25
+ *
26
+ * @since v0.2.0
27
+ */
28
+ export let isOk = result => {
29
+ match (result) {
30
+ Ok(_) => true,
31
+ _ => false,
32
+ }
6
33
  }
7
34
 
8
- export let isErr = (v) => !isOk(v)
35
+ /**
36
+ * Checks if the Result is the `Err` variant.
37
+ *
38
+ * @param result: The result to check
39
+ * @returns `true` if the Result is the `Err` variant or `false` otherwise
40
+ *
41
+ * @since v0.2.0
42
+ */
43
+ export let isErr = result => !isOk(result)
9
44
 
10
- export let toOption = (v) => {
11
- match (v) {
12
- Ok(x) => Some(x),
13
- _ => None
14
- }
45
+ /**
46
+ * Converts the Result to an Option. An error value is discarded and replaced with `None`.
47
+ *
48
+ * @param result: The result to convert
49
+ * @returns `Some(value)` if the Result is `Ok(value)` or `None` if the Result is an `Err`
50
+ *
51
+ * @since v0.2.0
52
+ */
53
+ export let toOption = result => {
54
+ match (result) {
55
+ Ok(x) => Some(x),
56
+ _ => None,
57
+ }
15
58
  }
16
59
 
17
- export let flatMap = (fn, v) => {
18
- match (v) {
19
- Ok(x) => fn(x),
20
- Err(e) => Err(e)
21
- }
60
+ /**
61
+ * If the Result is `Ok(value)`, applies the given function to the `value` to produce a new Result.
62
+ *
63
+ * @param fn: The function to call on the value of an `Ok` variant
64
+ * @param result: The result to map
65
+ * @returns A new Result produced by the mapping function if the variant was `Ok` or the unmodified `Err` otherwise
66
+ *
67
+ * @since v0.2.0
68
+ */
69
+ export let flatMap = (fn, result) => {
70
+ match (result) {
71
+ Ok(x) => fn(x),
72
+ Err(e) => Err(e),
73
+ }
22
74
  }
23
75
 
24
- export let flatMapErr = (fn, v) => {
25
- match (v) {
26
- Err(e) => fn(e),
27
- Ok(x) => Ok(x)
28
- }
76
+ /**
77
+ * If the Result is an `Err(value)`, applies the given function to the `value` to produce a new Result.
78
+ *
79
+ * @param fn: The function to call on the value of an `Err` variant
80
+ * @param result: The result to map
81
+ * @returns A new Result produced by the mapping function if the variant was `Err` or the unmodified `Ok` otherwise
82
+ *
83
+ * @since v0.2.0
84
+ */
85
+ export let flatMapErr = (fn, result) => {
86
+ match (result) {
87
+ Err(e) => fn(e),
88
+ Ok(x) => Ok(x),
89
+ }
29
90
  }
30
91
 
31
- export let map = (fn, v) => {
32
- match (v) {
33
- Ok(x) => Ok(fn(x)),
34
- Err(e) => Err(e)
35
- }
92
+ /**
93
+ * If the Result is `Ok(value)`, applies the given function to the `value` and wraps the new value in an `Ok` variant.
94
+ *
95
+ * @param fn: The function to call on the value of an `Ok` variant
96
+ * @param result: The result to map
97
+ * @returns A new `Ok` variant produced by the mapping function if the variant was `Ok` or the unmodified `Err` otherwise
98
+ *
99
+ * @since v0.2.0
100
+ */
101
+ export let map = (fn, result) => {
102
+ match (result) {
103
+ Ok(x) => Ok(fn(x)),
104
+ Err(e) => Err(e),
105
+ }
36
106
  }
37
107
 
38
- export let mapErr = (fn, v) => {
39
- match (v) {
40
- Err(e) => Err(fn(e)),
41
- Ok(x) => Ok(x)
42
- }
108
+ /**
109
+ * If the Result is `Err(value)`, applies the given function to the `value` and wraps the new value in an `Err` variant.
110
+ *
111
+ * @param fn: The function to call on the value of an `Err` variant
112
+ * @param result: The result to map
113
+ * @returns A new `Err` variant produced by the mapping function if the variant was `Err` or the unmodified `Ok` otherwise
114
+ *
115
+ * @since v0.2.0
116
+ */
117
+ export let mapErr = (fn, result) => {
118
+ match (result) {
119
+ Err(e) => Err(fn(e)),
120
+ Ok(x) => Ok(x),
121
+ }
43
122
  }
44
123
 
45
- export let mapWithDefault = (fn, def, v) => {
46
- match (v) {
47
- Ok(a) => fn(a),
48
- _ => def
49
- }
124
+ /**
125
+ * If the Result is `Ok(value)`, applies the given function to the `value` to produce a new value, otherwise uses the default value.
126
+ * Useful for unwrapping a successful Result while providing a fallback for any errors that can occur.
127
+ *
128
+ * @param fn: The function to call on the value of an `Ok` variant
129
+ * @param def: A fallback value for an `Err` variant
130
+ * @param result: The result to map
131
+ * @returns The value produced by the mapping function if the result is of the `Ok` variant or the default value otherwise
132
+ *
133
+ * @since v0.2.0
134
+ */
135
+ export let mapWithDefault = (fn, def, result) => {
136
+ match (result) {
137
+ Ok(a) => fn(a),
138
+ _ => def,
139
+ }
50
140
  }
51
141
 
52
- export let mapWithDefaultFn = (fnOk, fnErr, v) => {
53
- match (v) {
54
- Ok(a) => fnOk(a),
55
- Err(a) => fnErr(a)
56
- }
142
+ /**
143
+ * If the Result is `Ok(value)`, applies the `fnOk` function to the `value` to produce a new value.
144
+ * If the Result is `Err(value)`, applies the `fnErr` function to the `value` to produce a new value.
145
+ * Useful for unwrapping a Result into a value, whether it is successful or unsuccessful.
146
+ *
147
+ * @param fnOk: The function to call on the value of an `Ok` variant
148
+ * @param fnErr: The function to call on the value of an `Err` variant
149
+ * @param result: The result to map
150
+ * @returns The value produced by one of the mapping functions
151
+ *
152
+ * @since v0.2.0
153
+ */
154
+ export let mapWithDefaultFn = (fnOk, fnErr, result) => {
155
+ match (result) {
156
+ Ok(a) => fnOk(a),
157
+ Err(a) => fnErr(a),
158
+ }
57
159
  }
58
160
 
59
- export let or = (r1, r2) => {
60
- match (r1) {
61
- Ok(x) => r1,
62
- _ => r2
63
- }
161
+ /**
162
+ * Behaves like a logical OR (`||`) where the first Result is only returned if it is the `Ok` variant and falling back to the second Result in all other cases.
163
+ *
164
+ * @param result1: The first result
165
+ * @param result2: The second result
166
+ * @returns The first Result if it is the `Ok` variant or the second Result otherwise
167
+ *
168
+ * @since v0.2.0
169
+ */
170
+ export let or = (result1, result2) => {
171
+ match (result1) {
172
+ Ok(x) => result1,
173
+ _ => result2,
174
+ }
64
175
  }
65
176
 
66
- export let and = (r1, r2) => {
67
- match (r1) {
68
- Ok(_) => r2,
69
- Err(_) => r1
70
- }
177
+ /**
178
+ * Behaves like a logical AND (`&&`) where the first Result is only returned if it is the `Err` variant and falling back to the second Result in all other cases.
179
+ *
180
+ * @param result1: The first result
181
+ * @param result2: The second result
182
+ * @returns The second Result if both are the `Ok` variant or the first Result otherwise
183
+ *
184
+ * @since v0.2.0
185
+ */
186
+ export let and = (result1, result2) => {
187
+ match (result1) {
188
+ Ok(_) => result2,
189
+ Err(_) => result1,
190
+ }
71
191
  }
72
192
 
73
- export let peek = (fnOk, fnErr, r) => {
74
- match (r) {
75
- Ok(x) => ignore(fnOk(x)),
76
- Err(x) => ignore(fnErr(x))
77
- }
78
- r
193
+ /**
194
+ * If the Result is `Ok(value)`, applies the `fnOk` function to the `value` without producing a new value.
195
+ * If the Result is `Err(value)`, applies the `fnErr` function to the `value` without producing a new value.
196
+ * Useful for inspecting Results without changing anything.
197
+ *
198
+ * @param fnOk: The function to call on the value of an `Ok` variant
199
+ * @param fnErr: The function to call on the value of an `Err` variant
200
+ * @param result: The result to inspect
201
+ *
202
+ * @since v0.2.0
203
+ */
204
+ export let peek = (fnOk, fnErr, result) => {
205
+ match (result) {
206
+ Ok(x) => ignore(fnOk(x)),
207
+ Err(x) => ignore(fnErr(x)),
208
+ }
79
209
  }
80
210
 
81
- export let peekOk = (fn, r) => {
82
- peek(fn, identity, r)
211
+ /**
212
+ * If the Result is `Ok(value)`, applies the given function to the `value` without producing a new value.
213
+ *
214
+ * @param fn: The function to call on the value of an `Ok` variant
215
+ * @param result: The result to inspect
216
+ *
217
+ * @since v0.2.0
218
+ */
219
+ export let peekOk = (fn, result) => {
220
+ peek(fn, identity, result)
83
221
  }
84
222
 
85
- export let peekErr = (fn, r) => {
86
- peek(identity, fn, r)
223
+ /**
224
+ * If the Result is `Err(value)`, applies the given function to the `value` without producing a new value.
225
+ *
226
+ * @param fn: The function to call on the value of an `Err` variant
227
+ * @param result: The result to inspect
228
+ *
229
+ * @since v0.2.0
230
+ */
231
+ export let peekErr = (fn, result) => {
232
+ peek(identity, fn, result)
87
233
  }
88
234
 
89
235
  /**
@@ -92,17 +238,17 @@ export let peekErr = (fn, r) => {
92
238
  *
93
239
  * @param msg: The message to prepend if the result contains an `Err`
94
240
  * @param result: The result to extract a value from
95
- * @returns The value inside an `Ok` result
241
+ * @returns The unwrapped value if the Result is the `Ok` variant
96
242
  *
97
- * @example Result.expect("Unexpected error", Ok(1234))
243
+ * @example Result.expect("Unexpected error", Ok(1234)) + 42
98
244
  *
99
245
  * @since v0.4.0
100
246
  */
101
247
  export let expect = (msg, result) => {
102
- match (result) {
103
- Ok(x) => x,
104
- Err(err) => fail msg ++ ": " ++ toString(err)
105
- }
248
+ match (result) {
249
+ Ok(x) => x,
250
+ Err(err) => fail msg ++ ": " ++ toString(err),
251
+ }
106
252
  }
107
253
 
108
254
  /**
@@ -110,12 +256,12 @@ export let expect = (msg, result) => {
110
256
  * exception containing a default message and contents of the `Err`.
111
257
  *
112
258
  * @param result: The result to extract a value from
113
- * @returns The value inside an `Ok` result
259
+ * @returns The unwrapped value if the result is the `Ok` variant
114
260
  *
115
261
  * @example Result.unwrap(Err("This will throw"))
116
262
  *
117
263
  * @since v0.4.0
118
264
  */
119
- export let unwrap = (result) => {
120
- expect("Could not unwrap Err value", result)
265
+ export let unwrap = result => {
266
+ expect("Could not unwrap Err value", result)
121
267
  }