@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/CHANGELOG.md +65 -0
- package/LICENSE +21 -0
- package/README.md +34 -0
- package/array.gr +136 -44
- package/array.md +97 -21
- package/buffer.gr +495 -424
- package/buffer.md +850 -0
- package/bytes.gr +512 -407
- package/bytes.md +621 -0
- package/char.gr +11 -3
- package/hash.gr +26 -3
- package/hash.md +44 -0
- package/list.gr +54 -0
- package/number.gr +24 -6
- package/number.md +49 -17
- package/option.gr +244 -37
- package/option.md +579 -0
- package/package.json +33 -29
- package/queue.gr +98 -29
- package/queue.md +191 -0
- package/range.md +1 -1
- package/regex.gr +3055 -0
- package/regex.md +449 -0
- package/result.gr +216 -70
- package/result.md +446 -0
- package/runtime/gc.gr +2 -2
- package/runtime/string.gr +56 -24
- package/runtime/stringUtils.gr +172 -0
- package/runtime/unsafe/conv.gr +43 -0
- package/set.gr +172 -5
- package/set.md +502 -0
- package/stack.md +143 -0
- package/string.gr +444 -230
- package/string.md +815 -0
- package/sys/file.gr +3 -2
- package/sys/file.md +2 -2
package/result.gr
CHANGED
|
@@ -1,89 +1,235 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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
|
-
|
|
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
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
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
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
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
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
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
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
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
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
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
|
-
|
|
82
|
-
|
|
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
|
-
|
|
86
|
-
|
|
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
|
|
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
|
-
|
|
103
|
-
|
|
104
|
-
|
|
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
|
|
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 =
|
|
120
|
-
|
|
265
|
+
export let unwrap = result => {
|
|
266
|
+
expect("Could not unwrap Err value", result)
|
|
121
267
|
}
|