@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.
Files changed (61) hide show
  1. package/CHANGELOG.md +52 -0
  2. package/LICENSE +1 -1
  3. package/array.gr +200 -89
  4. package/array.md +81 -5
  5. package/buffer.gr +93 -36
  6. package/bytes.gr +10 -10
  7. package/char.gr +112 -56
  8. package/char.md +200 -0
  9. package/float32.gr +120 -4
  10. package/float32.md +315 -0
  11. package/float64.gr +120 -4
  12. package/float64.md +315 -0
  13. package/hash.gr +42 -15
  14. package/hash.md +44 -0
  15. package/int32.gr +370 -75
  16. package/int32.md +833 -0
  17. package/int64.gr +370 -75
  18. package/int64.md +833 -0
  19. package/list.gr +121 -50
  20. package/map.gr +106 -110
  21. package/number.gr +37 -1
  22. package/number.md +66 -0
  23. package/option.gr +260 -53
  24. package/option.md +579 -0
  25. package/package.json +1 -1
  26. package/pervasives.gr +32 -20
  27. package/queue.gr +102 -30
  28. package/queue.md +191 -0
  29. package/range.gr +26 -26
  30. package/range.md +1 -1
  31. package/regex.md +9 -9
  32. package/result.gr +216 -70
  33. package/result.md +446 -0
  34. package/runtime/dataStructures.gr +28 -29
  35. package/runtime/debug.gr +0 -1
  36. package/runtime/equal.gr +37 -16
  37. package/runtime/exception.gr +28 -15
  38. package/runtime/gc.gr +33 -20
  39. package/runtime/malloc.gr +19 -11
  40. package/runtime/numberUtils.gr +208 -103
  41. package/runtime/numbers.gr +217 -118
  42. package/runtime/string.gr +98 -39
  43. package/runtime/stringUtils.gr +176 -0
  44. package/runtime/unsafe/conv.gr +10 -10
  45. package/runtime/unsafe/memory.gr +14 -3
  46. package/runtime/unsafe/printWasm.gr +4 -4
  47. package/runtime/unsafe/tags.gr +2 -2
  48. package/runtime/unsafe/wasmf32.gr +9 -2
  49. package/runtime/unsafe/wasmf64.gr +9 -2
  50. package/runtime/unsafe/wasmi32.gr +65 -47
  51. package/runtime/unsafe/wasmi64.gr +78 -50
  52. package/runtime/wasi.gr +199 -45
  53. package/set.gr +281 -119
  54. package/set.md +502 -0
  55. package/stack.gr +26 -26
  56. package/string.gr +657 -341
  57. package/string.md +815 -0
  58. package/sys/file.gr +356 -177
  59. package/sys/process.gr +10 -6
  60. package/sys/random.gr +3 -6
  61. package/sys/time.gr +3 -3
