@grain/stdlib 0.6.6 → 0.7.1

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 (137) hide show
  1. package/CHANGELOG.md +67 -0
  2. package/LICENSE +1 -1
  3. package/README.md +2 -2
  4. package/array.gr +55 -7
  5. package/array.md +606 -560
  6. package/bigint.md +228 -228
  7. package/buffer.gr +85 -53
  8. package/buffer.md +442 -319
  9. package/bytes.gr +112 -35
  10. package/bytes.md +299 -219
  11. package/char.gr +201 -99
  12. package/char.md +447 -120
  13. package/exception.gr +11 -11
  14. package/exception.md +29 -4
  15. package/float32.gr +327 -3
  16. package/float32.md +698 -111
  17. package/float64.gr +320 -3
  18. package/float64.md +698 -111
  19. package/fs.gr +1082 -0
  20. package/fs.md +630 -0
  21. package/hash.gr +142 -88
  22. package/hash.md +105 -17
  23. package/int16.md +178 -178
  24. package/int32.gr +26 -5
  25. package/int32.md +266 -231
  26. package/int64.gr +27 -2
  27. package/int64.md +266 -231
  28. package/int8.md +178 -178
  29. package/json.gr +366 -51
  30. package/json.md +431 -15
  31. package/list.gr +328 -31
  32. package/list.md +759 -336
  33. package/map.gr +20 -12
  34. package/map.md +266 -260
  35. package/marshal.gr +41 -40
  36. package/marshal.md +14 -14
  37. package/number.gr +278 -35
  38. package/number.md +688 -269
  39. package/option.md +162 -162
  40. package/package.json +5 -3
  41. package/path.gr +48 -0
  42. package/path.md +180 -89
  43. package/pervasives.gr +2 -2
  44. package/pervasives.md +275 -275
  45. package/priorityqueue.gr +7 -7
  46. package/priorityqueue.md +131 -131
  47. package/queue.gr +183 -29
  48. package/queue.md +404 -148
  49. package/random.md +43 -43
  50. package/range.gr +4 -4
  51. package/range.md +42 -42
  52. package/rational.md +123 -123
  53. package/regex.gr +52 -51
  54. package/regex.md +102 -102
  55. package/result.md +118 -118
  56. package/runtime/atof/common.md +39 -39
  57. package/runtime/atof/decimal.gr +6 -6
  58. package/runtime/atof/decimal.md +14 -14
  59. package/runtime/atof/lemire.gr +5 -5
  60. package/runtime/atof/lemire.md +1 -1
  61. package/runtime/atof/parse.gr +16 -16
  62. package/runtime/atof/parse.md +2 -2
  63. package/runtime/atof/slow.md +1 -1
  64. package/runtime/atof/table.md +2 -2
  65. package/runtime/atoi/parse.gr +3 -3
  66. package/runtime/atoi/parse.md +1 -1
  67. package/runtime/bigint.gr +15 -47
  68. package/runtime/bigint.md +54 -60
  69. package/runtime/compare.gr +2 -2
  70. package/runtime/compare.md +8 -8
  71. package/runtime/dataStructures.md +211 -211
  72. package/runtime/debugPrint.gr +4 -1
  73. package/runtime/debugPrint.md +9 -9
  74. package/runtime/equal.gr +99 -77
  75. package/runtime/equal.md +8 -8
  76. package/runtime/exception.gr +62 -82
  77. package/runtime/exception.md +62 -11
  78. package/runtime/gc.gr +39 -45
  79. package/runtime/gc.md +4 -4
  80. package/runtime/malloc.gr +7 -7
  81. package/runtime/malloc.md +13 -13
  82. package/runtime/math/kernel/cos.gr +70 -0
  83. package/runtime/math/kernel/cos.md +14 -0
  84. package/runtime/math/kernel/sin.gr +65 -0
  85. package/runtime/math/kernel/sin.md +14 -0
  86. package/runtime/math/kernel/tan.gr +136 -0
  87. package/runtime/math/kernel/tan.md +14 -0
  88. package/runtime/math/rempio2.gr +244 -0
  89. package/runtime/math/rempio2.md +14 -0
  90. package/runtime/math/trig.gr +130 -0
  91. package/runtime/math/trig.md +28 -0
  92. package/runtime/math/umuldi.gr +26 -0
  93. package/runtime/math/umuldi.md +14 -0
  94. package/runtime/numberUtils.gr +29 -29
  95. package/runtime/numberUtils.md +12 -12
  96. package/runtime/numbers.gr +373 -381
  97. package/runtime/numbers.md +348 -342
  98. package/runtime/string.gr +37 -105
  99. package/runtime/string.md +20 -26
  100. package/runtime/unsafe/constants.md +24 -24
  101. package/runtime/unsafe/conv.md +19 -19
  102. package/runtime/unsafe/memory.gr +24 -20
  103. package/runtime/unsafe/memory.md +27 -7
  104. package/runtime/unsafe/offsets.gr +36 -0
  105. package/runtime/unsafe/offsets.md +88 -0
  106. package/runtime/unsafe/panic.gr +28 -0
  107. package/runtime/unsafe/panic.md +14 -0
  108. package/runtime/unsafe/tags.md +32 -32
  109. package/runtime/unsafe/wasmf32.md +28 -28
  110. package/runtime/unsafe/wasmf64.md +28 -28
  111. package/runtime/unsafe/wasmi32.md +47 -47
  112. package/runtime/unsafe/wasmi64.md +50 -50
  113. package/runtime/utf8.gr +189 -0
  114. package/runtime/utf8.md +117 -0
  115. package/runtime/wasi.gr +4 -2
  116. package/runtime/wasi.md +147 -147
  117. package/set.gr +18 -11
  118. package/set.md +253 -247
  119. package/stack.gr +171 -2
  120. package/stack.md +371 -89
  121. package/string.gr +352 -557
  122. package/string.md +298 -255
  123. package/uint16.md +170 -170
  124. package/uint32.gr +25 -4
  125. package/uint32.md +249 -214
  126. package/uint64.gr +25 -5
  127. package/uint64.md +249 -214
  128. package/uint8.md +170 -170
  129. package/uri.gr +57 -53
  130. package/uri.md +88 -89
  131. package/wasi/file.gr +67 -59
  132. package/wasi/file.md +308 -308
  133. package/wasi/process.md +26 -26
  134. package/wasi/random.md +12 -12
  135. package/wasi/time.md +16 -16
  136. package/runtime/utils/printing.gr +0 -60
  137. package/runtime/utils/printing.md +0 -26
