@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.
- package/CHANGELOG.md +67 -0
- package/LICENSE +1 -1
- package/README.md +2 -2
- package/array.gr +55 -7
- package/array.md +606 -560
- package/bigint.md +228 -228
- package/buffer.gr +85 -53
- package/buffer.md +442 -319
- package/bytes.gr +112 -35
- package/bytes.md +299 -219
- package/char.gr +201 -99
- package/char.md +447 -120
- package/exception.gr +11 -11
- package/exception.md +29 -4
- package/float32.gr +327 -3
- package/float32.md +698 -111
- package/float64.gr +320 -3
- package/float64.md +698 -111
- package/fs.gr +1082 -0
- package/fs.md +630 -0
- package/hash.gr +142 -88
- package/hash.md +105 -17
- package/int16.md +178 -178
- package/int32.gr +26 -5
- package/int32.md +266 -231
- package/int64.gr +27 -2
- package/int64.md +266 -231
- package/int8.md +178 -178
- package/json.gr +366 -51
- package/json.md +431 -15
- package/list.gr +328 -31
- package/list.md +759 -336
- package/map.gr +20 -12
- package/map.md +266 -260
- package/marshal.gr +41 -40
- package/marshal.md +14 -14
- package/number.gr +278 -35
- package/number.md +688 -269
- package/option.md +162 -162
- package/package.json +5 -3
- package/path.gr +48 -0
- package/path.md +180 -89
- package/pervasives.gr +2 -2
- package/pervasives.md +275 -275
- package/priorityqueue.gr +7 -7
- package/priorityqueue.md +131 -131
- package/queue.gr +183 -29
- package/queue.md +404 -148
- package/random.md +43 -43
- package/range.gr +4 -4
- package/range.md +42 -42
- package/rational.md +123 -123
- package/regex.gr +52 -51
- package/regex.md +102 -102
- package/result.md +118 -118
- package/runtime/atof/common.md +39 -39
- package/runtime/atof/decimal.gr +6 -6
- package/runtime/atof/decimal.md +14 -14
- 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 +8 -8
- package/runtime/dataStructures.md +211 -211
- package/runtime/debugPrint.gr +4 -1
- package/runtime/debugPrint.md +9 -9
- package/runtime/equal.gr +99 -77
- package/runtime/equal.md +8 -8
- 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 +13 -13
- 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 +348 -342
- package/runtime/string.gr +37 -105
- package/runtime/string.md +20 -26
- package/runtime/unsafe/constants.md +24 -24
- package/runtime/unsafe/conv.md +19 -19
- 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 +147 -147
- package/set.gr +18 -11
- package/set.md +253 -247
- package/stack.gr +171 -2
- package/stack.md +371 -89
- package/string.gr +352 -557
- package/string.md +298 -255
- package/uint16.md +170 -170
- package/uint32.gr +25 -4
- package/uint32.md +249 -214
- package/uint64.gr +25 -5
- package/uint64.md +249 -214
- package/uint8.md +170 -170
- package/uri.gr +57 -53
- package/uri.md +88 -89
- package/wasi/file.gr +67 -59
- package/wasi/file.md +308 -308
- package/wasi/process.md +26 -26
- package/wasi/random.md +12 -12
- package/wasi/time.md +16 -16
- package/runtime/utils/printing.gr +0 -60
- package/runtime/utils/printing.md +0 -26
package/stack.gr
CHANGED
|
@@ -25,12 +25,12 @@ abstract record Stack<a> {
|
|
|
25
25
|
/**
|
|
26
26
|
* Creates a new stack with an initial storage of the given size. As values are
|
|
27
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
|
|
28
|
+
* won’t need to care about the storage size of your stack and can use the
|
|
29
29
|
* default size.
|
|
30
30
|
*
|
|
31
31
|
* @param size: The initial storage size of the stack
|
|
32
32
|
* @returns An empty stack
|
|
33
|
-
*
|
|
33
|
+
*
|
|
34
34
|
* @since v0.6.0
|
|
35
35
|
*/
|
|
36
36
|
provide let make = (size=16) => {
|
|
@@ -134,6 +134,95 @@ provide let copy = stack => {
|
|
|
134
134
|
{ size, array: Array.copy(array) }
|
|
135
135
|
}
|
|
136
136
|
|
|
137
|
+
/**
|
|
138
|
+
* Creates a list containing the elements of a stack.
|
|
139
|
+
*
|
|
140
|
+
* @param stack: The stack to convert
|
|
141
|
+
* @returns A list containing all stack values
|
|
142
|
+
*
|
|
143
|
+
* @example
|
|
144
|
+
* let stack = Stack.make()
|
|
145
|
+
* Stack.push(1, stack)
|
|
146
|
+
* Stack.push(2, stack)
|
|
147
|
+
* assert Stack.toList(stack) == [2, 1]
|
|
148
|
+
*
|
|
149
|
+
* @since v0.7.0
|
|
150
|
+
*/
|
|
151
|
+
provide let toList = stack => {
|
|
152
|
+
let size = stack.size
|
|
153
|
+
List.init(size, i => match (stack.array[size - i - 1]) {
|
|
154
|
+
Some(v) => v,
|
|
155
|
+
None => fail "Impossible: None in stack bounds on toList",
|
|
156
|
+
})
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Creates a stack from a list.
|
|
161
|
+
*
|
|
162
|
+
* @param list: The list to convert
|
|
163
|
+
* @returns A stack containing all list values
|
|
164
|
+
*
|
|
165
|
+
* @example
|
|
166
|
+
* let stack = Stack.fromList([3, 2, 1])
|
|
167
|
+
* assert Stack.pop(stack) == Some(3)
|
|
168
|
+
* assert Stack.pop(stack) == Some(2)
|
|
169
|
+
* assert Stack.pop(stack) == Some(1)
|
|
170
|
+
* assert Stack.pop(stack) == None
|
|
171
|
+
*
|
|
172
|
+
* @since v0.7.0
|
|
173
|
+
*/
|
|
174
|
+
provide let fromList = list => {
|
|
175
|
+
let stack = make(size=List.length(list))
|
|
176
|
+
List.forEach(v => push(v, stack), List.reverse(list))
|
|
177
|
+
stack
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Creates an array containing the elements of a stack.
|
|
182
|
+
*
|
|
183
|
+
* @param stack: The stack to convert
|
|
184
|
+
* @returns An array containing all stack values
|
|
185
|
+
*
|
|
186
|
+
* @example
|
|
187
|
+
* let stack = Stack.make()
|
|
188
|
+
* Stack.push(1, stack)
|
|
189
|
+
* Stack.push(2, stack)
|
|
190
|
+
* assert Stack.toArray(stack) == [> 2, 1]
|
|
191
|
+
*
|
|
192
|
+
* @since v0.7.0
|
|
193
|
+
*/
|
|
194
|
+
provide let toArray = stack => {
|
|
195
|
+
let size = stack.size
|
|
196
|
+
Array.init(size, i => match (stack.array[size - i - 1]) {
|
|
197
|
+
Some(v) => v,
|
|
198
|
+
None => fail "Impossible: None in stack bounds on toList",
|
|
199
|
+
})
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Creates a stack from an array.
|
|
204
|
+
*
|
|
205
|
+
* @param arr: The array to convert
|
|
206
|
+
* @returns A stack containing all array values
|
|
207
|
+
*
|
|
208
|
+
* @example
|
|
209
|
+
* let s = Stack.fromArray([> 3, 2, 1])
|
|
210
|
+
* assert Stack.pop(s) == Some(3)
|
|
211
|
+
* assert Stack.pop(s) == Some(2)
|
|
212
|
+
* assert Stack.pop(s) == Some(1)
|
|
213
|
+
* assert Stack.pop(s) == None
|
|
214
|
+
*
|
|
215
|
+
* @since v0.7.0
|
|
216
|
+
*/
|
|
217
|
+
provide let fromArray = arr => {
|
|
218
|
+
let arrLen = Array.length(arr)
|
|
219
|
+
let stack = make(size=arrLen)
|
|
220
|
+
for (let mut i = arrLen - 1; i >= 0; i -= 1) {
|
|
221
|
+
push(arr[i], stack)
|
|
222
|
+
}
|
|
223
|
+
stack
|
|
224
|
+
}
|
|
225
|
+
|
|
137
226
|
/**
|
|
138
227
|
* An immutable stack implementation.
|
|
139
228
|
*/
|
|
@@ -237,4 +326,84 @@ provide module Immutable {
|
|
|
237
326
|
{ data } => List.length(data),
|
|
238
327
|
}
|
|
239
328
|
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* Creates a list containing the elements of a stack.
|
|
332
|
+
*
|
|
333
|
+
* @param stack: The stack to convert
|
|
334
|
+
* @returns A list containing all stack values
|
|
335
|
+
*
|
|
336
|
+
* @example
|
|
337
|
+
* use Stack.{ module Immutable as Stack }
|
|
338
|
+
* let stack = Stack.empty
|
|
339
|
+
* let stack = Stack.push(1, stack)
|
|
340
|
+
* let stack = Stack.push(2, stack)
|
|
341
|
+
* assert Stack.toList(stack) == [2, 1]
|
|
342
|
+
*
|
|
343
|
+
* @since v0.7.0
|
|
344
|
+
*/
|
|
345
|
+
provide let toList = stack => {
|
|
346
|
+
stack.data
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* Creates a stack from a list.
|
|
351
|
+
*
|
|
352
|
+
* @param list: The list to convert
|
|
353
|
+
* @returns A stack containing all list values
|
|
354
|
+
*
|
|
355
|
+
* @example
|
|
356
|
+
* use Stack.{ module Immutable as Stack }
|
|
357
|
+
* let stack = Stack.fromList([2, 1])
|
|
358
|
+
* assert Stack.peek(stack) == Some(2)
|
|
359
|
+
* let stack = Stack.pop(stack)
|
|
360
|
+
* assert Stack.peek(stack) == Some(1)
|
|
361
|
+
* let stack = Stack.pop(stack)
|
|
362
|
+
* assert Stack.isEmpty(stack)
|
|
363
|
+
*
|
|
364
|
+
* @since v0.7.0
|
|
365
|
+
*/
|
|
366
|
+
provide let fromList = list => {
|
|
367
|
+
{ data: list, }
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* Creates an array containing the elements of a stack.
|
|
372
|
+
*
|
|
373
|
+
* @param stack: The stack to convert
|
|
374
|
+
* @returns An array containing all stack values
|
|
375
|
+
*
|
|
376
|
+
* @example
|
|
377
|
+
* use Stack.{ module Immutable as Stack }
|
|
378
|
+
* let stack = Stack.empty
|
|
379
|
+
* let stack = Stack.push(1, stack)
|
|
380
|
+
* let stack = Stack.push(2, stack)
|
|
381
|
+
* assert Stack.toArray(stack) == [> 2, 1]
|
|
382
|
+
*
|
|
383
|
+
* @since v0.7.0
|
|
384
|
+
*/
|
|
385
|
+
provide let toArray = stack => {
|
|
386
|
+
Array.fromList(stack.data)
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* Creates a stack from an array.
|
|
391
|
+
*
|
|
392
|
+
* @param arr: The array to convert
|
|
393
|
+
* @returns A stack containing all array values
|
|
394
|
+
*
|
|
395
|
+
* @example
|
|
396
|
+
* use Stack.{ module Immutable as Stack }
|
|
397
|
+
* let stack = Stack.fromArray([> 2, 1])
|
|
398
|
+
* assert Stack.peek(stack) == Some(2)
|
|
399
|
+
* let stack = Stack.pop(stack)
|
|
400
|
+
* assert Stack.peek(stack) == Some(1)
|
|
401
|
+
* let stack = Stack.pop(stack)
|
|
402
|
+
* assert Stack.isEmpty(stack)
|
|
403
|
+
*
|
|
404
|
+
* @since v0.7.0
|
|
405
|
+
*/
|
|
406
|
+
provide let fromArray = arr => {
|
|
407
|
+
{ data: Array.toList(arr), }
|
|
408
|
+
}
|
|
240
409
|
}
|