@grain/stdlib 0.6.6 → 0.7.0
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 +57 -0
- package/LICENSE +1 -1
- package/README.md +2 -2
- package/array.gr +55 -7
- package/array.md +123 -77
- package/bigint.md +30 -30
- package/buffer.gr +20 -53
- package/buffer.md +47 -47
- package/bytes.gr +111 -35
- package/bytes.md +111 -32
- package/char.gr +201 -99
- package/char.md +361 -34
- package/exception.gr +11 -11
- package/exception.md +26 -1
- package/float32.gr +327 -3
- package/float32.md +606 -19
- package/float64.gr +320 -3
- package/float64.md +606 -19
- package/fs.gr +1082 -0
- package/fs.md +630 -0
- package/hash.gr +142 -88
- package/hash.md +102 -14
- package/int16.md +23 -23
- package/int32.gr +25 -4
- package/int32.md +65 -30
- package/int64.gr +26 -1
- package/int64.md +65 -30
- package/int8.md +23 -23
- package/json.gr +366 -51
- package/json.md +418 -2
- package/list.gr +328 -31
- package/list.md +492 -69
- package/map.gr +20 -12
- package/map.md +44 -38
- package/marshal.gr +41 -40
- package/marshal.md +2 -2
- package/number.gr +159 -30
- package/number.md +215 -38
- package/option.md +21 -21
- package/package.json +5 -3
- package/path.gr +48 -0
- package/path.md +103 -12
- package/pervasives.gr +2 -2
- package/pervasives.md +37 -37
- package/priorityqueue.gr +7 -7
- package/priorityqueue.md +19 -19
- package/queue.gr +183 -29
- package/queue.md +296 -40
- package/random.md +6 -6
- package/range.gr +4 -4
- package/range.md +6 -6
- package/rational.md +16 -16
- package/regex.gr +52 -51
- package/regex.md +11 -11
- package/result.md +16 -16
- package/runtime/atof/common.md +39 -39
- package/runtime/atof/decimal.gr +6 -6
- package/runtime/atof/decimal.md +8 -8
- package/runtime/atof/lemire.gr +5 -5
- package/runtime/atof/lemire.md +1 -1
- package/runtime/atof/parse.gr +16 -16
- package/runtime/atof/parse.md +2 -2
- package/runtime/atof/slow.md +1 -1
- package/runtime/atof/table.md +2 -2
- package/runtime/atoi/parse.gr +3 -3
- package/runtime/atoi/parse.md +1 -1
- package/runtime/bigint.gr +15 -47
- package/runtime/bigint.md +54 -60
- package/runtime/compare.gr +2 -2
- package/runtime/compare.md +1 -1
- package/runtime/dataStructures.md +33 -33
- package/runtime/debugPrint.gr +4 -1
- package/runtime/debugPrint.md +9 -9
- package/runtime/equal.gr +99 -77
- package/runtime/equal.md +1 -1
- package/runtime/exception.gr +62 -82
- package/runtime/exception.md +62 -11
- package/runtime/gc.gr +39 -45
- package/runtime/gc.md +4 -4
- package/runtime/malloc.gr +7 -7
- package/runtime/malloc.md +4 -4
- package/runtime/math/kernel/cos.gr +70 -0
- package/runtime/math/kernel/cos.md +14 -0
- package/runtime/math/kernel/sin.gr +65 -0
- package/runtime/math/kernel/sin.md +14 -0
- package/runtime/math/kernel/tan.gr +136 -0
- package/runtime/math/kernel/tan.md +14 -0
- package/runtime/math/rempio2.gr +244 -0
- package/runtime/math/rempio2.md +14 -0
- package/runtime/math/trig.gr +130 -0
- package/runtime/math/trig.md +28 -0
- package/runtime/math/umuldi.gr +26 -0
- package/runtime/math/umuldi.md +14 -0
- package/runtime/numberUtils.gr +29 -29
- package/runtime/numberUtils.md +12 -12
- package/runtime/numbers.gr +373 -381
- package/runtime/numbers.md +79 -73
- package/runtime/string.gr +37 -105
- package/runtime/string.md +3 -9
- package/runtime/unsafe/constants.md +24 -24
- package/runtime/unsafe/conv.md +13 -13
- package/runtime/unsafe/memory.gr +24 -20
- package/runtime/unsafe/memory.md +27 -7
- package/runtime/unsafe/offsets.gr +36 -0
- package/runtime/unsafe/offsets.md +88 -0
- package/runtime/unsafe/panic.gr +28 -0
- package/runtime/unsafe/panic.md +14 -0
- package/runtime/unsafe/tags.md +32 -32
- package/runtime/unsafe/wasmf32.md +28 -28
- package/runtime/unsafe/wasmf64.md +28 -28
- package/runtime/unsafe/wasmi32.md +47 -47
- package/runtime/unsafe/wasmi64.md +50 -50
- package/runtime/utf8.gr +189 -0
- package/runtime/utf8.md +117 -0
- package/runtime/wasi.gr +4 -2
- package/runtime/wasi.md +138 -138
- package/set.gr +18 -11
- package/set.md +42 -36
- package/stack.gr +171 -2
- package/stack.md +297 -15
- package/string.gr +352 -557
- package/string.md +77 -34
- package/uint16.md +22 -22
- package/uint32.gr +25 -4
- package/uint32.md +63 -28
- package/uint64.gr +25 -5
- package/uint64.md +63 -28
- package/uint8.md +22 -22
- package/uri.gr +57 -53
- package/uri.md +11 -12
- package/wasi/file.gr +67 -59
- package/wasi/file.md +39 -39
- package/wasi/process.md +5 -5
- package/wasi/random.md +3 -3
- package/wasi/time.md +4 -4
- package/runtime/utils/printing.gr +0 -60
- package/runtime/utils/printing.md +0 -26
package/map.gr
CHANGED
|
@@ -14,7 +14,6 @@ from "option" include Option
|
|
|
14
14
|
from "runtime/dataStructures" include DataStructures
|
|
15
15
|
use DataStructures.{ allocateArray, untagSimpleNumber }
|
|
16
16
|
from "hash" include Hash
|
|
17
|
-
use Hash.{ hash }
|
|
18
17
|
from "runtime/unsafe/memory" include Memory
|
|
19
18
|
from "runtime/unsafe/wasmi32" include WasmI32
|
|
20
19
|
|
|
@@ -27,6 +26,7 @@ record rec Bucket<k, v> {
|
|
|
27
26
|
|
|
28
27
|
abstract record Map<k, v> {
|
|
29
28
|
mut size: Number,
|
|
29
|
+
hashInstance: Hash.HashInstance,
|
|
30
30
|
mut buckets: Array<Option<Bucket<k, v>>>,
|
|
31
31
|
}
|
|
32
32
|
|
|
@@ -47,25 +47,28 @@ provide record InternalMapStats {
|
|
|
47
47
|
* @param size: The initial storage size of the map
|
|
48
48
|
* @returns An empty map with the given initial storage size
|
|
49
49
|
*
|
|
50
|
+
* @throws Failure(String): If WASI random_get fails
|
|
51
|
+
*
|
|
50
52
|
* @since v0.2.0
|
|
51
53
|
* @history v0.6.0: Merged with `makeSized`; modified signature to accept size
|
|
52
54
|
*/
|
|
53
55
|
provide let make = (size=16) => { // TODO: This could take an `eq` function to custom comparisons
|
|
54
56
|
let buckets = Array.make(size, None)
|
|
55
|
-
|
|
57
|
+
let hashInstance = Hash.make()
|
|
58
|
+
{ size: 0, hashInstance, buckets }
|
|
56
59
|
}
|
|
57
60
|
|
|
58
|
-
let getBucketIndex = (key, buckets) => {
|
|
61
|
+
let getBucketIndex = (hashInstance, key, buckets) => {
|
|
59
62
|
let bucketsLength = Array.length(buckets)
|
|
60
|
-
let hashedKey = hash(key)
|
|
63
|
+
let hashedKey = Hash.hash(hashInstance, key)
|
|
61
64
|
hashedKey % bucketsLength
|
|
62
65
|
}
|
|
63
66
|
|
|
64
|
-
let rec copyNodeWithNewHash = (oldNode, next, tail) => {
|
|
67
|
+
let rec copyNodeWithNewHash = (hashInstance, oldNode, next, tail) => {
|
|
65
68
|
match (oldNode) {
|
|
66
69
|
None => void,
|
|
67
70
|
Some(node) => {
|
|
68
|
-
let idx = getBucketIndex(node.key, next)
|
|
71
|
+
let idx = getBucketIndex(hashInstance, node.key, next)
|
|
69
72
|
let newNode = Some(node)
|
|
70
73
|
match (tail[idx]) {
|
|
71
74
|
None => {
|
|
@@ -79,7 +82,7 @@ let rec copyNodeWithNewHash = (oldNode, next, tail) => {
|
|
|
79
82
|
// Always place this node as the new tail
|
|
80
83
|
tail[idx] = newNode
|
|
81
84
|
// Recurse with the next node
|
|
82
|
-
copyNodeWithNewHash(node.next, next, tail)
|
|
85
|
+
copyNodeWithNewHash(hashInstance, node.next, next, tail)
|
|
83
86
|
},
|
|
84
87
|
}
|
|
85
88
|
}
|
|
@@ -93,8 +96,9 @@ let resize = map => {
|
|
|
93
96
|
// This tracks the tail nodes so we can set their `next` to None
|
|
94
97
|
let tailNodes = Array.make(nextSize, None)
|
|
95
98
|
map.buckets = nextBuckets
|
|
99
|
+
let hashInstance = map.hashInstance
|
|
96
100
|
Array.forEach(old => {
|
|
97
|
-
copyNodeWithNewHash(old, nextBuckets, tailNodes)
|
|
101
|
+
copyNodeWithNewHash(hashInstance, old, nextBuckets, tailNodes)
|
|
98
102
|
}, currentBuckets)
|
|
99
103
|
Array.forEach(tail => {
|
|
100
104
|
match (tail) {
|
|
@@ -132,7 +136,8 @@ let rec replaceInBucket = (key, value, node) => {
|
|
|
132
136
|
*/
|
|
133
137
|
provide let set = (key, value, map) => {
|
|
134
138
|
let buckets = map.buckets
|
|
135
|
-
let
|
|
139
|
+
let hashInstance = map.hashInstance
|
|
140
|
+
let idx = getBucketIndex(hashInstance, key, buckets)
|
|
136
141
|
let bucket = buckets[idx]
|
|
137
142
|
match (bucket) {
|
|
138
143
|
None => {
|
|
@@ -176,7 +181,8 @@ let rec valueFromBucket = (key, node) => {
|
|
|
176
181
|
*/
|
|
177
182
|
provide let get = (key, map) => {
|
|
178
183
|
let buckets = map.buckets
|
|
179
|
-
let
|
|
184
|
+
let hashInstance = map.hashInstance
|
|
185
|
+
let idx = getBucketIndex(hashInstance, key, buckets)
|
|
180
186
|
let bucket = buckets[idx]
|
|
181
187
|
match (bucket) {
|
|
182
188
|
None => None,
|
|
@@ -206,7 +212,8 @@ let rec nodeInBucket = (key, node) => {
|
|
|
206
212
|
*/
|
|
207
213
|
provide let contains = (key, map) => {
|
|
208
214
|
let buckets = map.buckets
|
|
209
|
-
let
|
|
215
|
+
let hashInstance = map.hashInstance
|
|
216
|
+
let idx = getBucketIndex(hashInstance, key, buckets)
|
|
210
217
|
let bucket = buckets[idx]
|
|
211
218
|
match (bucket) {
|
|
212
219
|
None => false,
|
|
@@ -238,7 +245,8 @@ let rec removeInBucket = (key, node) => {
|
|
|
238
245
|
*/
|
|
239
246
|
provide let remove = (key, map) => {
|
|
240
247
|
let buckets = map.buckets
|
|
241
|
-
let
|
|
248
|
+
let hashInstance = map.hashInstance
|
|
249
|
+
let idx = getBucketIndex(hashInstance, key, buckets)
|
|
242
250
|
let bucket = buckets[idx]
|
|
243
251
|
match (bucket) {
|
|
244
252
|
None => void,
|
package/map.md
CHANGED
|
@@ -55,7 +55,7 @@ Functions and constants included in the Map module.
|
|
|
55
55
|
</details>
|
|
56
56
|
|
|
57
57
|
```grain
|
|
58
|
-
make
|
|
58
|
+
make: (?size: Number) => Map<a, b>
|
|
59
59
|
```
|
|
60
60
|
|
|
61
61
|
Creates a new empty map with an initial storage of the given size. As
|
|
@@ -75,6 +75,12 @@ Returns:
|
|
|
75
75
|
|----|-----------|
|
|
76
76
|
|`Map<a, b>`|An empty map with the given initial storage size|
|
|
77
77
|
|
|
78
|
+
Throws:
|
|
79
|
+
|
|
80
|
+
`Failure(String)`
|
|
81
|
+
|
|
82
|
+
* If WASI random_get fails
|
|
83
|
+
|
|
78
84
|
### Map.**set**
|
|
79
85
|
|
|
80
86
|
<details disabled>
|
|
@@ -83,7 +89,7 @@ No other changes yet.
|
|
|
83
89
|
</details>
|
|
84
90
|
|
|
85
91
|
```grain
|
|
86
|
-
set
|
|
92
|
+
set: (key: a, value: b, map: Map<a, b>) => Void
|
|
87
93
|
```
|
|
88
94
|
|
|
89
95
|
Adds a new key-value pair to the map. If the key already exists in the map, the value is replaced.
|
|
@@ -104,7 +110,7 @@ No other changes yet.
|
|
|
104
110
|
</details>
|
|
105
111
|
|
|
106
112
|
```grain
|
|
107
|
-
get
|
|
113
|
+
get: (key: a, map: Map<a, b>) => Option<b>
|
|
108
114
|
```
|
|
109
115
|
|
|
110
116
|
Retrieves the value for the given key.
|
|
@@ -130,7 +136,7 @@ No other changes yet.
|
|
|
130
136
|
</details>
|
|
131
137
|
|
|
132
138
|
```grain
|
|
133
|
-
contains
|
|
139
|
+
contains: (key: a, map: Map<a, b>) => Bool
|
|
134
140
|
```
|
|
135
141
|
|
|
136
142
|
Determines if the map contains the given key. In such a case, it will always contain a value for the given key.
|
|
@@ -156,7 +162,7 @@ No other changes yet.
|
|
|
156
162
|
</details>
|
|
157
163
|
|
|
158
164
|
```grain
|
|
159
|
-
remove
|
|
165
|
+
remove: (key: a, map: Map<a, b>) => Void
|
|
160
166
|
```
|
|
161
167
|
|
|
162
168
|
Removes the given key from the map, which also removes the value. If the key pair doesn't exist, nothing happens.
|
|
@@ -176,7 +182,7 @@ No other changes yet.
|
|
|
176
182
|
</details>
|
|
177
183
|
|
|
178
184
|
```grain
|
|
179
|
-
update
|
|
185
|
+
update: (key: a, fn: (Option<b> => Option<b>), map: Map<a, b>) => Void
|
|
180
186
|
```
|
|
181
187
|
|
|
182
188
|
Updates a value in the map by calling an updater function that receives the previously stored value as an `Option` and returns the new value to be stored as an `Option`. If the key didn't exist previously, the value will be `None`. If `None` is returned from the updater function, the key-value pair is removed.
|
|
@@ -197,7 +203,7 @@ No other changes yet.
|
|
|
197
203
|
</details>
|
|
198
204
|
|
|
199
205
|
```grain
|
|
200
|
-
size
|
|
206
|
+
size: (map: Map<a, b>) => Number
|
|
201
207
|
```
|
|
202
208
|
|
|
203
209
|
Provides the count of key-value pairs stored within the map.
|
|
@@ -222,7 +228,7 @@ No other changes yet.
|
|
|
222
228
|
</details>
|
|
223
229
|
|
|
224
230
|
```grain
|
|
225
|
-
isEmpty
|
|
231
|
+
isEmpty: (map: Map<a, b>) => Bool
|
|
226
232
|
```
|
|
227
233
|
|
|
228
234
|
Determines if the map contains no key-value pairs.
|
|
@@ -247,7 +253,7 @@ No other changes yet.
|
|
|
247
253
|
</details>
|
|
248
254
|
|
|
249
255
|
```grain
|
|
250
|
-
clear
|
|
256
|
+
clear: (map: Map<a, b>) => Void
|
|
251
257
|
```
|
|
252
258
|
|
|
253
259
|
Resets the map by removing all key-value pairs.
|
|
@@ -273,7 +279,7 @@ Parameters:
|
|
|
273
279
|
</details>
|
|
274
280
|
|
|
275
281
|
```grain
|
|
276
|
-
forEach
|
|
282
|
+
forEach: (fn: ((a, b) => Void), map: Map<a, b>) => Void
|
|
277
283
|
```
|
|
278
284
|
|
|
279
285
|
Iterates the map, calling an iterator function with each key and value.
|
|
@@ -293,7 +299,7 @@ No other changes yet.
|
|
|
293
299
|
</details>
|
|
294
300
|
|
|
295
301
|
```grain
|
|
296
|
-
reduce
|
|
302
|
+
reduce: (fn: ((a, b, c) => a), init: a, map: Map<b, c>) => a
|
|
297
303
|
```
|
|
298
304
|
|
|
299
305
|
Combines all key-value pairs of a map using a reducer function.
|
|
@@ -320,7 +326,7 @@ No other changes yet.
|
|
|
320
326
|
</details>
|
|
321
327
|
|
|
322
328
|
```grain
|
|
323
|
-
keys
|
|
329
|
+
keys: (map: Map<a, b>) => List<a>
|
|
324
330
|
```
|
|
325
331
|
|
|
326
332
|
Enumerates all keys in the given map.
|
|
@@ -345,7 +351,7 @@ No other changes yet.
|
|
|
345
351
|
</details>
|
|
346
352
|
|
|
347
353
|
```grain
|
|
348
|
-
values
|
|
354
|
+
values: (map: Map<a, b>) => List<b>
|
|
349
355
|
```
|
|
350
356
|
|
|
351
357
|
Enumerates all values in the given map.
|
|
@@ -370,7 +376,7 @@ No other changes yet.
|
|
|
370
376
|
</details>
|
|
371
377
|
|
|
372
378
|
```grain
|
|
373
|
-
toList
|
|
379
|
+
toList: (map: Map<a, b>) => List<(a, b)>
|
|
374
380
|
```
|
|
375
381
|
|
|
376
382
|
Enumerates all key-value pairs in the given map.
|
|
@@ -395,7 +401,7 @@ No other changes yet.
|
|
|
395
401
|
</details>
|
|
396
402
|
|
|
397
403
|
```grain
|
|
398
|
-
fromList
|
|
404
|
+
fromList: (list: List<(a, b)>) => Map<a, b>
|
|
399
405
|
```
|
|
400
406
|
|
|
401
407
|
Creates a map from a list.
|
|
@@ -420,7 +426,7 @@ No other changes yet.
|
|
|
420
426
|
</details>
|
|
421
427
|
|
|
422
428
|
```grain
|
|
423
|
-
toArray
|
|
429
|
+
toArray: (map: Map<a, b>) => Array<(a, b)>
|
|
424
430
|
```
|
|
425
431
|
|
|
426
432
|
Converts a map into an array of its key-value pairs.
|
|
@@ -445,7 +451,7 @@ No other changes yet.
|
|
|
445
451
|
</details>
|
|
446
452
|
|
|
447
453
|
```grain
|
|
448
|
-
fromArray
|
|
454
|
+
fromArray: (array: Array<(a, b)>) => Map<a, b>
|
|
449
455
|
```
|
|
450
456
|
|
|
451
457
|
Creates a map from an array.
|
|
@@ -470,7 +476,7 @@ No other changes yet.
|
|
|
470
476
|
</details>
|
|
471
477
|
|
|
472
478
|
```grain
|
|
473
|
-
filter
|
|
479
|
+
filter: (fn: ((a, b) => Bool), map: Map<a, b>) => Void
|
|
474
480
|
```
|
|
475
481
|
|
|
476
482
|
Removes key-value pairs from a map where a predicate function returns `false`.
|
|
@@ -490,7 +496,7 @@ No other changes yet.
|
|
|
490
496
|
</details>
|
|
491
497
|
|
|
492
498
|
```grain
|
|
493
|
-
reject
|
|
499
|
+
reject: (fn: ((a, b) => Bool), map: Map<a, b>) => Void
|
|
494
500
|
```
|
|
495
501
|
|
|
496
502
|
Removes key-value pairs from a map where a predicate function returns `true`.
|
|
@@ -517,7 +523,7 @@ Parameters:
|
|
|
517
523
|
</details>
|
|
518
524
|
|
|
519
525
|
```grain
|
|
520
|
-
getInternalStats
|
|
526
|
+
getInternalStats: (map: Map<a, b>) => InternalMapStats
|
|
521
527
|
```
|
|
522
528
|
|
|
523
529
|
Provides data representing the internal state state of the map.
|
|
@@ -579,7 +585,7 @@ Functions and constants included in the Map.Immutable module.
|
|
|
579
585
|
</details>
|
|
580
586
|
|
|
581
587
|
```grain
|
|
582
|
-
empty
|
|
588
|
+
empty: Map<a, b>
|
|
583
589
|
```
|
|
584
590
|
|
|
585
591
|
An empty map
|
|
@@ -599,7 +605,7 @@ An empty map
|
|
|
599
605
|
</details>
|
|
600
606
|
|
|
601
607
|
```grain
|
|
602
|
-
size
|
|
608
|
+
size: (map: Map<a, b>) => Number
|
|
603
609
|
```
|
|
604
610
|
|
|
605
611
|
Provides the count of key-value pairs stored within the map.
|
|
@@ -631,7 +637,7 @@ Returns:
|
|
|
631
637
|
</details>
|
|
632
638
|
|
|
633
639
|
```grain
|
|
634
|
-
isEmpty
|
|
640
|
+
isEmpty: (map: Map<a, b>) => Bool
|
|
635
641
|
```
|
|
636
642
|
|
|
637
643
|
Determines if the map contains no key-value pairs.
|
|
@@ -663,7 +669,7 @@ Returns:
|
|
|
663
669
|
</details>
|
|
664
670
|
|
|
665
671
|
```grain
|
|
666
|
-
set
|
|
672
|
+
set: (key: a, value: b, map: Map<a, b>) => Map<a, b>
|
|
667
673
|
```
|
|
668
674
|
|
|
669
675
|
Produces a new map containing a new key-value pair. If the key already exists in the map, the value is replaced.
|
|
@@ -697,7 +703,7 @@ Returns:
|
|
|
697
703
|
</details>
|
|
698
704
|
|
|
699
705
|
```grain
|
|
700
|
-
get
|
|
706
|
+
get: (key: a, map: Map<a, b>) => Option<b>
|
|
701
707
|
```
|
|
702
708
|
|
|
703
709
|
Retrieves the value for the given key.
|
|
@@ -730,7 +736,7 @@ Returns:
|
|
|
730
736
|
</details>
|
|
731
737
|
|
|
732
738
|
```grain
|
|
733
|
-
contains
|
|
739
|
+
contains: (key: a, map: Map<a, b>) => Bool
|
|
734
740
|
```
|
|
735
741
|
|
|
736
742
|
Determines if the map contains the given key. In such a case, it will always contain a value for the given key.
|
|
@@ -763,7 +769,7 @@ Returns:
|
|
|
763
769
|
</details>
|
|
764
770
|
|
|
765
771
|
```grain
|
|
766
|
-
remove
|
|
772
|
+
remove: (key: a, map: Map<a, b>) => Map<a, b>
|
|
767
773
|
```
|
|
768
774
|
|
|
769
775
|
Produces a new map without the key-value pair corresponding to the given
|
|
@@ -797,7 +803,7 @@ Returns:
|
|
|
797
803
|
</details>
|
|
798
804
|
|
|
799
805
|
```grain
|
|
800
|
-
update
|
|
806
|
+
update: (key: a, fn: (Option<b> => Option<b>), map: Map<a, b>) => Map<a, b>
|
|
801
807
|
```
|
|
802
808
|
|
|
803
809
|
Produces a new map by calling an updater function that receives the
|
|
@@ -835,7 +841,7 @@ Returns:
|
|
|
835
841
|
</details>
|
|
836
842
|
|
|
837
843
|
```grain
|
|
838
|
-
forEach
|
|
844
|
+
forEach: (fn: ((a, b) => Void), map: Map<a, b>) => Void
|
|
839
845
|
```
|
|
840
846
|
|
|
841
847
|
Iterates the map, calling an iterator function with each key and value.
|
|
@@ -862,7 +868,7 @@ Parameters:
|
|
|
862
868
|
</details>
|
|
863
869
|
|
|
864
870
|
```grain
|
|
865
|
-
reduce
|
|
871
|
+
reduce: (fn: ((a, b, c) => a), init: a, map: Map<b, c>) => a
|
|
866
872
|
```
|
|
867
873
|
|
|
868
874
|
Combines all key-value pairs of a map using a reducer function.
|
|
@@ -896,7 +902,7 @@ Returns:
|
|
|
896
902
|
</details>
|
|
897
903
|
|
|
898
904
|
```grain
|
|
899
|
-
keys
|
|
905
|
+
keys: (map: Map<a, b>) => List<a>
|
|
900
906
|
```
|
|
901
907
|
|
|
902
908
|
Enumerates all keys in the given map.
|
|
@@ -928,7 +934,7 @@ Returns:
|
|
|
928
934
|
</details>
|
|
929
935
|
|
|
930
936
|
```grain
|
|
931
|
-
values
|
|
937
|
+
values: (map: Map<a, b>) => List<b>
|
|
932
938
|
```
|
|
933
939
|
|
|
934
940
|
Enumerates all values in the given map.
|
|
@@ -960,7 +966,7 @@ Returns:
|
|
|
960
966
|
</details>
|
|
961
967
|
|
|
962
968
|
```grain
|
|
963
|
-
filter
|
|
969
|
+
filter: (fn: ((a, b) => Bool), map: Map<a, b>) => Map<a, b>
|
|
964
970
|
```
|
|
965
971
|
|
|
966
972
|
Produces a new map excluding the key-value pairs where a predicate function returns `false`.
|
|
@@ -993,7 +999,7 @@ Returns:
|
|
|
993
999
|
</details>
|
|
994
1000
|
|
|
995
1001
|
```grain
|
|
996
|
-
reject
|
|
1002
|
+
reject: (fn: ((a, b) => Bool), map: Map<a, b>) => Map<a, b>
|
|
997
1003
|
```
|
|
998
1004
|
|
|
999
1005
|
Produces a new map excluding the key-value pairs where a predicate function returns `true`.
|
|
@@ -1026,7 +1032,7 @@ Returns:
|
|
|
1026
1032
|
</details>
|
|
1027
1033
|
|
|
1028
1034
|
```grain
|
|
1029
|
-
fromList
|
|
1035
|
+
fromList: (list: List<(a, b)>) => Map<a, b>
|
|
1030
1036
|
```
|
|
1031
1037
|
|
|
1032
1038
|
Creates a map from a list.
|
|
@@ -1058,7 +1064,7 @@ Returns:
|
|
|
1058
1064
|
</details>
|
|
1059
1065
|
|
|
1060
1066
|
```grain
|
|
1061
|
-
toList
|
|
1067
|
+
toList: (map: Map<a, b>) => List<(a, b)>
|
|
1062
1068
|
```
|
|
1063
1069
|
|
|
1064
1070
|
Enumerates all key-value pairs in the given map.
|
|
@@ -1090,7 +1096,7 @@ Returns:
|
|
|
1090
1096
|
</details>
|
|
1091
1097
|
|
|
1092
1098
|
```grain
|
|
1093
|
-
fromArray
|
|
1099
|
+
fromArray: (array: Array<(a, b)>) => Map<a, b>
|
|
1094
1100
|
```
|
|
1095
1101
|
|
|
1096
1102
|
Creates a map from an array.
|
|
@@ -1122,7 +1128,7 @@ Returns:
|
|
|
1122
1128
|
</details>
|
|
1123
1129
|
|
|
1124
1130
|
```grain
|
|
1125
|
-
toArray
|
|
1131
|
+
toArray: (map: Map<a, b>) => Array<(a, b)>
|
|
1126
1132
|
```
|
|
1127
1133
|
|
|
1128
1134
|
Converts a map into an array of its key-value pairs.
|
package/marshal.gr
CHANGED
|
@@ -75,8 +75,8 @@ let rec size = (value, acc, valuesSeen, toplevel) => {
|
|
|
75
75
|
}
|
|
76
76
|
let heapPtr = value
|
|
77
77
|
match (load(heapPtr, 0n)) {
|
|
78
|
-
t when t == Tags._GRAIN_STRING_HEAP_TAG
|
|
79
|
-
t == Tags._GRAIN_BYTES_HEAP_TAG => {
|
|
78
|
+
t when t == Tags._GRAIN_STRING_HEAP_TAG
|
|
79
|
+
|| t == Tags._GRAIN_BYTES_HEAP_TAG => {
|
|
80
80
|
acc + roundTo8(8n + load(heapPtr, 4n))
|
|
81
81
|
},
|
|
82
82
|
t when t == Tags._GRAIN_ADT_HEAP_TAG => {
|
|
@@ -142,27 +142,27 @@ let rec size = (value, acc, valuesSeen, toplevel) => {
|
|
|
142
142
|
t when t == Tags._GRAIN_BOXED_NUM_HEAP_TAG => {
|
|
143
143
|
let tag = load(heapPtr, 4n)
|
|
144
144
|
match (tag) {
|
|
145
|
-
t when t == Tags._GRAIN_INT64_BOXED_NUM_TAG
|
|
146
|
-
t == Tags._GRAIN_FLOAT64_BOXED_NUM_TAG => {
|
|
145
|
+
t when t == Tags._GRAIN_INT64_BOXED_NUM_TAG
|
|
146
|
+
|| t == Tags._GRAIN_FLOAT64_BOXED_NUM_TAG => {
|
|
147
147
|
acc + 16n
|
|
148
148
|
},
|
|
149
149
|
t when t == Tags._GRAIN_BIGINT_BOXED_NUM_TAG => {
|
|
150
150
|
acc + 16n + load(heapPtr, 8n) * 8n
|
|
151
151
|
},
|
|
152
152
|
t when t == Tags._GRAIN_RATIONAL_BOXED_NUM_TAG => {
|
|
153
|
-
acc
|
|
154
|
-
16n
|
|
155
|
-
size(load(value, 8n), 0n, valuesSeen, false)
|
|
156
|
-
size(load(value, 12n), 0n, valuesSeen, false)
|
|
153
|
+
acc
|
|
154
|
+
+ 16n
|
|
155
|
+
+ size(load(value, 8n), 0n, valuesSeen, false)
|
|
156
|
+
+ size(load(value, 12n), 0n, valuesSeen, false)
|
|
157
157
|
},
|
|
158
158
|
_ => {
|
|
159
159
|
fail "Unknown number type"
|
|
160
160
|
},
|
|
161
161
|
}
|
|
162
162
|
},
|
|
163
|
-
t when t == Tags._GRAIN_INT32_HEAP_TAG
|
|
164
|
-
t == Tags._GRAIN_FLOAT32_HEAP_TAG
|
|
165
|
-
t == Tags._GRAIN_UINT32_HEAP_TAG => {
|
|
163
|
+
t when t == Tags._GRAIN_INT32_HEAP_TAG
|
|
164
|
+
|| t == Tags._GRAIN_FLOAT32_HEAP_TAG
|
|
165
|
+
|| t == Tags._GRAIN_UINT32_HEAP_TAG => {
|
|
166
166
|
acc + 8n
|
|
167
167
|
},
|
|
168
168
|
t when t == Tags._GRAIN_UINT64_HEAP_TAG => {
|
|
@@ -386,9 +386,9 @@ let rec marshalHeap = (heapPtr, buf, offset, valuesSeen) => {
|
|
|
386
386
|
},
|
|
387
387
|
}
|
|
388
388
|
},
|
|
389
|
-
t when t == Tags._GRAIN_INT32_HEAP_TAG
|
|
390
|
-
t == Tags._GRAIN_FLOAT32_HEAP_TAG
|
|
391
|
-
t == Tags._GRAIN_UINT32_HEAP_TAG => {
|
|
389
|
+
t when t == Tags._GRAIN_INT32_HEAP_TAG
|
|
390
|
+
|| t == Tags._GRAIN_FLOAT32_HEAP_TAG
|
|
391
|
+
|| t == Tags._GRAIN_UINT32_HEAP_TAG => {
|
|
392
392
|
Memory.copy(buf + offset, heapPtr, 8n)
|
|
393
393
|
offset + 8n
|
|
394
394
|
},
|
|
@@ -449,11 +449,11 @@ let reportError = (message, offset) => {
|
|
|
449
449
|
@unsafe
|
|
450
450
|
let validateStack = (value, offset) => {
|
|
451
451
|
match (value) {
|
|
452
|
-
_ when value == fromGrain(true)
|
|
453
|
-
value == fromGrain(false)
|
|
454
|
-
value == fromGrain(void)
|
|
455
|
-
(value & Tags._GRAIN_NUMBER_TAG_MASK) == Tags._GRAIN_NUMBER_TAG_TYPE
|
|
456
|
-
(value & Tags._GRAIN_GENERIC_TAG_MASK) == Tags._GRAIN_SHORTVAL_TAG_TYPE =>
|
|
452
|
+
_ when value == fromGrain(true)
|
|
453
|
+
|| value == fromGrain(false)
|
|
454
|
+
|| value == fromGrain(void)
|
|
455
|
+
|| (value & Tags._GRAIN_NUMBER_TAG_MASK) == Tags._GRAIN_NUMBER_TAG_TYPE
|
|
456
|
+
|| (value & Tags._GRAIN_GENERIC_TAG_MASK) == Tags._GRAIN_SHORTVAL_TAG_TYPE =>
|
|
457
457
|
None,
|
|
458
458
|
_ => reportError("Unknown value", offset),
|
|
459
459
|
}
|
|
@@ -658,9 +658,9 @@ let rec validateHeap = (buf, bufSize, offset, valuesChecked) => {
|
|
|
658
658
|
None => void,
|
|
659
659
|
}
|
|
660
660
|
if (
|
|
661
|
-
load(buf, numeratorOffset) != Tags._GRAIN_BOXED_NUM_HEAP_TAG
|
|
662
|
-
load(buf, numeratorOffset + 4n)
|
|
663
|
-
Tags._GRAIN_BIGINT_BOXED_NUM_TAG
|
|
661
|
+
load(buf, numeratorOffset) != Tags._GRAIN_BOXED_NUM_HEAP_TAG
|
|
662
|
+
&& load(buf, numeratorOffset + 4n)
|
|
663
|
+
!= Tags._GRAIN_BIGINT_BOXED_NUM_TAG
|
|
664
664
|
) {
|
|
665
665
|
return reportError(
|
|
666
666
|
"Rational/Number numerator was not in the expected format",
|
|
@@ -668,9 +668,9 @@ let rec validateHeap = (buf, bufSize, offset, valuesChecked) => {
|
|
|
668
668
|
)
|
|
669
669
|
}
|
|
670
670
|
if (
|
|
671
|
-
load(buf, denominatorOffset) != Tags._GRAIN_BOXED_NUM_HEAP_TAG
|
|
672
|
-
load(buf, denominatorOffset + 4n)
|
|
673
|
-
Tags._GRAIN_BIGINT_BOXED_NUM_TAG
|
|
671
|
+
load(buf, denominatorOffset) != Tags._GRAIN_BOXED_NUM_HEAP_TAG
|
|
672
|
+
&& load(buf, denominatorOffset + 4n)
|
|
673
|
+
!= Tags._GRAIN_BIGINT_BOXED_NUM_TAG
|
|
674
674
|
) {
|
|
675
675
|
return reportError(
|
|
676
676
|
"Rational/Number denominator was not in the expected format",
|
|
@@ -682,9 +682,9 @@ let rec validateHeap = (buf, bufSize, offset, valuesChecked) => {
|
|
|
682
682
|
None => void,
|
|
683
683
|
}
|
|
684
684
|
if (
|
|
685
|
-
load(buf, numeratorOffset) != Tags._GRAIN_BOXED_NUM_HEAP_TAG
|
|
686
|
-
load(buf, numeratorOffset + 4n)
|
|
687
|
-
Tags._GRAIN_BIGINT_BOXED_NUM_TAG
|
|
685
|
+
load(buf, numeratorOffset) != Tags._GRAIN_BOXED_NUM_HEAP_TAG
|
|
686
|
+
&& load(buf, numeratorOffset + 4n)
|
|
687
|
+
!= Tags._GRAIN_BIGINT_BOXED_NUM_TAG
|
|
688
688
|
) {
|
|
689
689
|
return reportError(
|
|
690
690
|
"Rational/Number numerator was not in the expected format",
|
|
@@ -692,9 +692,9 @@ let rec validateHeap = (buf, bufSize, offset, valuesChecked) => {
|
|
|
692
692
|
)
|
|
693
693
|
}
|
|
694
694
|
let denominatorError = if (
|
|
695
|
-
load(buf, denominatorOffset) != Tags._GRAIN_BOXED_NUM_HEAP_TAG
|
|
696
|
-
load(buf, denominatorOffset + 4n)
|
|
697
|
-
Tags._GRAIN_BIGINT_BOXED_NUM_TAG
|
|
695
|
+
load(buf, denominatorOffset) != Tags._GRAIN_BOXED_NUM_HEAP_TAG
|
|
696
|
+
&& load(buf, denominatorOffset + 4n)
|
|
697
|
+
!= Tags._GRAIN_BIGINT_BOXED_NUM_TAG
|
|
698
698
|
) {
|
|
699
699
|
return reportError(
|
|
700
700
|
"Rational/Number denominator was not in the expected format",
|
|
@@ -754,12 +754,13 @@ let validate = (buf, bufSize) => {
|
|
|
754
754
|
} else {
|
|
755
755
|
// Handle non-heap values: booleans, chars, void, etc.
|
|
756
756
|
match (value) {
|
|
757
|
-
_ when value == fromGrain(true)
|
|
758
|
-
value == fromGrain(false)
|
|
759
|
-
value == fromGrain(void)
|
|
760
|
-
(value & Tags._GRAIN_NUMBER_TAG_MASK)
|
|
761
|
-
|
|
762
|
-
|
|
757
|
+
_ when value == fromGrain(true)
|
|
758
|
+
|| value == fromGrain(false)
|
|
759
|
+
|| value == fromGrain(void)
|
|
760
|
+
|| (value & Tags._GRAIN_NUMBER_TAG_MASK)
|
|
761
|
+
== Tags._GRAIN_NUMBER_TAG_TYPE
|
|
762
|
+
|| (value & Tags._GRAIN_GENERIC_TAG_MASK)
|
|
763
|
+
== Tags._GRAIN_SHORTVAL_TAG_TYPE => None,
|
|
763
764
|
_ => reportError("Unknown value", 0n),
|
|
764
765
|
}
|
|
765
766
|
}
|
|
@@ -1006,9 +1007,9 @@ let rec unmarshalHeap = (buf, offset, valuesUnmarshaled) => {
|
|
|
1006
1007
|
},
|
|
1007
1008
|
}
|
|
1008
1009
|
},
|
|
1009
|
-
t when t == Tags._GRAIN_INT32_HEAP_TAG
|
|
1010
|
-
t == Tags._GRAIN_FLOAT32_HEAP_TAG
|
|
1011
|
-
t == Tags._GRAIN_UINT32_HEAP_TAG => {
|
|
1010
|
+
t when t == Tags._GRAIN_INT32_HEAP_TAG
|
|
1011
|
+
|| t == Tags._GRAIN_FLOAT32_HEAP_TAG
|
|
1012
|
+
|| t == Tags._GRAIN_UINT32_HEAP_TAG => {
|
|
1012
1013
|
let value = Memory.malloc(8n)
|
|
1013
1014
|
Memory.copy(value, valuePtr, 8n)
|
|
1014
1015
|
|
package/marshal.md
CHANGED
|
@@ -37,7 +37,7 @@ No other changes yet.
|
|
|
37
37
|
</details>
|
|
38
38
|
|
|
39
39
|
```grain
|
|
40
|
-
marshal
|
|
40
|
+
marshal: (value: a) => Bytes
|
|
41
41
|
```
|
|
42
42
|
|
|
43
43
|
Serialize a value into a byte-based representation suitable for transmission
|
|
@@ -74,7 +74,7 @@ No other changes yet.
|
|
|
74
74
|
</details>
|
|
75
75
|
|
|
76
76
|
```grain
|
|
77
|
-
unmarshal
|
|
77
|
+
unmarshal: (bytes: Bytes) => Result<a, String>
|
|
78
78
|
```
|
|
79
79
|
|
|
80
80
|
Deserialize the byte-based representation of a value back into an in-memory
|