package/float64.md ADDED
@@ -0,0 +1,315 @@
1
+ ---
2
+ title: Float64
3
+ ---
4
+
5
+ Utilities for working with the Float64 type.
6
+
7
+ <details disabled>
8
+ <summary tabindex="-1">Added in <code>0.2.0</code></summary>
9
+ No other changes yet.
10
+ </details>
11
+
12
+ ```grain
13
+ import Float64 from "float64"
14
+ ```
15
+
16
+ ## Conversions
17
+
18
+ Functions for converting between Numbers and the Float64 type.
19
+
20
+ ### Float64.**fromNumber**
21
+
22
+ <details disabled>
23
+ <summary tabindex="-1">Added in <code>0.2.0</code></summary>
24
+ No other changes yet.
25
+ </details>
26
+
27
+ ```grain
28
+ fromNumber : Number -> Float64
29
+ ```
30
+
31
+ Converts a Number to a Float64.
32
+
33
+ Parameters:
34
+
35
+ |param|type|description|
36
+ |-----|----|-----------|
37
+ |`number`|`Number`|The value to convert|
38
+
39
+ Returns:
40
+
41
+ |type|description|
42
+ |----|-----------|
43
+ |`Float64`|The Number represented as a Float64|
44
+
45
+ ### Float64.**toNumber**
46
+
47
+ <details disabled>
48
+ <summary tabindex="-1">Added in <code>0.2.0</code></summary>
49
+ No other changes yet.
50
+ </details>
51
+
52
+ ```grain
53
+ toNumber : Float64 -> Number
54
+ ```
55
+
56
+ Converts a Float64 to a Number.
57
+
58
+ Parameters:
59
+
60
+ |param|type|description|
61
+ |-----|----|-----------|
62
+ |`float`|`Float64`|The value to convert|
63
+
64
+ Returns:
65
+
66
+ |type|description|
67
+ |----|-----------|
68
+ |`Number`|The Float64 represented as a Number|
69
+
70
+ ## Operations
71
+
72
+ Mathematical operations for Float64 values.
73
+
74
+ ### Float64.**add**
75
+
76
+ <details disabled>
77
+ <summary tabindex="-1">Added in <code>0.2.0</code></summary>
78
+ No other changes yet.
79
+ </details>
80
+
81
+ ```grain
82
+ add : (Float64, Float64) -> Float64
83
+ ```
84
+
85
+ Computes the sum of its operands.
86
+
87
+ Parameters:
88
+
89
+ |param|type|description|
90
+ |-----|----|-----------|
91
+ |`x`|`Float64`|The first operand|
92
+ |`y`|`Float64`|The second operand|
93
+
94
+ Returns:
95
+
96
+ |type|description|
97
+ |----|-----------|
98
+ |`Float64`|The sum of the two operands|
99
+
100
+ ### Float64.**sub**
101
+
102
+ <details disabled>
103
+ <summary tabindex="-1">Added in <code>0.2.0</code></summary>
104
+ No other changes yet.
105
+ </details>
106
+
107
+ ```grain
108
+ sub : (Float64, Float64) -> Float64
109
+ ```
110
+
111
+ Computes the difference of its operands.
112
+
113
+ Parameters:
114
+
115
+ |param|type|description|
116
+ |-----|----|-----------|
117
+ |`x`|`Float64`|The first operand|
118
+ |`y`|`Float64`|The second operand|
119
+
120
+ Returns:
121
+
122
+ |type|description|
123
+ |----|-----------|
124
+ |`Float64`|The difference of the two operands|
125
+
126
+ ### Float64.**mul**
127
+
128
+ <details disabled>
129
+ <summary tabindex="-1">Added in <code>0.2.0</code></summary>
130
+ No other changes yet.
131
+ </details>
132
+
133
+ ```grain
134
+ mul : (Float64, Float64) -> Float64
135
+ ```
136
+
137
+ Computes the product of its operands.
138
+
139
+ Parameters:
140
+
141
+ |param|type|description|
142
+ |-----|----|-----------|
143
+ |`x`|`Float64`|The first operand|
144
+ |`y`|`Float64`|The second operand|
145
+
146
+ Returns:
147
+
148
+ |type|description|
149
+ |----|-----------|
150
+ |`Float64`|The product of the two operands|
151
+
152
+ ### Float64.**div**
153
+
154
+ <details disabled>
155
+ <summary tabindex="-1">Added in <code>0.2.0</code></summary>
156
+ No other changes yet.
157
+ </details>
158
+
159
+ ```grain
160
+ div : (Float64, Float64) -> Float64
161
+ ```
162
+
163
+ Computes the quotient of its operands.
164
+
165
+ Parameters:
166
+
167
+ |param|type|description|
168
+ |-----|----|-----------|
169
+ |`x`|`Float64`|The first operand|
170
+ |`y`|`Float64`|The second operand|
171
+
172
+ Returns:
173
+
174
+ |type|description|
175
+ |----|-----------|
176
+ |`Float64`|The quotient of the two operands|
177
+
178
+ ## Comparisons
179
+
180
+ Functions for comparing Float64 values.
181
+
182
+ ### Float64.**lt**
183
+
184
+ <details disabled>
185
+ <summary tabindex="-1">Added in <code>0.2.0</code></summary>
186
+ No other changes yet.
187
+ </details>
188
+
189
+ ```grain
190
+ lt : (Float64, Float64) -> Bool
191
+ ```
192
+
193
+ Checks if the first value is less than the second value.
194
+
195
+ Parameters:
196
+
197
+ |param|type|description|
198
+ |-----|----|-----------|
199
+ |`x`|`Float64`|The first value|
200
+ |`y`|`Float64`|The second value|
201
+
202
+ Returns:
203
+
204
+ |type|description|
205
+ |----|-----------|
206
+ |`Bool`|`true` if the first value is less than the second value or `false` otherwise|
207
+
208
+ ### Float64.**gt**
209
+
210
+ <details disabled>
211
+ <summary tabindex="-1">Added in <code>0.2.0</code></summary>
212
+ No other changes yet.
213
+ </details>
214
+
215
+ ```grain
216
+ gt : (Float64, Float64) -> Bool
217
+ ```
218
+
219
+ Checks if the first value is greater than the second value.
220
+
221
+ Parameters:
222
+
223
+ |param|type|description|
224
+ |-----|----|-----------|
225
+ |`x`|`Float64`|The first value|
226
+ |`y`|`Float64`|The second value|
227
+
228
+ Returns:
229
+
230
+ |type|description|
231
+ |----|-----------|
232
+ |`Bool`|`true` if the first value is greater than the second value or `false` otherwise|
233
+
234
+ ### Float64.**lte**
235
+
236
+ <details disabled>
237
+ <summary tabindex="-1">Added in <code>0.2.0</code></summary>
238
+ No other changes yet.
239
+ </details>
240
+
241
+ ```grain
242
+ lte : (Float64, Float64) -> Bool
243
+ ```
244
+
245
+ Checks if the first value is less than or equal to the second value.
246
+
247
+ Parameters:
248
+
249
+ |param|type|description|
250
+ |-----|----|-----------|
251
+ |`x`|`Float64`|The first value|
252
+ |`y`|`Float64`|The second value|
253
+
254
+ Returns:
255
+
256
+ |type|description|
257
+ |----|-----------|
258
+ |`Bool`|`true` if the first value is less than or equal to the second value or `false` otherwise|
259
+
260
+ ### Float64.**gte**
261
+
262
+ <details disabled>
263
+ <summary tabindex="-1">Added in <code>0.2.0</code></summary>
264
+ No other changes yet.
265
+ </details>
266
+
267
+ ```grain
268
+ gte : (Float64, Float64) -> Bool
269
+ ```
270
+
271
+ Checks if the first value is greater than or equal to the second value.
272
+
273
+ Parameters:
274
+
275
+ |param|type|description|
276
+ |-----|----|-----------|
277
+ |`x`|`Float64`|The first value|
278
+ |`y`|`Float64`|The second value|
279
+
280
+ Returns:
281
+
282
+ |type|description|
283
+ |----|-----------|
284
+ |`Bool`|`true` if the first value is greater than or equal to the second value or `false` otherwise|
285
+
286
+ ## Constants
287
+
288
+ Float64 constant values.
289
+
290
+ ### Float64.**infinity**
291
+
292
+ <details disabled>
293
+ <summary tabindex="-1">Added in <code>0.4.0</code></summary>
294
+ No other changes yet.
295
+ </details>
296
+
297
+ ```grain
298
+ infinity : Float64
299
+ ```
300
+
301
+ Infinity represented as a Float64 value.
302
+
303
+ ### Float64.**nan**
304
+
305
+ <details disabled>
306
+ <summary tabindex="-1">Added in <code>0.4.0</code></summary>
307
+ No other changes yet.
308
+ </details>
309
+
310
+ ```grain
311
+ nan : Float64
312
+ ```
313
+
314
+ NaN (Not a Number) represented as a Float64 value.
315
+
package/hash.gr CHANGED
@@ -1,5 +1,12 @@
1
1
  /* grainc-flags --no-gc */