package/CHANGELOG.md CHANGED
@@ -1,5 +1,72 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.7.1](https://github.com/grain-lang/grain/compare/stdlib-v0.7.0...stdlib-v0.7.1) (2025-07-01)
4
+
5
+
6
+ ### Features
7
+
8
+ * **graindoc:** Pretty markdown tables ([#2292](https://github.com/grain-lang/grain/issues/2292)) ([1992734](https://github.com/grain-lang/grain/commit/1992734e4e3126b8b9da37bad7510235640004ad))
9
+ * **stdlib:** Add `Buffer.getChar` ([#2262](https://github.com/grain-lang/grain/issues/2262)) ([95d03cb](https://github.com/grain-lang/grain/commit/95d03cb97e67c05d0e7464a847cb8b0724bb28c8))
10
+ * **stdlib:** Deprecate `Buffer.set` operations in favour of `Bytes.set` ([#2303](https://github.com/grain-lang/grain/issues/2303)) ([684df98](https://github.com/grain-lang/grain/commit/684df98c8329f6ddd7fab1294e97728a4a44887f))
11
+ * **stdlib:** Expose equality operators through `Number` module ([#2296](https://github.com/grain-lang/grain/issues/2296)) ([5965dff](https://github.com/grain-lang/grain/commit/5965dfff2e43f2f84651c32fe3ef2060613f245d))
12
+
13
+ ## [0.7.0](https://github.com/grain-lang/grain/compare/stdlib-v0.6.6...stdlib-v0.7.0) (2025-04-26)
14
+
15
+
16
+ ### ⚠ BREAKING CHANGES
17
+
18
+ * **cli:** Require node `>=22.13` ([#2256](https://github.com/grain-lang/grain/issues/2256))
19
+ * **compiler:** Disallow whitespace for negative literals + allow infix operators on new line ([#2136](https://github.com/grain-lang/grain/issues/2136))
20
+ * **compiler:** Custom Grain object files ([#2104](https://github.com/grain-lang/grain/issues/2104))
21
+ * **stdlib:** Seeded hashing ([#2170](https://github.com/grain-lang/grain/issues/2170))
22
+ * **stdlib:** Add an `Ascii` submodule to `Char` and move `isAscii`, `toUppercase`, `toLowercase` ([#2178](https://github.com/grain-lang/grain/issues/2178))
23
+
24
+ ### Features
25
+
26
+ * Add `getAll`, `setAll`, `removeAll` to `List.Associative` ([#2224](https://github.com/grain-lang/grain/issues/2224)) ([9c49a23](https://github.com/grain-lang/grain/commit/9c49a23666f59c90499491ab2501214a33439fa1))
27
+ * **compiler:** Custom Grain object files ([#2104](https://github.com/grain-lang/grain/issues/2104)) ([4b56c53](https://github.com/grain-lang/grain/commit/4b56c535064e5ad8f68ca19af071d5e525a1a9aa))
28
+ * **compiler:** Disallow whitespace for negative literals + allow infix operators on new line ([#2136](https://github.com/grain-lang/grain/issues/2136)) ([3186155](https://github.com/grain-lang/grain/commit/318615583c1a1dbea3a7ea6c1c616bcac4df2c0a))
29
+ * **runtime:** Optimize `==` for lists ([#2247](https://github.com/grain-lang/grain/issues/2247)) ([1cba005](https://github.com/grain-lang/grain/commit/1cba005db44163e2d197c4af14f1c3d8997ebbf8))
30
+ * **runtime:** Prevent stack overflow while collecting large lists ([#2248](https://github.com/grain-lang/grain/issues/2248)) ([097ae7d](https://github.com/grain-lang/grain/commit/097ae7d403aaabe20fba373564562274b9ff38d3))
31
+ * **runtime:** Simplify `copy` and `fill` polyfills in `Memory` module ([#2148](https://github.com/grain-lang/grain/issues/2148)) ([1b3a9f0](https://github.com/grain-lang/grain/commit/1b3a9f07bd9749a4f67f12d1b58949476c2d6127))
32
+ * **stdlib:** `List.Associative` Submodule ([#2202](https://github.com/grain-lang/grain/issues/2202)) ([fbb08bf](https://github.com/grain-lang/grain/commit/fbb08bf28683b05fb77a57af6a787907da070ef7))
33
+ * **stdlib:** Add `**` to `Float64` and `Float32` ([#2163](https://github.com/grain-lang/grain/issues/2163)) ([7542d92](https://github.com/grain-lang/grain/commit/7542d92590fb87e168225c5b5019528a32ffef64))
34
+ * **stdlib:** Add `Array.tryInit` ([#2209](https://github.com/grain-lang/grain/issues/2209)) ([218ff78](https://github.com/grain-lang/grain/commit/218ff780c824a12bbfa2a78c12d84a66e65a4931))
35
+ * **stdlib:** Add `Bytes.setChar` and `Bytes.getChar` ([#2215](https://github.com/grain-lang/grain/issues/2215)) ([4919ba3](https://github.com/grain-lang/grain/commit/4919ba3fbf584eecd2c9db40ee74ac0c70bf92bd))
36
+ * **stdlib:** Add `Char.encodedLength` ([#2238](https://github.com/grain-lang/grain/issues/2238)) ([c549fac](https://github.com/grain-lang/grain/commit/c549facdd1cc8efb2d20e7efeb512e19e4a9fbab))
37
+ * **stdlib:** Add `copySign`, `sqrt`, `min`, `max`, `round`, `trunc`, `floor`, `ceil` to `Float32` ([#2161](https://github.com/grain-lang/grain/issues/2161)) ([f5a3dd3](https://github.com/grain-lang/grain/commit/f5a3dd363bb13c478e36049a3edac33576c90f4f))
38
+ * **stdlib:** Add `copySign`, `sqrt`, `min`, `max`, `round`, `trunc`, `floor`, `ceil` to `Float64` ([#2162](https://github.com/grain-lang/grain/issues/2162)) ([e99dcba](https://github.com/grain-lang/grain/commit/e99dcbae2ecee346587c6d5d6d77dfd4b358521b))
39
+ * **stdlib:** Add `Exception.toString` ([#2143](https://github.com/grain-lang/grain/issues/2143)) ([0894dc5](https://github.com/grain-lang/grain/commit/0894dc551c4aad678e39e9de29919bb17a0ea23e))
40
+ * **stdlib:** Add `isFinite`, `isClose`, `sin`, `cos`, `tan` to Float32 ([#2168](https://github.com/grain-lang/grain/issues/2168)) ([bdb4641](https://github.com/grain-lang/grain/commit/bdb46410f1967e1d063c4a1edad27fa19381f9de))
41
+ * **stdlib:** Add `isFinite`, `isClose`, `sin`, `cos`, `tan` to Float64 ([#2166](https://github.com/grain-lang/grain/issues/2166)) ([fa728d2](https://github.com/grain-lang/grain/commit/fa728d24437593f1eb60d65131d990ece27a5f30))
42
+ * **stdlib:** Add `List.filterMap`, `List.filterMapi`, `List.findMap` ([#2201](https://github.com/grain-lang/grain/issues/2201)) ([551f5ad](https://github.com/grain-lang/grain/commit/551f5adf8f36e601657fbdda7e3c546acd71f916))
43
+ * **stdlib:** add `Path.removeExtension` ([#2226](https://github.com/grain-lang/grain/issues/2226)) ([7b7acfd](https://github.com/grain-lang/grain/commit/7b7acfd5b7e12d2d490b9f3ff3b28911ef9240e7))
44
+ * **stdlib:** Add `Path.updateExtension` ([#2282](https://github.com/grain-lang/grain/issues/2282)) ([6d795d0](https://github.com/grain-lang/grain/commit/6d795d059fcc6107719e4149ee4697a10a5941a8))
45
+ * **stdlib:** Add `String.repeat` to `String` module ([#2140](https://github.com/grain-lang/grain/issues/2140)) ([6c33d08](https://github.com/grain-lang/grain/commit/6c33d08ddabdc57e27ddc9b5595d5ae1fd9bf37d))
46
+ * **stdlib:** Add `toList`, `fromList`, `toArray`, `fromArray` to Stack ([#2198](https://github.com/grain-lang/grain/issues/2198)) ([b815bcd](https://github.com/grain-lang/grain/commit/b815bcd17fe083846b1b8aac94fd0ceffca544cc))
47
+ * **stdlib:** Add an `Ascii` submodule to `Char` and move `isAscii`, `toUppercase`, `toLowercase` ([#2178](https://github.com/grain-lang/grain/issues/2178)) ([328cf01](https://github.com/grain-lang/grain/commit/328cf0168b07ac75b8a01e7e5cf4e7675b492acc))
48
+ * **stdlib:** Add user-friendly file system module ([#1966](https://github.com/grain-lang/grain/issues/1966)) ([c4f638e](https://github.com/grain-lang/grain/commit/c4f638eaf0f7049dd3cfec96791f7d7ba22531f6))
49
+ * **stdlib:** Implement `reinterpret` between float and int values ([#2257](https://github.com/grain-lang/grain/issues/2257)) ([e3a4eac](https://github.com/grain-lang/grain/commit/e3a4eacb9a57caa6f45890d04233c7aa8abdfb08))
50
+ * **stdlib:** Json value access utils ([#2150](https://github.com/grain-lang/grain/issues/2150)) ([72cc978](https://github.com/grain-lang/grain/commit/72cc9784783731440cbb73196fc45c0fd77745f2))
51
+ * **stdlib:** Optimize `List.init` to support large lists ([#2249](https://github.com/grain-lang/grain/issues/2249)) ([3de64ba](https://github.com/grain-lang/grain/commit/3de64ba88cb3e18fedd60175ce2b5e03e3d398ce))
52
+ * **stdlib:** Reimplement `Number.gamma` and `Number.factorial` ([#2182](https://github.com/grain-lang/grain/issues/2182)) ([1e5f921](https://github.com/grain-lang/grain/commit/1e5f921a436de8e0705073083f2b9c8403a1e928))
53
+ * **stdlib:** Reimplement `Number.sin`, `Number.cos`, `Number.tan` ([#2158](https://github.com/grain-lang/grain/issues/2158)) ([f97c011](https://github.com/grain-lang/grain/commit/f97c01168eebff400e5d7bb9020536d784fa1da2))
54
+ * **stdlib:** Seeded hashing ([#2170](https://github.com/grain-lang/grain/issues/2170)) ([ce872ab](https://github.com/grain-lang/grain/commit/ce872ab718577ebd4d46857a2f3f7bb08c58efe4))
55
+
56
+
57
+ ### Bug Fixes
58
+
59
+ * **compiler:** Handle non-exhaustive record patterns ([#2274](https://github.com/grain-lang/grain/issues/2274)) ([0431e74](https://github.com/grain-lang/grain/commit/0431e74b1e0a02ec22fd46871aa5f5f8d8dd1649))
60
+ * **runtime:** Correct `Bi.toFloat64` rounding ([#2160](https://github.com/grain-lang/grain/issues/2160)) ([6755782](https://github.com/grain-lang/grain/commit/6755782948b9bd1bcf7cbf6ad7ad0a3d894bd1f6))
61
+ * **stdlib:** Correct sign bit in `_rempio` when computing trig reduction ([#2181](https://github.com/grain-lang/grain/issues/2181)) ([6a78502](https://github.com/grain-lang/grain/commit/6a78502dfb9e52d0466cab090c958c0ffd3fb83f))
62
+ * **stdlib:** Optimize number modulo ([#2144](https://github.com/grain-lang/grain/issues/2144)) ([960fadd](https://github.com/grain-lang/grain/commit/960faddf4ed80a6d071c727c9640e0d7fb5b63e1))
63
+ * **stdlib:** Properly print `Range` values ([#2184](https://github.com/grain-lang/grain/issues/2184)) ([11b1fc2](https://github.com/grain-lang/grain/commit/11b1fc2ff089f08e9c4d350c1343ac04bdc68caa))
64
+
65
+
66
+ ### Miscellaneous Chores
67
+
68
+ * **cli:** Require node `>=22.13` ([#2256](https://github.com/grain-lang/grain/issues/2256)) ([220bfc3](https://github.com/grain-lang/grain/commit/220bfc3ec527a27fdaf588551f2ca87b3326f5db))
69
+
3
70
  ## [0.6.6](https://github.com/grain-lang/grain/compare/stdlib-v0.6.5...stdlib-v0.6.6) (2024-08-12)
4
71
 
5
72
 
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2017-2024 Oscar Spencer <oscar@grain-lang.org> and Philip Blair <philip@grain-lang.org>
3
+ Copyright (c) 2017-2025 Oscar Spencer <oscar@grain-lang.org> and Philip Blair <philip@grain-lang.org>
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -41,13 +41,13 @@ If you want to contribute to the `stdlib`, please consider the guidelines [here]
41
41
  To regenerate the `stdlib`` documentation you can run:
42
42
 
43
43
  ```sh
44
- grain doc stdlib -o stdlib --current-version=$(grain -v)
44
+ npm run stdlib doc
45
45
  ```
46
46
 
47
47
  To format the `stdlib` you can run:
48
48
 
49
49
  ```sh
50
- grain format ./stdlib/ -o ./stdlib/
50
+ npm run stdlib format
51
51
  ```
52
52
 
53
53
  To run the `stdlib` tests you can run:
package/array.gr CHANGED
@@ -21,9 +21,8 @@ use DataStructures.{ allocateArray, tagSimpleNumber }
21
21
  from "runtime/exception" include Exception
22
22
  from "runtime/numbers" include Numbers
23
23
  use Numbers.{ coerceNumberToWasmI32 }
24
-
25
- @unsafe
26
- let _ARRAY_START_OFFSET = 8n
24
+ from "runtime/unsafe/offsets" include Offsets
25
+ use Offsets.{ _ARRAY_DATA_OFFSET }
27
26
 
28
27
  @unsafe
29
28
  let checkLength = length => {
@@ -88,7 +87,7 @@ provide let make = (length: Number, item: a) => {
88
87
  WasmI32.store(
89
88
  array + i,
90
89
  Memory.incRef(WasmI32.fromGrain(item)),
91
- _ARRAY_START_OFFSET
90
+ _ARRAY_DATA_OFFSET
92
91
  )
93
92
  }
94
93
  ignore(item)
@@ -121,15 +120,64 @@ provide let init = (length: Number, fn: Number => a) => {
121
120
  let mut index = 0n
122
121
  for (let mut i = 0n; i < byteLength; i += 4n) {
123
122
  WasmI32.store(
124
- array + i,
123
+ array,
125
124
  Memory.incRef(WasmI32.fromGrain(fn(tagSimpleNumber(index)))),
126
- _ARRAY_START_OFFSET
125
+ _ARRAY_DATA_OFFSET + i
127
126
  )
128
127
  index += 1n
129
128
  }
130
129
  WasmI32.toGrain(array): Array<a>
131
130
  }
132
131
 
132
+ /**
133
+ * Creates a new array where each element is initialized with the `Ok`
134
+ * result value of an initializer function. The initializer is called with the
135
+ * index of each element, and returns the new array if all calls to the
136
+ * initializer succeed or the first error otherwise.
137
+ *
138
+ * @param length: The length of the new array
139
+ * @param fn: The initializer function to call with each index, where the `Ok` value returned will be used to initialize each element
140
+ * @returns `Ok(array)` if all elements were successfully initialized or `Err(error)` if the initializer function returned an error
141
+ *
142
+ * @throws InvalidArgument(String): When `length` is not an integer
143
+ * @throws InvalidArgument(String): When `length` is negative
144
+ *
145
+ * @example Array.tryInit(5, n => Ok(n + 3)) == [> 3, 4, 5, 6, 7]
146
+ * @example Array.tryInit(5, n => if (n == 1) Err("stop") else Ok(n)) == Err("stop")
147
+ *
148
+ * @since v0.7.0
149
+ */
150
+ @unsafe
151
+ provide let tryInit = (length: Number, fn: Number => Result<a, b>) => {
152
+ use WasmI32.{ (+), (*), (<) }
153
+ checkLength(length)
154
+ let length = coerceNumberToWasmI32(length)
155
+ let byteLength = length * 4n
156
+ let array = allocateArray(length)
157
+ let mut index = 0n
158
+ for (let mut i = 0n; i < byteLength; i += 4n) {
159
+ let value = fn(tagSimpleNumber(index))
160
+ match (value) {
161
+ Ok(value) => {
162
+ WasmI32.store(
163
+ array,
164
+ Memory.incRef(WasmI32.fromGrain(value)),
165
+ _ARRAY_DATA_OFFSET + i
166
+ )
167
+ index += 1n
168
+ },
169
+ Err(e) => {
170
+ for (let mut j = 0n; j < i; j += 4n) {
171
+ ignore(Memory.decRef(WasmI32.load(array, _ARRAY_DATA_OFFSET + j)))
172
+ }
173
+ Memory.free(array)
174
+ return Err(e)
175
+ },
176
+ }
177
+ }
178
+ return Ok(WasmI32.toGrain(array): Array<a>)
179
+ }
180
+
133
181
  /**
134
182
  * An alias for normal syntactic array access, i.e. `array[n]`.
135
183
  *
@@ -1942,7 +1990,7 @@ provide module Immutable {
1942
1990
  *
1943
1991
  * @param fn: The function to call on each element, where the returned value indicates if the element satisfies the condition
1944
1992
  * @param array: The array to check
1945
- * @returns `true` if all elements satify the condition or `false` otherwise
1993
+ * @returns `true` if all elements satisfy the condition or `false` otherwise
1946
1994
  *
1947
1995
  * @example
1948
1996
  * use Array.{ module Immutable }