@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/set.md
ADDED
|
@@ -0,0 +1,502 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Set
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
A Set is an unordered collection of unique values. Operations on a Set mutate the internal state, so it never needs to be re-assigned.
|
|
6
|
+
|
|
7
|
+
<details disabled>
|
|
8
|
+
<summary tabindex="-1">Added in <code>0.3.0</code></summary>
|
|
9
|
+
No other changes yet.
|
|
10
|
+
</details>
|
|
11
|
+
|
|
12
|
+
```grain
|
|
13
|
+
import Set from "set"
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
### Set.**Bucket**
|
|
17
|
+
|
|
18
|
+
```grain
|
|
19
|
+
record Bucket<t> {
|
|
20
|
+
key: t,
|
|
21
|
+
next: Option<Bucket<t>>,
|
|
22
|
+
}
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
### Set.**Set**
|
|
26
|
+
|
|
27
|
+
```grain
|
|
28
|
+
record Set<k> {
|
|
29
|
+
size: Number,
|
|
30
|
+
buckets: Array<Option<Bucket<k>>>,
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### Set.**makeSized**
|
|
35
|
+
|
|
36
|
+
<details disabled>
|
|
37
|
+
<summary tabindex="-1">Added in <code>0.3.0</code></summary>
|
|
38
|
+
No other changes yet.
|
|
39
|
+
</details>
|
|
40
|
+
|
|
41
|
+
```grain
|
|
42
|
+
makeSized : Number -> Set<a>
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Creates a new empty set with an initial storage of the given length. As values are added or removed, the length may grow or shrink. Generally, you won't need to care about the length of your set and can use `Set.make()` instead.
|
|
46
|
+
|
|
47
|
+
Parameters:
|
|
48
|
+
|
|
49
|
+
|param|type|description|
|
|
50
|
+
|-----|----|-----------|
|
|
51
|
+
|`storageLength`|`Number`|The initial storage length of the set|
|
|
52
|
+
|
|
53
|
+
Returns:
|
|
54
|
+
|
|
55
|
+
|type|description|
|
|
56
|
+
|----|-----------|
|
|
57
|
+
|`Set<a>`|An empty set with the given initial storage length|
|
|
58
|
+
|
|
59
|
+
### Set.**make**
|
|
60
|
+
|
|
61
|
+
<details disabled>
|
|
62
|
+
<summary tabindex="-1">Added in <code>0.3.0</code></summary>
|
|
63
|
+
No other changes yet.
|
|
64
|
+
</details>
|
|
65
|
+
|
|
66
|
+
```grain
|
|
67
|
+
make : () -> Set<a>
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Creates a new, empty set.
|
|
71
|
+
|
|
72
|
+
Returns:
|
|
73
|
+
|
|
74
|
+
|type|description|
|
|
75
|
+
|----|-----------|
|
|
76
|
+
|`Set<a>`|An empty set|
|
|
77
|
+
|
|
78
|
+
### Set.**add**
|
|
79
|
+
|
|
80
|
+
<details disabled>
|
|
81
|
+
<summary tabindex="-1">Added in <code>0.3.0</code></summary>
|
|
82
|
+
No other changes yet.
|
|
83
|
+
</details>
|
|
84
|
+
|
|
85
|
+
```grain
|
|
86
|
+
add : (a, Set<a>) -> Void
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Adds a new value to the set. If the value already exists, nothing happens.
|
|
90
|
+
|
|
91
|
+
Parameters:
|
|
92
|
+
|
|
93
|
+
|param|type|description|
|
|
94
|
+
|-----|----|-----------|
|
|
95
|
+
|`key`|`a`|The value to add|
|
|
96
|
+
|`set`|`Set<a>`|The set to update|
|
|
97
|
+
|
|
98
|
+
### Set.**contains**
|
|
99
|
+
|
|
100
|
+
<details disabled>
|
|
101
|
+
<summary tabindex="-1">Added in <code>0.3.0</code></summary>
|
|
102
|
+
No other changes yet.
|
|
103
|
+
</details>
|
|
104
|
+
|
|
105
|
+
```grain
|
|
106
|
+
contains : (a, Set<a>) -> Bool
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
Determines if the set contains the given value.
|
|
110
|
+
|
|
111
|
+
Parameters:
|
|
112
|
+
|
|
113
|
+
|param|type|description|
|
|
114
|
+
|-----|----|-----------|
|
|
115
|
+
|`key`|`a`|The value to search for|
|
|
116
|
+
|`set`|`Set<a>`|The set to search|
|
|
117
|
+
|
|
118
|
+
Returns:
|
|
119
|
+
|
|
120
|
+
|type|description|
|
|
121
|
+
|----|-----------|
|
|
122
|
+
|`Bool`|`true` if the set contains the given value or `false` otherwise|
|
|
123
|
+
|
|
124
|
+
### Set.**remove**
|
|
125
|
+
|
|
126
|
+
<details disabled>
|
|
127
|
+
<summary tabindex="-1">Added in <code>0.3.0</code></summary>
|
|
128
|
+
No other changes yet.
|
|
129
|
+
</details>
|
|
130
|
+
|
|
131
|
+
```grain
|
|
132
|
+
remove : (a, Set<a>) -> Void
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
Removes the given value from the set. If the value doesn't exist, nothing happens.
|
|
136
|
+
|
|
137
|
+
Parameters:
|
|
138
|
+
|
|
139
|
+
|param|type|description|
|
|
140
|
+
|-----|----|-----------|
|
|
141
|
+
|`key`|`a`|The value to remove|
|
|
142
|
+
|`set`|`Set<a>`|The set to update|
|
|
143
|
+
|
|
144
|
+
### Set.**size**
|
|
145
|
+
|
|
146
|
+
<details disabled>
|
|
147
|
+
<summary tabindex="-1">Added in <code>0.3.0</code></summary>
|
|
148
|
+
No other changes yet.
|
|
149
|
+
</details>
|
|
150
|
+
|
|
151
|
+
```grain
|
|
152
|
+
size : Set<a> -> Number
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
Returns the number of values within the set.
|
|
156
|
+
|
|
157
|
+
Parameters:
|
|
158
|
+
|
|
159
|
+
|param|type|description|
|
|
160
|
+
|-----|----|-----------|
|
|
161
|
+
|`set`|`Set<a>`|The set to inspect|
|
|
162
|
+
|
|
163
|
+
Returns:
|
|
164
|
+
|
|
165
|
+
|type|description|
|
|
166
|
+
|----|-----------|
|
|
167
|
+
|`Number`|The number of elements in the set|
|
|
168
|
+
|
|
169
|
+
### Set.**isEmpty**
|
|
170
|
+
|
|
171
|
+
<details disabled>
|
|
172
|
+
<summary tabindex="-1">Added in <code>0.3.0</code></summary>
|
|
173
|
+
No other changes yet.
|
|
174
|
+
</details>
|
|
175
|
+
|
|
176
|
+
```grain
|
|
177
|
+
isEmpty : Set<a> -> Bool
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
Determines if the set contains no elements.
|
|
181
|
+
|
|
182
|
+
Parameters:
|
|
183
|
+
|
|
184
|
+
|param|type|description|
|
|
185
|
+
|-----|----|-----------|
|
|
186
|
+
|`set`|`Set<a>`|The set to inspect|
|
|
187
|
+
|
|
188
|
+
Returns:
|
|
189
|
+
|
|
190
|
+
|type|description|
|
|
191
|
+
|----|-----------|
|
|
192
|
+
|`Bool`|`true` if the given set is empty or `false` otherwise|
|
|
193
|
+
|
|
194
|
+
### Set.**clear**
|
|
195
|
+
|
|
196
|
+
<details disabled>
|
|
197
|
+
<summary tabindex="-1">Added in <code>0.3.0</code></summary>
|
|
198
|
+
No other changes yet.
|
|
199
|
+
</details>
|
|
200
|
+
|
|
201
|
+
```grain
|
|
202
|
+
clear : Set<a> -> Void
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
Resets the set by removing all values.
|
|
206
|
+
|
|
207
|
+
Parameters:
|
|
208
|
+
|
|
209
|
+
|param|type|description|
|
|
210
|
+
|-----|----|-----------|
|
|
211
|
+
|`set`|`Set<a>`|The set to reset|
|
|
212
|
+
|
|
213
|
+
### Set.**forEach**
|
|
214
|
+
|
|
215
|
+
<details disabled>
|
|
216
|
+
<summary tabindex="-1">Added in <code>0.3.0</code></summary>
|
|
217
|
+
No other changes yet.
|
|
218
|
+
</details>
|
|
219
|
+
|
|
220
|
+
```grain
|
|
221
|
+
forEach : ((a -> b), Set<a>) -> Void
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
Iterates the set, calling an iterator function on each element.
|
|
225
|
+
|
|
226
|
+
Parameters:
|
|
227
|
+
|
|
228
|
+
|param|type|description|
|
|
229
|
+
|-----|----|-----------|
|
|
230
|
+
|`fn`|`a -> b`|The iterator function to call with each element|
|
|
231
|
+
|`set`|`Set<a>`|The set to iterate|
|
|
232
|
+
|
|
233
|
+
### Set.**reduce**
|
|
234
|
+
|
|
235
|
+
<details disabled>
|
|
236
|
+
<summary tabindex="-1">Added in <code>0.3.0</code></summary>
|
|
237
|
+
No other changes yet.
|
|
238
|
+
</details>
|
|
239
|
+
|
|
240
|
+
```grain
|
|
241
|
+
reduce : (((a, b) -> a), a, Set<b>) -> a
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
Combines all elements of a set using a reducer function.
|
|
245
|
+
|
|
246
|
+
Parameters:
|
|
247
|
+
|
|
248
|
+
|param|type|description|
|
|
249
|
+
|-----|----|-----------|
|
|
250
|
+
|`fn`|`(a, b) -> a`|The reducer function to call on each element, where the value returned will be the next accumulator value|
|
|
251
|
+
|`init`|`a`|The initial value to use for the accumulator on the first iteration|
|
|
252
|
+
|`set`|`Set<b>`|The set to iterate|
|
|
253
|
+
|
|
254
|
+
Returns:
|
|
255
|
+
|
|
256
|
+
|type|description|
|
|
257
|
+
|----|-----------|
|
|
258
|
+
|`a`|The final accumulator returned from `fn`|
|
|
259
|
+
|
|
260
|
+
### Set.**filter**
|
|
261
|
+
|
|
262
|
+
<details disabled>
|
|
263
|
+
<summary tabindex="-1">Added in <code>0.3.0</code></summary>
|
|
264
|
+
No other changes yet.
|
|
265
|
+
</details>
|
|
266
|
+
|
|
267
|
+
```grain
|
|
268
|
+
filter : ((a -> Bool), Set<a>) -> Void
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
Removes elements from a set where a predicate function returns `false`.
|
|
272
|
+
|
|
273
|
+
Parameters:
|
|
274
|
+
|
|
275
|
+
|param|type|description|
|
|
276
|
+
|-----|----|-----------|
|
|
277
|
+
|`fn`|`a -> Bool`|The predicate function to indicate which elements to remove from the set, where returning `false` indicates the value should be removed|
|
|
278
|
+
|`set`|`Set<a>`|The set to iterate|
|
|
279
|
+
|
|
280
|
+
### Set.**reject**
|
|
281
|
+
|
|
282
|
+
<details disabled>
|
|
283
|
+
<summary tabindex="-1">Added in <code>0.3.0</code></summary>
|
|
284
|
+
No other changes yet.
|
|
285
|
+
</details>
|
|
286
|
+
|
|
287
|
+
```grain
|
|
288
|
+
reject : ((a -> Bool), Set<a>) -> Void
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
Removes elements from a set where a predicate function returns `true`.
|
|
292
|
+
|
|
293
|
+
Parameters:
|
|
294
|
+
|
|
295
|
+
|param|type|description|
|
|
296
|
+
|-----|----|-----------|
|
|
297
|
+
|`fn`|`a -> Bool`|The predicate function to indicate which elements to remove from the set, where returning `true` indicates the value should be removed|
|
|
298
|
+
|`set`|`Set<a>`|The set to iterate|
|
|
299
|
+
|
|
300
|
+
### Set.**toList**
|
|
301
|
+
|
|
302
|
+
<details disabled>
|
|
303
|
+
<summary tabindex="-1">Added in <code>0.3.0</code></summary>
|
|
304
|
+
No other changes yet.
|
|
305
|
+
</details>
|
|
306
|
+
|
|
307
|
+
```grain
|
|
308
|
+
toList : Set<a> -> List<a>
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
Converts a set into a list of its elements.
|
|
312
|
+
|
|
313
|
+
Parameters:
|
|
314
|
+
|
|
315
|
+
|param|type|description|
|
|
316
|
+
|-----|----|-----------|
|
|
317
|
+
|`set`|`Set<a>`|The set to convert|
|
|
318
|
+
|
|
319
|
+
Returns:
|
|
320
|
+
|
|
321
|
+
|type|description|
|
|
322
|
+
|----|-----------|
|
|
323
|
+
|`List<a>`|A list containing all set values|
|
|
324
|
+
|
|
325
|
+
### Set.**fromList**
|
|
326
|
+
|
|
327
|
+
<details disabled>
|
|
328
|
+
<summary tabindex="-1">Added in <code>0.3.0</code></summary>
|
|
329
|
+
No other changes yet.
|
|
330
|
+
</details>
|
|
331
|
+
|
|
332
|
+
```grain
|
|
333
|
+
fromList : List<a> -> Set<a>
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
Creates a set from a list.
|
|
337
|
+
|
|
338
|
+
Parameters:
|
|
339
|
+
|
|
340
|
+
|param|type|description|
|
|
341
|
+
|-----|----|-----------|
|
|
342
|
+
|`list`|`List<a>`|The list to convert|
|
|
343
|
+
|
|
344
|
+
Returns:
|
|
345
|
+
|
|
346
|
+
|type|description|
|
|
347
|
+
|----|-----------|
|
|
348
|
+
|`Set<a>`|A set containing all list values|
|
|
349
|
+
|
|
350
|
+
### Set.**toArray**
|
|
351
|
+
|
|
352
|
+
<details disabled>
|
|
353
|
+
<summary tabindex="-1">Added in <code>0.3.0</code></summary>
|
|
354
|
+
No other changes yet.
|
|
355
|
+
</details>
|
|
356
|
+
|
|
357
|
+
```grain
|
|
358
|
+
toArray : Set<a> -> Array<a>
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
Converts a set into an array of its elements.
|
|
362
|
+
|
|
363
|
+
Parameters:
|
|
364
|
+
|
|
365
|
+
|param|type|description|
|
|
366
|
+
|-----|----|-----------|
|
|
367
|
+
|`set`|`Set<a>`|The set to convert|
|
|
368
|
+
|
|
369
|
+
Returns:
|
|
370
|
+
|
|
371
|
+
|type|description|
|
|
372
|
+
|----|-----------|
|
|
373
|
+
|`Array<a>`|An array containing all set values|
|
|
374
|
+
|
|
375
|
+
### Set.**fromArray**
|
|
376
|
+
|
|
377
|
+
<details disabled>
|
|
378
|
+
<summary tabindex="-1">Added in <code>0.3.0</code></summary>
|
|
379
|
+
No other changes yet.
|
|
380
|
+
</details>
|
|
381
|
+
|
|
382
|
+
```grain
|
|
383
|
+
fromArray : Array<a> -> Set<a>
|
|
384
|
+
```
|
|
385
|
+
|
|
386
|
+
Creates a set from an array.
|
|
387
|
+
|
|
388
|
+
Parameters:
|
|
389
|
+
|
|
390
|
+
|param|type|description|
|
|
391
|
+
|-----|----|-----------|
|
|
392
|
+
|`array`|`Array<a>`|The array to convert|
|
|
393
|
+
|
|
394
|
+
Returns:
|
|
395
|
+
|
|
396
|
+
|type|description|
|
|
397
|
+
|----|-----------|
|
|
398
|
+
|`Set<a>`|A set containing all array values|
|
|
399
|
+
|
|
400
|
+
### Set.**union**
|
|
401
|
+
|
|
402
|
+
<details disabled>
|
|
403
|
+
<summary tabindex="-1">Added in <code>0.3.0</code></summary>
|
|
404
|
+
No other changes yet.
|
|
405
|
+
</details>
|
|
406
|
+
|
|
407
|
+
```grain
|
|
408
|
+
union : (Set<a>, Set<a>) -> Set<a>
|
|
409
|
+
```
|
|
410
|
+
|
|
411
|
+
Combines two sets into a single set containing all elements from both sets.
|
|
412
|
+
|
|
413
|
+
Parameters:
|
|
414
|
+
|
|
415
|
+
|param|type|description|
|
|
416
|
+
|-----|----|-----------|
|
|
417
|
+
|`set1`|`Set<a>`|The first set to combine|
|
|
418
|
+
|`set2`|`Set<a>`|The second set to combine|
|
|
419
|
+
|
|
420
|
+
Returns:
|
|
421
|
+
|
|
422
|
+
|type|description|
|
|
423
|
+
|----|-----------|
|
|
424
|
+
|`Set<a>`|A set containing all elements of both sets|
|
|
425
|
+
|
|
426
|
+
### Set.**diff**
|
|
427
|
+
|
|
428
|
+
<details disabled>
|
|
429
|
+
<summary tabindex="-1">Added in <code>0.3.0</code></summary>
|
|
430
|
+
No other changes yet.
|
|
431
|
+
</details>
|
|
432
|
+
|
|
433
|
+
```grain
|
|
434
|
+
diff : (Set<a>, Set<a>) -> Set<a>
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
Combines two sets into a single set containing only the elements not shared between both sets.
|
|
438
|
+
|
|
439
|
+
Parameters:
|
|
440
|
+
|
|
441
|
+
|param|type|description|
|
|
442
|
+
|-----|----|-----------|
|
|
443
|
+
|`set1`|`Set<a>`|The first set to combine|
|
|
444
|
+
|`set2`|`Set<a>`|The second set to combine|
|
|
445
|
+
|
|
446
|
+
Returns:
|
|
447
|
+
|
|
448
|
+
|type|description|
|
|
449
|
+
|----|-----------|
|
|
450
|
+
|`Set<a>`|A set containing only unshared elements from both sets|
|
|
451
|
+
|
|
452
|
+
### Set.**intersect**
|
|
453
|
+
|
|
454
|
+
<details disabled>
|
|
455
|
+
<summary tabindex="-1">Added in <code>0.3.0</code></summary>
|
|
456
|
+
No other changes yet.
|
|
457
|
+
</details>
|
|
458
|
+
|
|
459
|
+
```grain
|
|
460
|
+
intersect : (Set<a>, Set<a>) -> Set<a>
|
|
461
|
+
```
|
|
462
|
+
|
|
463
|
+
Combines two sets into a single set containing only the elements shared between both sets.
|
|
464
|
+
|
|
465
|
+
Parameters:
|
|
466
|
+
|
|
467
|
+
|param|type|description|
|
|
468
|
+
|-----|----|-----------|
|
|
469
|
+
|`set1`|`Set<a>`|The first set to combine|
|
|
470
|
+
|`set2`|`Set<a>`|The second set to combine|
|
|
471
|
+
|
|
472
|
+
Returns:
|
|
473
|
+
|
|
474
|
+
|type|description|
|
|
475
|
+
|----|-----------|
|
|
476
|
+
|`Set<a>`|A set containing only shared elements from both sets|
|
|
477
|
+
|
|
478
|
+
### Set.**getInternalStats**
|
|
479
|
+
|
|
480
|
+
<details disabled>
|
|
481
|
+
<summary tabindex="-1">Added in <code>0.3.0</code></summary>
|
|
482
|
+
No other changes yet.
|
|
483
|
+
</details>
|
|
484
|
+
|
|
485
|
+
```grain
|
|
486
|
+
getInternalStats : Set<a> -> (Number, Number)
|
|
487
|
+
```
|
|
488
|
+
|
|
489
|
+
Provides data representing the internal state state of the set.
|
|
490
|
+
|
|
491
|
+
Parameters:
|
|
492
|
+
|
|
493
|
+
|param|type|description|
|
|
494
|
+
|-----|----|-----------|
|
|
495
|
+
|`set`|`Set<a>`|The set to inspect|
|
|
496
|
+
|
|
497
|
+
Returns:
|
|
498
|
+
|
|
499
|
+
|type|description|
|
|
500
|
+
|----|-----------|
|
|
501
|
+
|`(Number, Number)`|The internal state of the set|
|
|
502
|
+
|
package/stack.gr
CHANGED
|
@@ -13,7 +13,7 @@ import List from "list"
|
|
|
13
13
|
* Stacks are immutable data structures that store their data in a List.
|
|
14
14
|
*/
|
|
15
15
|
record Stack<a> {
|
|
16
|
-
|
|
16
|
+
data: List<a>,
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
/**
|
|
@@ -26,7 +26,7 @@ record Stack<a> {
|
|
|
26
26
|
* @returns An empty stack
|
|
27
27
|
*/
|
|
28
28
|
export let make = () => {
|
|
29
|
-
|
|
29
|
+
{ data: [], }
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
/**
|
|
@@ -35,11 +35,11 @@ export let make = () => {
|
|
|
35
35
|
* @param stack: The stack to check
|
|
36
36
|
* @returns `true` if the stack has no items, and `false` otherwise
|
|
37
37
|
*/
|
|
38
|
-
export let isEmpty =
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
38
|
+
export let isEmpty = stack => {
|
|
39
|
+
match (stack) {
|
|
40
|
+
{ data: [] } => true,
|
|
41
|
+
_ => false,
|
|
42
|
+
}
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
/**
|
|
@@ -48,11 +48,11 @@ export let isEmpty = (stack) => {
|
|
|
48
48
|
* @param stack: The stack to inspect
|
|
49
49
|
* @returns The value at the top of the stack, if it exists
|
|
50
50
|
*/
|
|
51
|
-
export let peek =
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
51
|
+
export let peek = stack => {
|
|
52
|
+
match (stack) {
|
|
53
|
+
{ data: [] } => None,
|
|
54
|
+
{ data } => List.head(data),
|
|
55
|
+
}
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
/**
|
|
@@ -63,10 +63,10 @@ export let peek = (stack) => {
|
|
|
63
63
|
* @returns A new stack with the item added to the end
|
|
64
64
|
*/
|
|
65
65
|
export let push = (value, stack) => {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
66
|
+
match (stack) {
|
|
67
|
+
{ data: [] } => { data: [value], },
|
|
68
|
+
{ data } => { data: [value, ...data], },
|
|
69
|
+
}
|
|
70
70
|
}
|
|
71
71
|
|
|
72
72
|
/**
|
|
@@ -75,11 +75,11 @@ export let push = (value, stack) => {
|
|
|
75
75
|
* @param stack: The stack being updated
|
|
76
76
|
* @returns A new stack with the last item removed
|
|
77
77
|
*/
|
|
78
|
-
export let pop =
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
78
|
+
export let pop = stack => {
|
|
79
|
+
match (stack) {
|
|
80
|
+
{ data: [] } => stack,
|
|
81
|
+
{ data: [head, ...tail] } => { data: tail, },
|
|
82
|
+
}
|
|
83
83
|
}
|
|
84
84
|
|
|
85
85
|
/**
|
|
@@ -88,9 +88,9 @@ export let pop = (stack) => {
|
|
|
88
88
|
* @param stack: The stack to inspect
|
|
89
89
|
* @returns The count of the items in the stack
|
|
90
90
|
*/
|
|
91
|
-
export let size =
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
91
|
+
export let size = stack => {
|
|
92
|
+
match (stack) {
|
|
93
|
+
{ data: [] } => 0,
|
|
94
|
+
{ data } => List.length(data),
|
|
95
|
+
}
|
|
96
96
|
}
|