2
2
 
3
+ /**
4
+ * @module Hash: Utilities for hashing any value.
5
+ * @example import Hash from "hash"
6
+ *
7
+ * @since v0.1.0
8
+ */
9
+
3
10
  /**
4
11
  This module implements MurmurHash3 for Grain data types.
5
12
  https://en.wikipedia.org/wiki/MurmurHash
@@ -18,7 +25,7 @@ import WasmI32, {
18
25
  eq as (==),
19
26
  ne as (!=),
20
27
  gtU as (>),
21
- ltU as (<)
28
+ ltU as (<),
22
29
  } from "runtime/unsafe/wasmi32"
23
30
  import Tags from "runtime/unsafe/tags"
24
31
  import Memory from "runtime/unsafe/memory"
@@ -48,17 +55,17 @@ let n = 0xe6546b64n
48
55
 
49
56
  let mut h = seed
50
57
 
51
- let hash32 = (k) => {
58
+ let hash32 = k => {
52
59
  let mut k = k * c1
53
60
  k = WasmI32.rotl(k, r1)
54
61
  k *= c2
55
62
 
56
63
  h = h ^ k
57
64
  h = WasmI32.rotl(h, r2)
58
- h = (h * m) + n
65
+ h = h * m + n
59
66
  }
60
67
 
61
- let hashRemaining = (r) => {
68
+ let hashRemaining = r => {
62
69
  // Note: wasm is little-endian so no swap is necessary
63
70
 
64
71
  let mut r = r * c1
@@ -68,14 +75,14 @@ let hashRemaining = (r) => {
68
75
  h = h ^ r
69
76
  }
70
77
 
71
- let finalize = (len) => {
78
+ let finalize = len => {
72
79
  h = h ^ len
73
80
 
74
- h = h ^ (h >>> 16n)
81
+ h = h ^ h >>> 16n
75
82
  h *= 0x85ebca6bn
76
- h = h ^ (h >>> 13n)
83
+ h = h ^ h >>> 13n
77
84
  h *= 0xc2b2ae35n
78
- h = h ^ (h >>> 16n)
85
+ h = h ^ h >>> 16n
79
86
  }
80
87
 
81
88
  let rec hashOne = (val, depth) => {
@@ -83,10 +90,14 @@ let rec hashOne = (val, depth) => {
83
90
  void
84
91
  } else if ((val & Tags._GRAIN_NUMBER_TAG_MASK) != 0n) {
85
92
  hash32(val)
86
- } else if ((val & Tags._GRAIN_GENERIC_TAG_MASK) == Tags._GRAIN_GENERIC_HEAP_TAG_TYPE) {
93
+ } else if (
94
+ (val & Tags._GRAIN_GENERIC_TAG_MASK) == Tags._GRAIN_GENERIC_HEAP_TAG_TYPE
95
+ ) {
87
96
  let heapPtr = val
88
97
  match (WasmI32.load(heapPtr, 0n)) {
89
- t when t == Tags._GRAIN_STRING_HEAP_TAG || t == Tags._GRAIN_BYTES_HEAP_TAG => {
98
+ t when (
99
+ t == Tags._GRAIN_STRING_HEAP_TAG || t == Tags._GRAIN_BYTES_HEAP_TAG
100
+ ) => {
90
101
  let length = WasmI32.load(heapPtr, 4n)
91
102
  let extra = length % 4n
92
103
  let l = length - extra
@@ -191,12 +202,12 @@ let rec hashOne = (val, depth) => {
191
202
  },
192
203
  _ => {
193
204
  hash32(heapPtr)
194
- }
205
+ },
195
206
  }
196
207
  },
197
208
  _ => {
198
209
  hash32(heapPtr)
199
- }
210
+ },
200
211
  }
201
212
  } else if (val == WasmI32.fromGrain(true)) {
202
213
  hash32(val)
@@ -208,14 +219,30 @@ let rec hashOne = (val, depth) => {
208
219
  hash32(val)
209
220
  }
210
221
  }
222
+ /**
223
+ * @section Values: Functions for hashing.
224
+ */
211
225
 
