@grain/stdlib 0.5.13 → 0.6.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 (155) hide show
  1. package/CHANGELOG.md +201 -0
  2. package/LICENSE +1 -1
  3. package/README.md +25 -2
  4. package/array.gr +1512 -199
  5. package/array.md +2032 -94
  6. package/bigint.gr +239 -140
  7. package/bigint.md +450 -106
  8. package/buffer.gr +595 -102
  9. package/buffer.md +903 -145
  10. package/bytes.gr +401 -110
  11. package/bytes.md +551 -63
  12. package/char.gr +228 -49
  13. package/char.md +373 -7
  14. package/exception.gr +26 -12
  15. package/exception.md +29 -5
  16. package/float32.gr +130 -109
  17. package/float32.md +185 -57
  18. package/float64.gr +112 -99
  19. package/float64.md +185 -57
  20. package/hash.gr +62 -40
  21. package/hash.md +27 -3
  22. package/int16.gr +430 -0
  23. package/int16.md +618 -0
  24. package/int32.gr +200 -269
  25. package/int32.md +254 -289
  26. package/int64.gr +142 -225
  27. package/int64.md +254 -289
  28. package/int8.gr +511 -0
  29. package/int8.md +786 -0
  30. package/json.gr +2071 -0
  31. package/json.md +646 -0
  32. package/list.gr +120 -68
  33. package/list.md +125 -80
  34. package/map.gr +560 -57
  35. package/map.md +672 -56
  36. package/marshal.gr +239 -227
  37. package/marshal.md +36 -4
  38. package/number.gr +626 -676
  39. package/number.md +738 -153
  40. package/option.gr +33 -35
  41. package/option.md +58 -42
  42. package/package.json +2 -2
  43. package/path.gr +148 -187
  44. package/path.md +47 -96
  45. package/pervasives.gr +75 -416
  46. package/pervasives.md +85 -180
  47. package/priorityqueue.gr +433 -74
  48. package/priorityqueue.md +422 -54
  49. package/queue.gr +362 -80
  50. package/queue.md +433 -38
  51. package/random.gr +67 -75
  52. package/random.md +68 -40
  53. package/range.gr +135 -63
  54. package/range.md +198 -43
  55. package/rational.gr +284 -0
  56. package/rational.md +545 -0
  57. package/regex.gr +933 -1066
  58. package/regex.md +59 -60
  59. package/result.gr +23 -25
  60. package/result.md +54 -39
  61. package/runtime/atof/common.gr +78 -82
  62. package/runtime/atof/common.md +22 -10
  63. package/runtime/atof/decimal.gr +102 -127
  64. package/runtime/atof/decimal.md +28 -7
  65. package/runtime/atof/lemire.gr +56 -71
  66. package/runtime/atof/lemire.md +9 -1
  67. package/runtime/atof/parse.gr +83 -110
  68. package/runtime/atof/parse.md +12 -2
  69. package/runtime/atof/slow.gr +28 -35
  70. package/runtime/atof/slow.md +9 -1
  71. package/runtime/atof/table.gr +19 -18
  72. package/runtime/atof/table.md +10 -2
  73. package/runtime/atoi/parse.gr +153 -136
  74. package/runtime/atoi/parse.md +50 -1
  75. package/runtime/bigint.gr +410 -517
  76. package/runtime/bigint.md +71 -57
  77. package/runtime/compare.gr +176 -85
  78. package/runtime/compare.md +31 -1
  79. package/runtime/dataStructures.gr +144 -32
  80. package/runtime/dataStructures.md +267 -31
  81. package/runtime/debugPrint.gr +34 -15
  82. package/runtime/debugPrint.md +37 -5
  83. package/runtime/equal.gr +53 -52
  84. package/runtime/equal.md +30 -1
  85. package/runtime/exception.gr +38 -47
  86. package/runtime/exception.md +10 -8
  87. package/runtime/gc.gr +23 -152
  88. package/runtime/gc.md +13 -17
  89. package/runtime/malloc.gr +31 -31
  90. package/runtime/malloc.md +11 -3
  91. package/runtime/numberUtils.gr +193 -174
  92. package/runtime/numberUtils.md +29 -9
  93. package/runtime/numbers.gr +1695 -1021
  94. package/runtime/numbers.md +1098 -134
  95. package/runtime/string.gr +543 -245
  96. package/runtime/string.md +76 -6
  97. package/runtime/unsafe/constants.gr +30 -13
  98. package/runtime/unsafe/constants.md +80 -0
  99. package/runtime/unsafe/conv.gr +55 -28
  100. package/runtime/unsafe/conv.md +41 -9
  101. package/runtime/unsafe/memory.gr +10 -30
  102. package/runtime/unsafe/memory.md +15 -19
  103. package/runtime/unsafe/tags.gr +37 -21
  104. package/runtime/unsafe/tags.md +88 -8
  105. package/runtime/unsafe/wasmf32.gr +30 -36
  106. package/runtime/unsafe/wasmf32.md +64 -56
  107. package/runtime/unsafe/wasmf64.gr +30 -36
  108. package/runtime/unsafe/wasmf64.md +64 -56
  109. package/runtime/unsafe/wasmi32.gr +49 -66
  110. package/runtime/unsafe/wasmi32.md +102 -94
  111. package/runtime/unsafe/wasmi64.gr +52 -79
  112. package/runtime/unsafe/wasmi64.md +108 -100
  113. package/runtime/utils/printing.gr +13 -15
  114. package/runtime/utils/printing.md +11 -3
  115. package/runtime/wasi.gr +294 -295
  116. package/runtime/wasi.md +62 -42
  117. package/set.gr +574 -64
  118. package/set.md +634 -54
  119. package/stack.gr +181 -64
  120. package/stack.md +271 -42
  121. package/string.gr +453 -533
  122. package/string.md +241 -151
  123. package/uint16.gr +369 -0
  124. package/uint16.md +585 -0
  125. package/uint32.gr +470 -0
  126. package/uint32.md +737 -0
  127. package/uint64.gr +471 -0
  128. package/uint64.md +737 -0
  129. package/uint8.gr +369 -0
  130. package/uint8.md +585 -0
  131. package/uri.gr +1093 -0
  132. package/uri.md +477 -0
  133. package/{sys → wasi}/file.gr +914 -500
  134. package/{sys → wasi}/file.md +454 -50
  135. package/wasi/process.gr +292 -0
  136. package/{sys → wasi}/process.md +164 -6
  137. package/wasi/random.gr +77 -0
  138. package/wasi/random.md +80 -0
  139. package/{sys → wasi}/time.gr +15 -22
  140. package/{sys → wasi}/time.md +5 -5
  141. package/immutablearray.gr +0 -929
  142. package/immutablearray.md +0 -1038
  143. package/immutablemap.gr +0 -493
  144. package/immutablemap.md +0 -479
  145. package/immutablepriorityqueue.gr +0 -360
  146. package/immutablepriorityqueue.md +0 -291
  147. package/immutableset.gr +0 -498
  148. package/immutableset.md +0 -449
  149. package/runtime/debug.gr +0 -2
  150. package/runtime/debug.md +0 -6
  151. package/runtime/unsafe/errors.gr +0 -36
  152. package/runtime/unsafe/errors.md +0 -204
  153. package/sys/process.gr +0 -254
  154. package/sys/random.gr +0 -79
  155. package/sys/random.md +0 -66
