@grain/stdlib 0.4.2 → 0.4.6
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 +52 -0
- package/LICENSE +1 -1
- package/array.gr +200 -89
- package/array.md +81 -5
- package/buffer.gr +93 -36
- package/bytes.gr +10 -10
- package/char.gr +112 -56
- package/char.md +200 -0
- package/float32.gr +120 -4
- package/float32.md +315 -0
- package/float64.gr +120 -4
- package/float64.md +315 -0
- package/hash.gr +42 -15
- package/hash.md +44 -0
- package/int32.gr +370 -75
- package/int32.md +833 -0
- package/int64.gr +370 -75
- package/int64.md +833 -0
- package/list.gr +121 -50
- package/map.gr +106 -110
- package/number.gr +37 -1
- package/number.md +66 -0
- package/option.gr +260 -53
- package/option.md +579 -0
- package/package.json +1 -1
- package/pervasives.gr +32 -20
- package/queue.gr +102 -30
- package/queue.md +191 -0
- package/range.gr +26 -26
- package/range.md +1 -1
- package/regex.md +9 -9
- package/result.gr +216 -70
- package/result.md +446 -0
- package/runtime/dataStructures.gr +28 -29
- package/runtime/debug.gr +0 -1
- package/runtime/equal.gr +37 -16
- package/runtime/exception.gr +28 -15
- package/runtime/gc.gr +33 -20
- package/runtime/malloc.gr +19 -11
- package/runtime/numberUtils.gr +208 -103
- package/runtime/numbers.gr +217 -118
- package/runtime/string.gr +98 -39
- package/runtime/stringUtils.gr +176 -0
- package/runtime/unsafe/conv.gr +10 -10
- package/runtime/unsafe/memory.gr +14 -3
- package/runtime/unsafe/printWasm.gr +4 -4
- package/runtime/unsafe/tags.gr +2 -2
- package/runtime/unsafe/wasmf32.gr +9 -2
- package/runtime/unsafe/wasmf64.gr +9 -2
- package/runtime/unsafe/wasmi32.gr +65 -47
- package/runtime/unsafe/wasmi64.gr +78 -50
- package/runtime/wasi.gr +199 -45
- package/set.gr +281 -119
- package/set.md +502 -0
- package/stack.gr +26 -26
- package/string.gr +657 -341
- package/string.md +815 -0
- package/sys/file.gr +356 -177
- package/sys/process.gr +10 -6
- package/sys/random.gr +3 -6
- package/sys/time.gr +3 -3
package/option.md
ADDED
|
@@ -0,0 +1,579 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Option
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Utilities for working with the Option data type.
|
|
6
|
+
|
|
7
|
+
The Option type is an enum that represents the possibility of something being present (with the `Some` variant), or not (with the `None` variant). There’s no standalone `null` or `nil` type in Grain; use an Option where you would normally reach for `null` or `nil`.
|
|
8
|
+
|
|
9
|
+
<details disabled>
|
|
10
|
+
<summary tabindex="-1">Added in <code>0.2.0</code></summary>
|
|
11
|
+
No other changes yet.
|
|
12
|
+
</details>
|
|
13
|
+
|
|
14
|
+
```grain
|
|
15
|
+
import Option from "option"
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
```grain
|
|
19
|
+
let hasValue = Some(1234) // Creates an Option containing 1234
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
```grain
|
|
23
|
+
let noValue = None // Creates an Option containing nothing
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Values
|
|
27
|
+
|
|
28
|
+
Functions for working with the Option data type.
|
|
29
|
+
|
|
30
|
+
### Option.**isSome**
|
|
31
|
+
|
|
32
|
+
<details disabled>
|
|
33
|
+
<summary tabindex="-1">Added in <code>0.2.0</code></summary>
|
|
34
|
+
No other changes yet.
|
|
35
|
+
</details>
|
|
36
|
+
|
|
37
|
+
```grain
|
|
38
|
+
isSome : Option<a> -> Bool
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Checks if the Option is the `Some` variant.
|
|
42
|
+
|
|
43
|
+
Parameters:
|
|
44
|
+
|
|
45
|
+
|param|type|description|
|
|
46
|
+
|-----|----|-----------|
|
|
47
|
+
|`option`|`Option<a>`|The option to check|
|
|
48
|
+
|
|
49
|
+
Returns:
|
|
50
|
+
|
|
51
|
+
|type|description|
|
|
52
|
+
|----|-----------|
|
|
53
|
+
|`Bool`|`true` if the Option is the `Some` variant or `false` otherwise|
|
|
54
|
+
|
|
55
|
+
### Option.**isNone**
|
|
56
|
+
|
|
57
|
+
<details disabled>
|
|
58
|
+
<summary tabindex="-1">Added in <code>0.2.0</code></summary>
|
|
59
|
+
No other changes yet.
|
|
60
|
+
</details>
|
|
61
|
+
|
|
62
|
+
```grain
|
|
63
|
+
isNone : Option<a> -> Bool
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Checks if the Option is the `None` variant.
|
|
67
|
+
|
|
68
|
+
Parameters:
|
|
69
|
+
|
|
70
|
+
|param|type|description|
|
|
71
|
+
|-----|----|-----------|
|
|
72
|
+
|`option`|`Option<a>`|The option to check|
|
|
73
|
+
|
|
74
|
+
Returns:
|
|
75
|
+
|
|
76
|
+
|type|description|
|
|
77
|
+
|----|-----------|
|
|
78
|
+
|`Bool`|`true` if the Option is the `None` variant or `false` otherwise|
|
|
79
|
+
|
|
80
|
+
### Option.**contains**
|
|
81
|
+
|
|
82
|
+
<details disabled>
|
|
83
|
+
<summary tabindex="-1">Added in <code>0.2.0</code></summary>
|
|
84
|
+
No other changes yet.
|
|
85
|
+
</details>
|
|
86
|
+
|
|
87
|
+
```grain
|
|
88
|
+
contains : (a, Option<a>) -> Bool
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Checks if the Option is the `Some` variant and contains the given value. Uses the generic `==` equality operator.
|
|
92
|
+
|
|
93
|
+
Parameters:
|
|
94
|
+
|
|
95
|
+
|param|type|description|
|
|
96
|
+
|-----|----|-----------|
|
|
97
|
+
|`value`|`a`|The value to search for|
|
|
98
|
+
|`option`|`Option<a>`|The option to search|
|
|
99
|
+
|
|
100
|
+
Returns:
|
|
101
|
+
|
|
102
|
+
|type|description|
|
|
103
|
+
|----|-----------|
|
|
104
|
+
|`Bool`|`true` if the Option is equivalent to `Some(value)` or `false` otherwise|
|
|
105
|
+
|
|
106
|
+
### Option.**expect**
|
|
107
|
+
|
|
108
|
+
<details disabled>
|
|
109
|
+
<summary tabindex="-1">Added in <code>0.2.0</code></summary>
|
|
110
|
+
No other changes yet.
|
|
111
|
+
</details>
|
|
112
|
+
|
|
113
|
+
```grain
|
|
114
|
+
expect : (String, Option<a>) -> a
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
Extracts the value inside a `Some` option, otherwise throws an
|
|
118
|
+
exception containing the message provided.
|
|
119
|
+
|
|
120
|
+
Parameters:
|
|
121
|
+
|
|
122
|
+
|param|type|description|
|
|
123
|
+
|-----|----|-----------|
|
|
124
|
+
|`msg`|`String`|The message to use upon failure|
|
|
125
|
+
|`option`|`Option<a>`|The option to extract a value from|
|
|
126
|
+
|
|
127
|
+
Returns:
|
|
128
|
+
|
|
129
|
+
|type|description|
|
|
130
|
+
|----|-----------|
|
|
131
|
+
|`a`|The unwrapped value if the Option is the `Some` variant|
|
|
132
|
+
|
|
133
|
+
### Option.**unwrap**
|
|
134
|
+
|
|
135
|
+
<details disabled>
|
|
136
|
+
<summary tabindex="-1">Added in <code>0.2.0</code></summary>
|
|
137
|
+
No other changes yet.
|
|
138
|
+
</details>
|
|
139
|
+
|
|
140
|
+
```grain
|
|
141
|
+
unwrap : Option<a> -> a
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
Extracts the value inside a `Some` option, otherwise
|
|
145
|
+
throws an exception containing a default message.
|
|
146
|
+
|
|
147
|
+
Parameters:
|
|
148
|
+
|
|
149
|
+
|param|type|description|
|
|
150
|
+
|-----|----|-----------|
|
|
151
|
+
|`option`|`Option<a>`|The option to extract the value from|
|
|
152
|
+
|
|
153
|
+
Returns:
|
|
154
|
+
|
|
155
|
+
|type|description|
|
|
156
|
+
|----|-----------|
|
|
157
|
+
|`a`|The unwrapped value if the Option is the `Some` variant|
|
|
158
|
+
|
|
159
|
+
### Option.**unwrapWithDefault**
|
|
160
|
+
|
|
161
|
+
<details disabled>
|
|
162
|
+
<summary tabindex="-1">Added in <code>0.2.0</code></summary>
|
|
163
|
+
No other changes yet.
|
|
164
|
+
</details>
|
|
165
|
+
|
|
166
|
+
```grain
|
|
167
|
+
unwrapWithDefault : (a, Option<a>) -> a
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
Extracts the value inside a `Some` option or provide the default value if `None`.
|
|
171
|
+
|
|
172
|
+
Parameters:
|
|
173
|
+
|
|
174
|
+
|param|type|description|
|
|
175
|
+
|-----|----|-----------|
|
|
176
|
+
|`default`|`a`|The default value|
|
|
177
|
+
|`option`|`Option<a>`|The option to unwrap|
|
|
178
|
+
|
|
179
|
+
Returns:
|
|
180
|
+
|
|
181
|
+
|type|description|
|
|
182
|
+
|----|-----------|
|
|
183
|
+
|`a`|The unwrapped value if the Option is the `Some` variant or the default value otherwise|
|
|
184
|
+
|
|
185
|
+
### Option.**map**
|
|
186
|
+
|
|
187
|
+
<details disabled>
|
|
188
|
+
<summary tabindex="-1">Added in <code>0.2.0</code></summary>
|
|
189
|
+
No other changes yet.
|
|
190
|
+
</details>
|
|
191
|
+
|
|
192
|
+
```grain
|
|
193
|
+
map : ((a -> b), Option<a>) -> Option<b>
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
If the Option is `Some(value)`, applies the given function to the `value` and wraps the new value in a `Some` variant.
|
|
197
|
+
|
|
198
|
+
Parameters:
|
|
199
|
+
|
|
200
|
+
|param|type|description|
|
|
201
|
+
|-----|----|-----------|
|
|
202
|
+
|`fn`|`a -> b`|The function to call on the value of a `Some` variant|
|
|
203
|
+
|`option`|`Option<a>`|The option to map|
|
|
204
|
+
|
|
205
|
+
Returns:
|
|
206
|
+
|
|
207
|
+
|type|description|
|
|
208
|
+
|----|-----------|
|
|
209
|
+
|`Option<b>`|A new `Some` variant produced by the mapping function if the variant was `Some` or the unmodified `None` otherwise|
|
|
210
|
+
|
|
211
|
+
### Option.**mapWithDefault**
|
|
212
|
+
|
|
213
|
+
<details disabled>
|
|
214
|
+
<summary tabindex="-1">Added in <code>0.2.0</code></summary>
|
|
215
|
+
No other changes yet.
|
|
216
|
+
</details>
|
|
217
|
+
|
|
218
|
+
```grain
|
|
219
|
+
mapWithDefault : ((a -> b), b, Option<a>) -> b
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
If the Option is `Some(value)`, applies the given function to the `value` to produce a new value, otherwise uses the default value.
|
|
223
|
+
Useful for unwrapping an Option while providing a fallback for any `None` variants.
|
|
224
|
+
|
|
225
|
+
Parameters:
|
|
226
|
+
|
|
227
|
+
|param|type|description|
|
|
228
|
+
|-----|----|-----------|
|
|
229
|
+
|`fn`|`a -> b`|The function to call on the value of a `Some` variant|
|
|
230
|
+
|`default`|`b`|A fallback value for a `None` variant|
|
|
231
|
+
|`option`|`Option<a>`|The option to map|
|
|
232
|
+
|
|
233
|
+
Returns:
|
|
234
|
+
|
|
235
|
+
|type|description|
|
|
236
|
+
|----|-----------|
|
|
237
|
+
|`b`|The value produced by the mapping function if the Option is of the `Some` variant or the default value otherwise|
|
|
238
|
+
|
|
239
|
+
### Option.**mapWithDefaultFn**
|
|
240
|
+
|
|
241
|
+
<details disabled>
|
|
242
|
+
<summary tabindex="-1">Added in <code>0.2.0</code></summary>
|
|
243
|
+
No other changes yet.
|
|
244
|
+
</details>
|
|
245
|
+
|
|
246
|
+
```grain
|
|
247
|
+
mapWithDefaultFn : ((a -> b), (() -> b), Option<a>) -> b
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
If the Option is `Some(value)`, applies the `fn` function to the `value` to produce a new value.
|
|
251
|
+
If the Option is `None`, calls the `defaultFn` function to produce a new value.
|
|
252
|
+
Useful for unwrapping an Option into a value, whether it is `Some` or `None`.
|
|
253
|
+
|
|
254
|
+
Parameters:
|
|
255
|
+
|
|
256
|
+
|param|type|description|
|
|
257
|
+
|-----|----|-----------|
|
|
258
|
+
|`fn`|`a -> b`|The function to call on the value of a `Some` variant|
|
|
259
|
+
|`defaultFn`|`() -> b`|The default function|
|
|
260
|
+
|`option`|`Option<a>`|The option to map|
|
|
261
|
+
|
|
262
|
+
Returns:
|
|
263
|
+
|
|
264
|
+
|type|description|
|
|
265
|
+
|----|-----------|
|
|
266
|
+
|`b`|The value produced by one of the mapping functions|
|
|
267
|
+
|
|
268
|
+
### Option.**flatMap**
|
|
269
|
+
|
|
270
|
+
<details disabled>
|
|
271
|
+
<summary tabindex="-1">Added in <code>0.2.0</code></summary>
|
|
272
|
+
No other changes yet.
|
|
273
|
+
</details>
|
|
274
|
+
|
|
275
|
+
```grain
|
|
276
|
+
flatMap : ((a -> Option<b>), Option<a>) -> Option<b>
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
If the Option is `Some(value)`, applies the given function to the `value` to produce a new Option.
|
|
280
|
+
|
|
281
|
+
Parameters:
|
|
282
|
+
|
|
283
|
+
|param|type|description|
|
|
284
|
+
|-----|----|-----------|
|
|
285
|
+
|`fn`|`a -> Option<b>`|The function to call on the value of a `Some` variant|
|
|
286
|
+
|`option`|`Option<a>`|The option to map|
|
|
287
|
+
|
|
288
|
+
Returns:
|
|
289
|
+
|
|
290
|
+
|type|description|
|
|
291
|
+
|----|-----------|
|
|
292
|
+
|`Option<b>`|A new Option produced by the mapping function if the variant was `Some` or the unmodified `None` otherwise|
|
|
293
|
+
|
|
294
|
+
### Option.**filter**
|
|
295
|
+
|
|
296
|
+
<details disabled>
|
|
297
|
+
<summary tabindex="-1">Added in <code>0.2.0</code></summary>
|
|
298
|
+
No other changes yet.
|
|
299
|
+
</details>
|
|
300
|
+
|
|
301
|
+
```grain
|
|
302
|
+
filter : ((a -> Bool), Option<a>) -> Option<a>
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
Converts `Some(value)` variants to `None` variants where the predicate function returns `false`.
|
|
306
|
+
if the `fn` return `true` returns `Some(value)`, otherwise returns `None`.
|
|
307
|
+
|
|
308
|
+
Parameters:
|
|
309
|
+
|
|
310
|
+
|param|type|description|
|
|
311
|
+
|-----|----|-----------|
|
|
312
|
+
|`fn`|`a -> Bool`|The predicate function to indicate if the option should remain `Some`|
|
|
313
|
+
|`option`|`Option<a>`|The option to inspect|
|
|
314
|
+
|
|
315
|
+
Returns:
|
|
316
|
+
|
|
317
|
+
|type|description|
|
|
318
|
+
|----|-----------|
|
|
319
|
+
|`Option<a>`|`Some(value)` if the variant was `Some` and the predicate returns `true` or `None` otherwise|
|
|
320
|
+
|
|
321
|
+
### Option.**zip**
|
|
322
|
+
|
|
323
|
+
<details disabled>
|
|
324
|
+
<summary tabindex="-1">Added in <code>0.2.0</code></summary>
|
|
325
|
+
No other changes yet.
|
|
326
|
+
</details>
|
|
327
|
+
|
|
328
|
+
```grain
|
|
329
|
+
zip : (Option<a>, Option<b>) -> Option<(a, b)>
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
Combine two Options into a single Option containing a tuple of their values.
|
|
333
|
+
|
|
334
|
+
Parameters:
|
|
335
|
+
|
|
336
|
+
|param|type|description|
|
|
337
|
+
|-----|----|-----------|
|
|
338
|
+
|`optionA`|`Option<a>`|The first option to combine|
|
|
339
|
+
|`optionB`|`Option<b>`|The second option to combine|
|
|
340
|
+
|
|
341
|
+
Returns:
|
|
342
|
+
|
|
343
|
+
|type|description|
|
|
344
|
+
|----|-----------|
|
|
345
|
+
|`Option<(a, b)>`|`Some((valueA, valueB))` if both Options are `Some` variants or `None` otherwise|
|
|
346
|
+
|
|
347
|
+
### Option.**zipWith**
|
|
348
|
+
|
|
349
|
+
<details disabled>
|
|
350
|
+
<summary tabindex="-1">Added in <code>0.2.0</code></summary>
|
|
351
|
+
No other changes yet.
|
|
352
|
+
</details>
|
|
353
|
+
|
|
354
|
+
```grain
|
|
355
|
+
zipWith : (((a, b) -> c), Option<a>, Option<b>) -> Option<c>
|
|
356
|
+
```
|
|
357
|
+
|
|
358
|
+
Combine two Options into a single Option. The new value is produced by applying the given function to both values.
|
|
359
|
+
|
|
360
|
+
Parameters:
|
|
361
|
+
|
|
362
|
+
|param|type|description|
|
|
363
|
+
|-----|----|-----------|
|
|
364
|
+
|`fn`|`(a, b) -> c`|The function to generate a new value|
|
|
365
|
+
|`optionA`|`Option<a>`|The first option to combine|
|
|
366
|
+
|`optionB`|`Option<b>`|The second option to combine|
|
|
367
|
+
|
|
368
|
+
Returns:
|
|
369
|
+
|
|
370
|
+
|type|description|
|
|
371
|
+
|----|-----------|
|
|
372
|
+
|`Option<c>`|`Some(newValue)` if both Options are `Some` variants or `None` otherwise|
|
|
373
|
+
|
|
374
|
+
### Option.**flatten**
|
|
375
|
+
|
|
376
|
+
<details disabled>
|
|
377
|
+
<summary tabindex="-1">Added in <code>0.2.0</code></summary>
|
|
378
|
+
No other changes yet.
|
|
379
|
+
</details>
|
|
380
|
+
|
|
381
|
+
```grain
|
|
382
|
+
flatten : Option<Option<a>> -> Option<a>
|
|
383
|
+
```
|
|
384
|
+
|
|
385
|
+
Flattens nested Options.
|
|
386
|
+
|
|
387
|
+
Parameters:
|
|
388
|
+
|
|
389
|
+
|param|type|description|
|
|
390
|
+
|-----|----|-----------|
|
|
391
|
+
|`option`|`Option<Option<a>>`|The option to flatten|
|
|
392
|
+
|
|
393
|
+
Returns:
|
|
394
|
+
|
|
395
|
+
|type|description|
|
|
396
|
+
|----|-----------|
|
|
397
|
+
|`Option<a>`|`Some(innerValue)` if all nested options were the `Some` variant or `None` otherwise|
|
|
398
|
+
|
|
399
|
+
Examples:
|
|
400
|
+
|
|
401
|
+
```grain
|
|
402
|
+
Option.flatten(Some(Some(1))) == Some(1)
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
### Option.**toList**
|
|
406
|
+
|
|
407
|
+
<details disabled>
|
|
408
|
+
<summary tabindex="-1">Added in <code>0.2.0</code></summary>
|
|
409
|
+
No other changes yet.
|
|
410
|
+
</details>
|
|
411
|
+
|
|
412
|
+
```grain
|
|
413
|
+
toList : Option<a> -> List<a>
|
|
414
|
+
```
|
|
415
|
+
|
|
416
|
+
Converts an Option to a list with either zero or one item.
|
|
417
|
+
|
|
418
|
+
Parameters:
|
|
419
|
+
|
|
420
|
+
|param|type|description|
|
|
421
|
+
|-----|----|-----------|
|
|
422
|
+
|`option`|`Option<a>`|The option to convert|
|
|
423
|
+
|
|
424
|
+
Returns:
|
|
425
|
+
|
|
426
|
+
|type|description|
|
|
427
|
+
|----|-----------|
|
|
428
|
+
|`List<a>`|`[value]` if the Option was the `Some` variant or `[]` otherwise|
|
|
429
|
+
|
|
430
|
+
### Option.**toArray**
|
|
431
|
+
|
|
432
|
+
<details disabled>
|
|
433
|
+
<summary tabindex="-1">Added in <code>0.2.0</code></summary>
|
|
434
|
+
No other changes yet.
|
|
435
|
+
</details>
|
|
436
|
+
|
|
437
|
+
```grain
|
|
438
|
+
toArray : Option<a> -> Array<a>
|
|
439
|
+
```
|
|
440
|
+
|
|
441
|
+
Converts an Option to an array with either zero or one item.
|
|
442
|
+
|
|
443
|
+
Parameters:
|
|
444
|
+
|
|
445
|
+
|param|type|description|
|
|
446
|
+
|-----|----|-----------|
|
|
447
|
+
|`option`|`Option<a>`|The option to convert|
|
|
448
|
+
|
|
449
|
+
Returns:
|
|
450
|
+
|
|
451
|
+
|type|description|
|
|
452
|
+
|----|-----------|
|
|
453
|
+
|`Array<a>`|`[> value]` if the Option was the `Some` variant or `[> ]` otherwise|
|
|
454
|
+
|
|
455
|
+
### Option.**toResult**
|
|
456
|
+
|
|
457
|
+
<details disabled>
|
|
458
|
+
<summary tabindex="-1">Added in <code>0.2.0</code></summary>
|
|
459
|
+
No other changes yet.
|
|
460
|
+
</details>
|
|
461
|
+
|
|
462
|
+
```grain
|
|
463
|
+
toResult : (a, Option<b>) -> Result<b, a>
|
|
464
|
+
```
|
|
465
|
+
|
|
466
|
+
Converts the Option to a Result, using the provided error in case of the `None` variant.
|
|
467
|
+
|
|
468
|
+
Parameters:
|
|
469
|
+
|
|
470
|
+
|param|type|description|
|
|
471
|
+
|-----|----|-----------|
|
|
472
|
+
|`err`|`a`|The error to use if the option is `None`|
|
|
473
|
+
|`option`|`Option<b>`|The option to convert|
|
|
474
|
+
|
|
475
|
+
Returns:
|
|
476
|
+
|
|
477
|
+
|type|description|
|
|
478
|
+
|----|-----------|
|
|
479
|
+
|`Result<b, a>`|`Ok(value)` if the Option is `Some(value)` or `Err(err)` if the Option is `None`|
|
|
480
|
+
|
|
481
|
+
### Option.**sideEffect**
|
|
482
|
+
|
|
483
|
+
<details disabled>
|
|
484
|
+
<summary tabindex="-1">Added in <code>0.2.0</code></summary>
|
|
485
|
+
No other changes yet.
|
|
486
|
+
</details>
|
|
487
|
+
|
|
488
|
+
```grain
|
|
489
|
+
sideEffect : ((a -> Void), Option<a>) -> Void
|
|
490
|
+
```
|
|
491
|
+
|
|
492
|
+
If the Option is `Some(value)`, applies the `fn` function to the `value` without producing a new value.
|
|
493
|
+
|
|
494
|
+
Parameters:
|
|
495
|
+
|
|
496
|
+
|param|type|description|
|
|
497
|
+
|-----|----|-----------|
|
|
498
|
+
|`fn`|`a -> Void`|The function to call on the value of a `Some` variant|
|
|
499
|
+
|`option`|`Option<a>`|The option to inspect|
|
|
500
|
+
|
|
501
|
+
### Option.**peek**
|
|
502
|
+
|
|
503
|
+
<details disabled>
|
|
504
|
+
<summary tabindex="-1">Added in <code>0.2.0</code></summary>
|
|
505
|
+
No other changes yet.
|
|
506
|
+
</details>
|
|
507
|
+
|
|
508
|
+
```grain
|
|
509
|
+
peek : ((a -> Void), Option<a>) -> Option<a>
|
|
510
|
+
```
|
|
511
|
+
|
|
512
|
+
If the Option is `Some(value)`, applies the `fn` function to the `value` without producing a new value.
|
|
513
|
+
Useful for inspecting Options without changing anything.
|
|
514
|
+
|
|
515
|
+
Parameters:
|
|
516
|
+
|
|
517
|
+
|param|type|description|
|
|
518
|
+
|-----|----|-----------|
|
|
519
|
+
|`fn`|`a -> Void`|The function to call on the value of a `Some` variant|
|
|
520
|
+
|`option`|`Option<a>`|The option to inspect|
|
|
521
|
+
|
|
522
|
+
Returns:
|
|
523
|
+
|
|
524
|
+
|type|description|
|
|
525
|
+
|----|-----------|
|
|
526
|
+
|`Option<a>`|The unmodified option|
|
|
527
|
+
|
|
528
|
+
### Option.**or**
|
|
529
|
+
|
|
530
|
+
<details disabled>
|
|
531
|
+
<summary tabindex="-1">Added in <code>0.2.0</code></summary>
|
|
532
|
+
No other changes yet.
|
|
533
|
+
</details>
|
|
534
|
+
|
|
535
|
+
```grain
|
|
536
|
+
( or ) : (Option<a>, Option<a>) -> Option<a>
|
|
537
|
+
```
|
|
538
|
+
|
|
539
|
+
Behaves like a logical OR (`||`) where the first Option is only returned if it is the `Some` variant and falling back to the second Option in all other cases.
|
|
540
|
+
|
|
541
|
+
Parameters:
|
|
542
|
+
|
|
543
|
+
|param|type|description|
|
|
544
|
+
|-----|----|-----------|
|
|
545
|
+
|`optionA`|`Option<a>`|The first option|
|
|
546
|
+
|`optionB`|`Option<a>`|The second option|
|
|
547
|
+
|
|
548
|
+
Returns:
|
|
549
|
+
|
|
550
|
+
|type|description|
|
|
551
|
+
|----|-----------|
|
|
552
|
+
|`Option<a>`|The first Option if it is the `Some` variant or the second Option otherwise|
|
|
553
|
+
|
|
554
|
+
### Option.**and**
|
|
555
|
+
|
|
556
|
+
<details disabled>
|
|
557
|
+
<summary tabindex="-1">Added in <code>0.2.0</code></summary>
|
|
558
|
+
No other changes yet.
|
|
559
|
+
</details>
|
|
560
|
+
|
|
561
|
+
```grain
|
|
562
|
+
and : (Option<a>, Option<a>) -> Option<a>
|
|
563
|
+
```
|
|
564
|
+
|
|
565
|
+
Behaves like a logical AND (`&&`) where the first Option is only returned if it is the `None` variant and falling back to the second Option Result in all other cases.
|
|
566
|
+
|
|
567
|
+
Parameters:
|
|
568
|
+
|
|
569
|
+
|param|type|description|
|
|
570
|
+
|-----|----|-----------|
|
|
571
|
+
|`optionA`|`Option<a>`|The first option|
|
|
572
|
+
|`optionB`|`Option<a>`|The second option|
|
|
573
|
+
|
|
574
|
+
Returns:
|
|
575
|
+
|
|
576
|
+
|type|description|
|
|
577
|
+
|----|-----------|
|
|
578
|
+
|`Option<a>`|The second Option if both are the `Some` variant or the first Option otherwise|
|
|
579
|
+
|
package/package.json
CHANGED
package/pervasives.gr
CHANGED
|
@@ -24,7 +24,7 @@ import {
|
|
|
24
24
|
(^),
|
|
25
25
|
(<<),
|
|
26
26
|
(>>>),
|
|
27
|
-
(>>)
|
|
27
|
+
(>>),
|
|
28
28
|
} from "runtime/numbers"
|
|
29
29
|
|
|
30
30
|
// Math operations
|
|
@@ -53,7 +53,6 @@ export (<<)
|
|
|
53
53
|
export (>>>)
|
|
54
54
|
export (>>)
|
|
55
55
|
|
|
56
|
-
|
|
57
56
|
// Number coercions & conversions
|
|
58
57
|
|
|
59
58
|
// [TODO] (#311) Commented until we nail down semantics
|
|
@@ -61,23 +60,23 @@ export (>>)
|
|
|
61
60
|
// import foreign wasm convertInexactToExact : Number -> Number as exact from "stdlib-external/runtime"
|
|
62
61
|
|
|
63
62
|
// Boolean operations
|
|
64
|
-
primitive (!)
|
|
65
|
-
primitive (&&)
|
|
66
|
-
primitive (||)
|
|
63
|
+
primitive (!): Bool -> Bool = "@not"
|
|
64
|
+
primitive (&&): (Bool, Bool) -> Bool = "@and"
|
|
65
|
+
primitive (||): (Bool, Bool) -> Bool = "@or"
|
|
67
66
|
|
|
68
67
|
// Box operations
|
|
69
|
-
primitive box
|
|
70
|
-
primitive unbox
|
|
68
|
+
primitive box: a -> Box<a> = "@box"
|
|
69
|
+
primitive unbox: Box<a> -> a = "@unbox"
|
|
71
70
|
|
|
72
71
|
// Exceptions
|
|
73
72
|
exception Failure(String)
|
|
74
73
|
exception InvalidArgument(String)
|
|
75
74
|
|
|
76
75
|
// Other operations
|
|
77
|
-
primitive ignore
|
|
78
|
-
primitive assert
|
|
79
|
-
primitive throw
|
|
80
|
-
let fail
|
|
76
|
+
primitive ignore: a -> Void = "@ignore"
|
|
77
|
+
primitive assert: Bool -> Void = "@assert"
|
|
78
|
+
primitive throw: Exception -> a = "@throw"
|
|
79
|
+
let fail: String -> a = msg => throw Failure(msg)
|
|
81
80
|
|
|
82
81
|
import { toString, print, concat as (++) } from "runtime/string"
|
|
83
82
|
|
|
@@ -91,28 +90,41 @@ export (++)
|
|
|
91
90
|
|
|
92
91
|
// Checks the given items for structural equality.
|
|
93
92
|
export (==)
|
|
94
|
-
let (!=)
|
|
93
|
+
let (!=): (a, a) -> Bool = (x, y) => !(x == y)
|
|
95
94
|
// Checks the given items for physical equality.
|
|
96
|
-
primitive (is)
|
|
95
|
+
primitive (is): (a, a) -> Bool = "@is"
|
|
97
96
|
// The opposite of is operator
|
|
98
|
-
let (isnt)
|
|
97
|
+
let (isnt): (a, a) -> Bool = (x, y) => !(x is y)
|
|
99
98
|
|
|
100
|
-
export enum List<a> {
|
|
99
|
+
export enum List<a> {
|
|
100
|
+
[],
|
|
101
|
+
[...](a, List<a>),
|
|
102
|
+
}
|
|
101
103
|
|
|
102
104
|
/**
|
|
103
105
|
* @deprecated This will be removed in a future release of Grain.
|
|
104
106
|
*/
|
|
105
|
-
let cons = (a, b) =>
|
|
107
|
+
let cons = (a, b) =>
|
|
108
|
+
[
|
|
109
|
+
a,
|
|
110
|
+
...b
|
|
111
|
+
] // <- workaround for (grain-lang/grain#802) [TODO] fix #802 and delete
|
|
106
112
|
let empty = [] // <- for parity with `cons`, but should be deleted as well
|
|
107
113
|
|
|
108
114
|
// Maybe some data, maybe not!
|
|
109
|
-
enum Option<a> {
|
|
115
|
+
enum Option<a> {
|
|
116
|
+
Some(a),
|
|
117
|
+
None,
|
|
118
|
+
}
|
|
110
119
|
|
|
111
120
|
// Maybe some data, maybe an error!
|
|
112
|
-
enum Result<t, e> {
|
|
121
|
+
enum Result<t, e> {
|
|
122
|
+
Ok(t),
|
|
123
|
+
Err(e),
|
|
124
|
+
}
|
|
113
125
|
|
|
114
126
|
// Identity function
|
|
115
|
-
let identity =
|
|
127
|
+
let identity = x => x
|
|
116
128
|
|
|
117
129
|
// Setup exception printing
|
|
118
130
|
@disableGC
|
|
@@ -121,7 +133,7 @@ let rec setupExceptions = () => {
|
|
|
121
133
|
match (e) {
|
|
122
134
|
Failure(msg) => Some("Failure: " ++ msg),
|
|
123
135
|
InvalidArgument(msg) => Some("Invalid argument: " ++ msg),
|
|
124
|
-
_ => None
|
|
136
|
+
_ => None,
|
|
125
137
|
}
|
|
126
138
|
})
|
|
127
139
|
|