@grain/stdlib 0.4.3 → 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/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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@grain/stdlib",
3
- "version": "0.4.3",
3
+ "version": "0.4.4",
4
4
  "description": "The standard library for the Grain language.",
5
5
  "license": "MIT",
6
6
  "homepage": "https://grain-lang.org",