@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.
- package/CHANGELOG.md +201 -0
- package/LICENSE +1 -1
- package/README.md +25 -2
- package/array.gr +1512 -199
- package/array.md +2032 -94
- package/bigint.gr +239 -140
- package/bigint.md +450 -106
- package/buffer.gr +595 -102
- package/buffer.md +903 -145
- package/bytes.gr +401 -110
- package/bytes.md +551 -63
- package/char.gr +228 -49
- package/char.md +373 -7
- package/exception.gr +26 -12
- package/exception.md +29 -5
- package/float32.gr +130 -109
- package/float32.md +185 -57
- package/float64.gr +112 -99
- package/float64.md +185 -57
- package/hash.gr +62 -40
- package/hash.md +27 -3
- package/int16.gr +430 -0
- package/int16.md +618 -0
- package/int32.gr +200 -269
- package/int32.md +254 -289
- package/int64.gr +142 -225
- package/int64.md +254 -289
- package/int8.gr +511 -0
- package/int8.md +786 -0
- package/json.gr +2071 -0
- package/json.md +646 -0
- package/list.gr +120 -68
- package/list.md +125 -80
- package/map.gr +560 -57
- package/map.md +672 -56
- package/marshal.gr +239 -227
- package/marshal.md +36 -4
- package/number.gr +626 -676
- package/number.md +738 -153
- package/option.gr +33 -35
- package/option.md +58 -42
- package/package.json +2 -2
- package/path.gr +148 -187
- package/path.md +47 -96
- package/pervasives.gr +75 -416
- package/pervasives.md +85 -180
- package/priorityqueue.gr +433 -74
- package/priorityqueue.md +422 -54
- package/queue.gr +362 -80
- package/queue.md +433 -38
- package/random.gr +67 -75
- package/random.md +68 -40
- package/range.gr +135 -63
- package/range.md +198 -43
- package/rational.gr +284 -0
- package/rational.md +545 -0
- package/regex.gr +933 -1066
- package/regex.md +59 -60
- package/result.gr +23 -25
- package/result.md +54 -39
- package/runtime/atof/common.gr +78 -82
- package/runtime/atof/common.md +22 -10
- package/runtime/atof/decimal.gr +102 -127
- package/runtime/atof/decimal.md +28 -7
- package/runtime/atof/lemire.gr +56 -71
- package/runtime/atof/lemire.md +9 -1
- package/runtime/atof/parse.gr +83 -110
- package/runtime/atof/parse.md +12 -2
- package/runtime/atof/slow.gr +28 -35
- package/runtime/atof/slow.md +9 -1
- package/runtime/atof/table.gr +19 -18
- package/runtime/atof/table.md +10 -2
- package/runtime/atoi/parse.gr +153 -136
- package/runtime/atoi/parse.md +50 -1
- package/runtime/bigint.gr +410 -517
- package/runtime/bigint.md +71 -57
- package/runtime/compare.gr +176 -85
- package/runtime/compare.md +31 -1
- package/runtime/dataStructures.gr +144 -32
- package/runtime/dataStructures.md +267 -31
- package/runtime/debugPrint.gr +34 -15
- package/runtime/debugPrint.md +37 -5
- package/runtime/equal.gr +53 -52
- package/runtime/equal.md +30 -1
- package/runtime/exception.gr +38 -47
- package/runtime/exception.md +10 -8
- package/runtime/gc.gr +23 -152
- package/runtime/gc.md +13 -17
- package/runtime/malloc.gr +31 -31
- package/runtime/malloc.md +11 -3
- package/runtime/numberUtils.gr +193 -174
- package/runtime/numberUtils.md +29 -9
- package/runtime/numbers.gr +1695 -1021
- package/runtime/numbers.md +1098 -134
- package/runtime/string.gr +543 -245
- package/runtime/string.md +76 -6
- package/runtime/unsafe/constants.gr +30 -13
- package/runtime/unsafe/constants.md +80 -0
- package/runtime/unsafe/conv.gr +55 -28
- package/runtime/unsafe/conv.md +41 -9
- package/runtime/unsafe/memory.gr +10 -30
- package/runtime/unsafe/memory.md +15 -19
- package/runtime/unsafe/tags.gr +37 -21
- package/runtime/unsafe/tags.md +88 -8
- package/runtime/unsafe/wasmf32.gr +30 -36
- package/runtime/unsafe/wasmf32.md +64 -56
- package/runtime/unsafe/wasmf64.gr +30 -36
- package/runtime/unsafe/wasmf64.md +64 -56
- package/runtime/unsafe/wasmi32.gr +49 -66
- package/runtime/unsafe/wasmi32.md +102 -94
- package/runtime/unsafe/wasmi64.gr +52 -79
- package/runtime/unsafe/wasmi64.md +108 -100
- package/runtime/utils/printing.gr +13 -15
- package/runtime/utils/printing.md +11 -3
- package/runtime/wasi.gr +294 -295
- package/runtime/wasi.md +62 -42
- package/set.gr +574 -64
- package/set.md +634 -54
- package/stack.gr +181 -64
- package/stack.md +271 -42
- package/string.gr +453 -533
- package/string.md +241 -151
- package/uint16.gr +369 -0
- package/uint16.md +585 -0
- package/uint32.gr +470 -0
- package/uint32.md +737 -0
- package/uint64.gr +471 -0
- package/uint64.md +737 -0
- package/uint8.gr +369 -0
- package/uint8.md +585 -0
- package/uri.gr +1093 -0
- package/uri.md +477 -0
- package/{sys → wasi}/file.gr +914 -500
- package/{sys → wasi}/file.md +454 -50
- package/wasi/process.gr +292 -0
- package/{sys → wasi}/process.md +164 -6
- package/wasi/random.gr +77 -0
- package/wasi/random.md +80 -0
- package/{sys → wasi}/time.gr +15 -22
- package/{sys → wasi}/time.md +5 -5
- package/immutablearray.gr +0 -929
- package/immutablearray.md +0 -1038
- package/immutablemap.gr +0 -493
- package/immutablemap.md +0 -479
- package/immutablepriorityqueue.gr +0 -360
- package/immutablepriorityqueue.md +0 -291
- package/immutableset.gr +0 -498
- package/immutableset.md +0 -449
- package/runtime/debug.gr +0 -2
- package/runtime/debug.md +0 -6
- package/runtime/unsafe/errors.gr +0 -36
- package/runtime/unsafe/errors.md +0 -204
- package/sys/process.gr +0 -254
- package/sys/random.gr +0 -79
- package/sys/random.md +0 -66
package/stack.gr
CHANGED
|
@@ -1,64 +1,61 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
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
|
-
|
|
14
|
+
from "list" include List
|
|
15
|
+
from "array" include Array
|
|
10
16
|
|
|
11
17
|
/**
|
|
12
|
-
*
|
|
18
|
+
* A mutable LIFO (last-in-first-out) data structure.
|
|
13
19
|
*/
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
-
*
|
|
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
|
-
* @
|
|
31
|
+
* @param size: The initial storage size of the stack
|
|
32
|
+
* @returns An empty stack
|
|
33
|
+
*
|
|
34
|
+
* @since v0.6.0
|
|
30
35
|
*/
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
empty
|
|
36
|
+
provide let make = (size=16) => {
|
|
37
|
+
{ size: 0, array: Array.make(size, None) }
|
|
34
38
|
}
|
|
35
39
|
|
|
36
40
|
/**
|
|
37
|
-
*
|
|
41
|
+
* Checks if the given stack contains no items.
|
|
38
42
|
*
|
|
39
|
-
* @
|
|
43
|
+
* @param stack: The stack to check
|
|
44
|
+
* @returns `true` if the stack has no items or `false` otherwise
|
|
40
45
|
*
|
|
41
|
-
* @since v0.
|
|
42
|
-
* @deprecated This will be removed in the v0.6.0 release of Grain.
|
|
46
|
+
* @since v0.6.0
|
|
43
47
|
*/
|
|
44
|
-
|
|
45
|
-
{ data: [], }
|
|
46
|
-
}
|
|
48
|
+
provide let isEmpty = stack => stack.size == 0
|
|
47
49
|
|
|
48
50
|
/**
|
|
49
|
-
*
|
|
51
|
+
* Computes the size of the input stack.
|
|
50
52
|
*
|
|
51
|
-
* @param stack: The stack to
|
|
52
|
-
* @returns
|
|
53
|
+
* @param stack: The stack to inspect
|
|
54
|
+
* @returns The count of the items in the stack
|
|
53
55
|
*
|
|
54
|
-
* @since v0.
|
|
56
|
+
* @since v0.6.0
|
|
55
57
|
*/
|
|
56
|
-
|
|
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.
|
|
70
|
-
* @history v0.3.1: Rename from `head` to `peek`
|
|
66
|
+
* @since v0.6.0
|
|
71
67
|
*/
|
|
72
|
-
|
|
73
|
-
|
|
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.
|
|
78
|
+
* @since v0.6.0
|
|
87
79
|
*/
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
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
|
|
99
|
+
* @returns The element removed from the stack
|
|
100
100
|
*
|
|
101
|
-
* @since v0.
|
|
101
|
+
* @since v0.6.0
|
|
102
102
|
*/
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
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
|
-
*
|
|
113
|
+
* Clears the stack by removing all of its elements
|
|
112
114
|
*
|
|
113
|
-
* @param stack: The stack to
|
|
114
|
-
* @returns The count of the items in the stack
|
|
115
|
+
* @param stack: The stack to clear
|
|
115
116
|
*
|
|
116
|
-
* @since v0.
|
|
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
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
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
|
}
|