package/stack.gr CHANGED
@@ -1,64 +1,61 @@
1
1
  /**
2
- * @module Stack: An immutable stack implementation. A stack is a LIFO (last-in-first-out) data structure where new values are added, retrieved, and removed from the end.
3
- * @example import Stack from "stack"
2
+ * A stack is a LIFO (last-in-first-out) data structure where new
3
+ * values are added, retrieved, and removed from the end.
4
+ *
5
+ * The default implementation is mutable, but an immutable stack
6
+ * implementation is available in the `Immutable` submodule.
7
+ *
8
+ * @example from "stack" include Stack
4
9
  *
5
10
  * @since v0.3.0
6
- * @deprecated This module will be renamed to ImmutableStack in the v0.6.0 release of Grain.
7
11
  */
12
+ module Stack
8
13
 
9
- import List from "list"
14
+ from "list" include List
15
+ from "array" include Array
10
16
 
11
17
  /**
12
- * @section Types: Type declarations included in the Stack module.
18
+ * A mutable LIFO (last-in-first-out) data structure.
13
19
  */
14
-
15
- /**
16
- * Stacks are immutable data structures that store their data in a List.
17
- */
18
- record Stack<a> {
19
- data: List<a>,
20
+ abstract record Stack<a> {
21
+ mut size: Number,
22
+ mut array: Array<Option<a>>,
20
23
  }
21
24
 
22
25
  /**
23
- * @section Values: Functions and constants included in the Stack module.
24
- */
25
-
26
- /**
27
- * An empty stack.
26
+ * Creates a new stack with an initial storage of the given size. As values are
27
+ * added or removed, the internal storage may grow or shrink. Generally, you
28
+ * won’t need to care about the storage size of your map and can use the
29
+ * default size.
28
30
  *
29
- * @since v0.5.4
31
+ * @param size: The initial storage size of the stack
32
+ * @returns An empty stack
33
+ *
34
+ * @since v0.6.0
30
35
  */
31
- export let empty = {
32
- let empty = { data: [], }
33
- empty
36
+ provide let make = (size=16) => {
37
+ { size: 0, array: Array.make(size, None) }
34
38
  }
35
39
 
36
40
  /**
37
- * Creates a new stack.
41
+ * Checks if the given stack contains no items.
38
42
  *
39
- * @returns An empty stack
43
+ * @param stack: The stack to check
44
+ * @returns `true` if the stack has no items or `false` otherwise
40
45
  *
41
- * @since v0.3.0
42
- * @deprecated This will be removed in the v0.6.0 release of Grain.
46
+ * @since v0.6.0
43
47
  */
44
- export let make = () => {
45
- { data: [], }
46
- }
48
+ provide let isEmpty = stack => stack.size == 0
47
49
 
48
50
  /**
49
- * Checks if the given stack contains no items.
51
+ * Computes the size of the input stack.
50
52
  *
51
- * @param stack: The stack to check
52
- * @returns `true` if the stack has no items or `false` otherwise
53
+ * @param stack: The stack to inspect
54
+ * @returns The count of the items in the stack
53
55
  *
54
- * @since v0.3.0
56
+ * @since v0.6.0
55
57
  */
56
- export let isEmpty = stack => {
57
- match (stack) {
58
- { data: [] } => true,
59
- _ => false,
60
- }
61
- }
58
+ provide let size = stack => stack.size
62
59
 
63
60
  /**
64
61
  * Provides the value at the top of the stack, if it exists.
@@ -66,14 +63,10 @@ export let isEmpty = stack => {
66
63
  * @param stack: The stack to inspect
67
64
  * @returns `Some(value)` containing the value at the top of the stack or `None` otherwise.
68
65
  *
69
- * @since v0.3.0
70
- * @history v0.3.1: Rename from `head` to `peek`
66
+ * @since v0.6.0
71
67
  */
72
- export let peek = stack => {
73
- match (stack) {
74
- { data: [] } => None,
75
- { data } => List.head(data),
76
- }
68
+ provide let peek = stack => {
69
+ if (stack.size == 0) None else stack.array[stack.size - 1]
77
70
  }
78
71
 
79
72
  /**
@@ -81,43 +74,167 @@ export let peek = stack => {
81
74
  *
82
75
  * @param value: The item to be added
83
76
  * @param stack: The stack being updated
84
- * @returns A new stack with the item added to the end
85
77
  *
86
- * @since v0.3.0
78
+ * @since v0.6.0
87
79
  */
88
- export let push = (value, stack) => {
89
- match (stack) {
90
- { data: [] } => { data: [value], },
91
- { data } => { data: [value, ...data], },
80
+ provide let push = (value, stack) => {
81
+ let arrLen = Array.length(stack.array)
82
+ if (arrLen == 0) {
83
+ stack.array = Array.make(1, None)
84
+ } else if (stack.size == arrLen) {
85
+ let newArray = Array.make(stack.size * 2, None)
86
+ for (let mut i = 0; i < arrLen; i += 1) {
87
+ newArray[i] = stack.array[i]
88
+ }
89
+ stack.array = newArray
92
90
  }
91
+ stack.array[stack.size] = Some(value)
92
+ stack.size += 1
93
93
  }
94
94
 
95
95
  /**
96
96
  * Removes the item at the top of the stack.
97
97
  *
98
98
  * @param stack: The stack being updated
99
- * @returns A new stack with the last item removed
99
+ * @returns The element removed from the stack
100
100
  *
101
- * @since v0.3.0
101
+ * @since v0.6.0
102
102
  */
103
- export let pop = stack => {
104
- match (stack) {
105
- { data: [] } => stack,
106
- { data: [head, ...tail] } => { data: tail, },
103
+ provide let pop = stack => {
104
+ if (stack.size == 0) {
105
+ None
106
+ } else {
107
+ stack.size -= 1
108
+ stack.array[stack.size]
107
109
  }
108
110
  }
109
111
 
110
112
  /**
111
- * Computes the size of the input stack.
113
+ * Clears the stack by removing all of its elements
112
114
  *
113
- * @param stack: The stack to inspect
114
- * @returns The count of the items in the stack
115
+ * @param stack: The stack to clear
115
116
  *
116
- * @since v0.3.2
117
+ * @since v0.6.0
118
+ */
119
+ provide let clear = stack => {
120
+ stack.size = 0
121
+ stack.array = Array.make(Array.length(stack.array), None)
122
+ }
123
+
124
+ /**
125
+ * Produces a shallow copy of the input stack.
126
+ *
127
+ * @param stack: The stack to copy
128
+ * @returns A new stack containing the elements from the input
129
+ *
130
+ * @since v0.6.0
131
+ */
132
+ provide let copy = stack => {
133
+ let { size, array } = stack
134
+ { size, array: Array.copy(array) }
135
+ }
136
+
137
+ /**
138
+ * An immutable stack implementation.
117
139
  */
118
- export let size = stack => {
119
- match (stack) {
120
- { data: [] } => 0,
121
- { data } => List.length(data),
140
+ provide module Immutable {
141
+ /**
142
+ * ImmutableStacks are immutable data structures that store their data in a List.
143
+ */
144
+ abstract record ImmutableStack<a> {
145
+ data: List<a>,
146
+ }
147
+
148
+ /**
149
+ * An empty stack.
150
+ *
151
+ * @since v0.6.0
152
+ * @history v0.5.4: Originally a module root API
153
+ */
154
+ provide let empty = {
155
+ let empty = { data: [], }
156
+ empty
157
+ }
158
+
159
+ /**
160
+ * Checks if the given stack contains no items.
161
+ *
162
+ * @param stack: The stack to check
163
+ * @returns `true` if the stack has no items or `false` otherwise
164
+ *
165
+ * @since v0.6.0
166
+ * @history v0.3.0: Originally a module root API
167
+ */
168
+ provide let isEmpty = stack => {
169
+ match (stack) {
170
+ { data: [] } => true,
171
+ _ => false,
172
+ }
173
+ }
174
+
175
+ /**
176
+ * Provides the value at the top of the stack, if it exists.
177
+ *
178
+ * @param stack: The stack to inspect
179
+ * @returns `Some(value)` containing the value at the top of the stack or `None` otherwise.
180
+ *
181
+ * @since v0.6.0
182
+ * @history v0.3.0: Originally a module root API
183
+ * @history v0.3.1: Rename from `head` to `peek`
184
+ */
185
+ provide let peek = stack => {
186
+ match (stack) {
187
+ { data: [] } => None,
188
+ { data } => List.head(data),
189
+ }
190
+ }
191
+
192
+ /**
193
+ * Adds a new item to the top of the stack.
194
+ *
195
+ * @param value: The item to be added
196
+ * @param stack: The stack being updated
197
+ * @returns A new stack with the item added to the end
198
+ *
199
+ * @since v0.6.0
200
+ * @history v0.3.0: Originally a module root API
201
+ */
202
+ provide let push = (value, stack) => {
203
+ match (stack) {
204
+ { data: [] } => { data: [value], },
205
+ { data } => { data: [value, ...data], },
206
+ }
207
+ }
208
+
209
+ /**
210
+ * Removes the item at the top of the stack.
211
+ *
212
+ * @param stack: The stack being updated
213
+ * @returns A new stack with the last item removed
214
+ *
215
+ * @since v0.6.0
216
+ * @history v0.3.0: Originally a module root API
217
+ */
218
+ provide let pop = stack => {
219
+ match (stack) {
220
+ { data: [] } => stack,
221
+ { data: [head, ...tail] } => { data: tail, },
222
+ }
223
+ }
224
+
225
+ /**
226
+ * Computes the size of the input stack.
227
+ *
228
+ * @param stack: The stack to inspect
229
+ * @returns The count of the items in the stack
230
+ *
231
+ * @since v0.6.0
232
+ * @history v0.3.2: Originally a module root API
233
+ */
234
+ provide let size = stack => {
235
+ match (stack) {
236
+ { data: [] } => 0,
237
+ { data } => List.length(data),
238
+ }
122
239
  }
123
240
  }