212
- export let hash = (a) => {
226
+ /**
227
+ * A generic hash function that produces an integer from any value. If `a == b` then `Hash.hash(a) == Hash.hash(b)`.
228
+ *
229
+ * @param anything: The value to hash
230
+ * @returns A hash for the given value
231
+ *
232
+ * @since v0.1.0
233
+ */
234
+ export let rec hash = anything => {
213
235
  h = seed
214
236
 
215
- hashOne(WasmI32.fromGrain(a), 0n)
237
+ hashOne(WasmI32.fromGrain(anything), 0n)
216
238
  finalize(0n)
217
239
 
218
240
  // Tag the number on the way out.
219
241
  // Since Grain has proper modulus, negative numbers are okay.
220
- tagSimpleNumber(h)
242
+ let result = tagSimpleNumber(h)
243
+
244
+ Memory.decRef(WasmI32.fromGrain(hash))
245
+ Memory.decRef(WasmI32.fromGrain(anything))
246
+
247
+ result
221
248
  }
package/hash.md ADDED
@@ -0,0 +1,44 @@
1
+ ---
2
+ title: Hash
3
+ ---
4
+
5
+ Utilities for hashing any value.
6
+
7
+ <details disabled>
8
+ <summary tabindex="-1">Added in <code>0.1.0</code></summary>
9
+ No other changes yet.
10
+ </details>
11
+
12
+ ```grain
13
+ import Hash from "hash"
14
+ ```
15
+
16
+ ## Values
17
+
18
+ Functions for hashing.
19
+
20
+ ### Hash.**hash**
21
+
22
+ <details disabled>
23
+ <summary tabindex="-1">Added in <code>0.1.0</code></summary>
24
+ No other changes yet.
25
+ </details>
26
+
27
+ ```grain
28
+ hash : a -> Number
29
+ ```
30
+
31
+ A generic hash function that produces an integer from any value. If `a == b` then `Hash.hash(a) == Hash.hash(b)`.
32
+
33
+ Parameters:
34
+
35
+ |param|type|description|
36
+ |-----|----|-----------|
37
+ |`anything`|`a`|The value to hash|
38
+
39
+ Returns:
40
+
41
+ |type|description|
42
+ |----|-----------|
43
+ |`Number`|A hash for the given value|
44
+