@grain/stdlib 0.5.3 → 0.5.5
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 +61 -0
- package/array.gr +65 -57
- package/array.md +54 -6
- package/buffer.gr +71 -1
- package/buffer.md +142 -0
- package/bytes.gr +52 -3
- package/bytes.md +117 -0
- package/char.gr +23 -20
- package/char.md +18 -3
- package/immutablemap.gr +493 -0
- package/immutablemap.md +479 -0
- package/immutablepriorityqueue.gr +44 -16
- package/immutablepriorityqueue.md +44 -1
- package/immutableset.gr +498 -0
- package/immutableset.md +449 -0
- package/int32.gr +39 -37
- package/int32.md +6 -0
- package/int64.gr +39 -37
- package/int64.md +6 -0
- package/list.gr +33 -24
- package/list.md +39 -10
- package/map.gr +19 -28
- package/marshal.gr +4 -4
- package/number.gr +727 -26
- package/number.md +345 -23
- package/option.gr +30 -26
- package/option.md +12 -0
- package/package.json +1 -1
- package/path.gr +787 -0
- package/path.md +727 -0
- package/pervasives.gr +3 -4
- package/pervasives.md +6 -1
- package/priorityqueue.gr +25 -5
- package/priorityqueue.md +30 -0
- package/queue.gr +22 -7
- package/queue.md +18 -1
- package/regex.gr +161 -65
- package/regex.md +70 -0
- package/result.gr +24 -20
- package/result.md +12 -0
- package/runtime/atof/common.gr +198 -0
- package/runtime/atof/common.md +243 -0
- package/runtime/atof/decimal.gr +663 -0
- package/runtime/atof/decimal.md +59 -0
- package/runtime/atof/lemire.gr +264 -0
- package/runtime/atof/lemire.md +6 -0
- package/runtime/atof/parse.gr +615 -0
- package/runtime/atof/parse.md +12 -0
- package/runtime/atof/slow.gr +238 -0
- package/runtime/atof/slow.md +6 -0
- package/runtime/atof/table.gr +2016 -0
- package/runtime/atof/table.md +12 -0
- package/runtime/{stringUtils.gr → atoi/parse.gr} +1 -1
- package/runtime/{stringUtils.md → atoi/parse.md} +1 -1
- package/runtime/bigint.gr +7 -7
- package/runtime/compare.gr +2 -1
- package/runtime/equal.gr +3 -2
- package/runtime/exception.gr +9 -5
- package/runtime/exception.md +8 -2
- package/runtime/gc.gr +2 -1
- package/runtime/malloc.gr +1 -3
- package/runtime/numberUtils.gr +13 -13
- package/runtime/numberUtils.md +6 -0
- package/runtime/numbers.gr +123 -39
- package/runtime/numbers.md +26 -0
- package/runtime/string.gr +4 -2
- package/runtime/unsafe/conv.gr +21 -41
- package/runtime/unsafe/conv.md +0 -3
- package/runtime/unsafe/printWasm.gr +4 -40
- package/runtime/utils/printing.gr +3 -3
- package/set.gr +25 -25
- package/stack.gr +14 -0
- package/stack.md +17 -0
- package/string.gr +313 -39
- package/string.md +99 -0
- package/sys/file.gr +1 -1
- package/sys/time.gr +4 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,66 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
### [0.5.5](https://github.com/grain-lang/grain/compare/stdlib-v0.5.4...stdlib-v0.5.5) (2022-12-05)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* **graindoc:** Add attribute for exceptions that may be thrown ([#1492](https://github.com/grain-lang/grain/issues/1492)) ([b2e75c7](https://github.com/grain-lang/grain/commit/b2e75c7452ef2544c768729c7a45e21ff31616d0))
|
|
9
|
+
* **graindoc:** Support deprecations on module docblocks ([#1498](https://github.com/grain-lang/grain/issues/1498)) ([b3dc85c](https://github.com/grain-lang/grain/commit/b3dc85c0fc311479de4e57774a075c3a922216ba))
|
|
10
|
+
* **stdlib:** Add `parse` function to Number module ([#1517](https://github.com/grain-lang/grain/issues/1517)) ([59e89d1](https://github.com/grain-lang/grain/commit/59e89d12b7fcf2626c8adb45c742a787171b7024))
|
|
11
|
+
* **stdlib:** Add `parseFloat` function to Number module ([#1288](https://github.com/grain-lang/grain/issues/1288)) ([e21f2b1](https://github.com/grain-lang/grain/commit/e21f2b137f7dcd67cccf9debf695db852dc2afc5))
|
|
12
|
+
* **stdlib:** Add `split` function to Regex module ([#1469](https://github.com/grain-lang/grain/issues/1469)) ([0c1eb73](https://github.com/grain-lang/grain/commit/0c1eb73d01e30f457138c6e3b603a9faddcf8e9b))
|
|
13
|
+
* **stdlib:** Add `splitAll` function to Regex module ([0c1eb73](https://github.com/grain-lang/grain/commit/0c1eb73d01e30f457138c6e3b603a9faddcf8e9b))
|
|
14
|
+
* **stdlib:** Add Path module for working with system paths ([#1452](https://github.com/grain-lang/grain/issues/1452)) ([900e976](https://github.com/grain-lang/grain/commit/900e976654565b3618e2215e9b7cefbda873d9a8))
|
|
15
|
+
* **stdlib:** Improve performance in Array & List modules ([#1487](https://github.com/grain-lang/grain/issues/1487)) ([2168f6a](https://github.com/grain-lang/grain/commit/2168f6ade151548bc655debeb8a1bc09ce87cb77))
|
|
16
|
+
* **stdlib:** Improve performance of `flatMap`, `some`, and `every` functions in Array module ([2168f6a](https://github.com/grain-lang/grain/commit/2168f6ade151548bc655debeb8a1bc09ce87cb77))
|
|
17
|
+
* **stdlib:** Improve performance of `some` and `every` functions in List module ([2168f6a](https://github.com/grain-lang/grain/commit/2168f6ade151548bc655debeb8a1bc09ce87cb77))
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
### Bug Fixes
|
|
21
|
+
|
|
22
|
+
* **grainfmt:** Add parentheses around some binops for precedence clarity ([#1514](https://github.com/grain-lang/grain/issues/1514)) ([3ac27cc](https://github.com/grain-lang/grain/commit/3ac27cc6e17b896dae0ef2cb5f5de510c7c2dd60))
|
|
23
|
+
* **grainfmt:** Handle multiple line items and comments better ([#1460](https://github.com/grain-lang/grain/issues/1460)) ([5395fd4](https://github.com/grain-lang/grain/commit/5395fd45b79fb3bcf3dd1ec52a1d5973a23a4bdc))
|
|
24
|
+
* **runtime:** Properly divide bigints in the number type ([59e89d1](https://github.com/grain-lang/grain/commit/59e89d12b7fcf2626c8adb45c742a787171b7024))
|
|
25
|
+
|
|
26
|
+
### [0.5.4](https://github.com/grain-lang/grain/compare/stdlib-v0.5.3...stdlib-v0.5.4) (2022-11-12)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
### Features
|
|
30
|
+
|
|
31
|
+
* **stdlib:** Add `empty` constant to ImmutablePriorityQueue module ([427335f](https://github.com/grain-lang/grain/commit/427335fa5c211445f727a650ca06adacfe9c5310))
|
|
32
|
+
* **stdlib:** Add `empty` constant to Queue module ([427335f](https://github.com/grain-lang/grain/commit/427335fa5c211445f727a650ca06adacfe9c5310))
|
|
33
|
+
* **stdlib:** Add `empty` constant to Stack module ([427335f](https://github.com/grain-lang/grain/commit/427335fa5c211445f727a650ca06adacfe9c5310))
|
|
34
|
+
* **stdlib:** Add `exp` function to Number module ([5af9a99](https://github.com/grain-lang/grain/commit/5af9a99b2ec3b4a2d6745cb22b70defe2b366cfa))
|
|
35
|
+
* **stdlib:** Add `factorial` function to Number module ([5af9a99](https://github.com/grain-lang/grain/commit/5af9a99b2ec3b4a2d6745cb22b70defe2b366cfa))
|
|
36
|
+
* **stdlib:** Add `gamma` function to Number module ([5af9a99](https://github.com/grain-lang/grain/commit/5af9a99b2ec3b4a2d6745cb22b70defe2b366cfa))
|
|
37
|
+
* **stdlib:** Add `infinity` constant to the Number module ([c24f6c1](https://github.com/grain-lang/grain/commit/c24f6c1cfae87632a003c0337c29ec98a80cfda2))
|
|
38
|
+
* **stdlib:** Add `nan` constant to the Number module ([c24f6c1](https://github.com/grain-lang/grain/commit/c24f6c1cfae87632a003c0337c29ec98a80cfda2))
|
|
39
|
+
* **stdlib:** Add `pow` function to Number module ([5af9a99](https://github.com/grain-lang/grain/commit/5af9a99b2ec3b4a2d6745cb22b70defe2b366cfa))
|
|
40
|
+
* **stdlib:** Add `replaceAll` function to String module ([5606cd2](https://github.com/grain-lang/grain/commit/5606cd246583884175b135cbeb29024400651b34))
|
|
41
|
+
* **stdlib:** Add `replaceFirst` function to String module ([5606cd2](https://github.com/grain-lang/grain/commit/5606cd246583884175b135cbeb29024400651b34))
|
|
42
|
+
* **stdlib:** Add `replaceLast` function to String module ([5606cd2](https://github.com/grain-lang/grain/commit/5606cd246583884175b135cbeb29024400651b34))
|
|
43
|
+
* **stdlib:** Add `tan` function to Number module ([5af9a99](https://github.com/grain-lang/grain/commit/5af9a99b2ec3b4a2d6745cb22b70defe2b366cfa))
|
|
44
|
+
* **stdlib:** Add `toDegrees` function to Number module ([5af9a99](https://github.com/grain-lang/grain/commit/5af9a99b2ec3b4a2d6745cb22b70defe2b366cfa))
|
|
45
|
+
* **stdlib:** Add `toRadians` function to Number module ([5af9a99](https://github.com/grain-lang/grain/commit/5af9a99b2ec3b4a2d6745cb22b70defe2b366cfa))
|
|
46
|
+
* **stdlib:** Add additional functions to Number module ([#1443](https://github.com/grain-lang/grain/issues/1443)) ([5af9a99](https://github.com/grain-lang/grain/commit/5af9a99b2ec3b4a2d6745cb22b70defe2b366cfa))
|
|
47
|
+
* **stdlib:** Add replacement functions to String module ([#1441](https://github.com/grain-lang/grain/issues/1441)) ([5606cd2](https://github.com/grain-lang/grain/commit/5606cd246583884175b135cbeb29024400651b34))
|
|
48
|
+
* **stdlib:** Added `empty` constant to immutable data structures ([#1466](https://github.com/grain-lang/grain/issues/1466)) ([427335f](https://github.com/grain-lang/grain/commit/427335fa5c211445f727a650ca06adacfe9c5310))
|
|
49
|
+
* **stdlib:** Implement `fromArray` in PriorityQueue & ImmutablePriorityQueue modules ([#1451](https://github.com/grain-lang/grain/issues/1451)) ([d321f84](https://github.com/grain-lang/grain/commit/d321f84174fee2a340745a9f55994fbfa23f6c7a))
|
|
50
|
+
* **stdlib:** Implement ImmutableMap and ImmutableSet ([#1414](https://github.com/grain-lang/grain/issues/1414)) ([b31120d](https://github.com/grain-lang/grain/commit/b31120d41be668c48b9bca9f2b944616371a8ab4))
|
|
51
|
+
* **stdlib:** Improved efficiency of constructing a PriorityQueue from a List ([d321f84](https://github.com/grain-lang/grain/commit/d321f84174fee2a340745a9f55994fbfa23f6c7a))
|
|
52
|
+
* **stdlib:** Optimize string trimming ([#1442](https://github.com/grain-lang/grain/issues/1442)) ([0212247](https://github.com/grain-lang/grain/commit/0212247a7fbf0d54085959de2853f3fe66cd8b12))
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
### Bug Fixes
|
|
56
|
+
|
|
57
|
+
* **compiler:** Panic immediately when out of memory ([#1450](https://github.com/grain-lang/grain/issues/1450)) ([943d47d](https://github.com/grain-lang/grain/commit/943d47dddde2d88fd96727e9d7ed8501efec42ef))
|
|
58
|
+
* **grainfmt:** Handle chained value bindings properly ([#1467](https://github.com/grain-lang/grain/issues/1467)) ([07bfcd3](https://github.com/grain-lang/grain/commit/07bfcd3f15c34ef99b05531591b1473f206b7395))
|
|
59
|
+
* **grainfmt:** Indent lines when wrapping infix operators ([#1465](https://github.com/grain-lang/grain/issues/1465)) ([d705849](https://github.com/grain-lang/grain/commit/d705849ea8d9073e608576b77adeae834c454e0b))
|
|
60
|
+
* **runtime:** Handle bigint mul/div within Number correctly ([#1475](https://github.com/grain-lang/grain/issues/1475)) ([0fe8aa6](https://github.com/grain-lang/grain/commit/0fe8aa6a96a9c5ebf2f2bf2e1f28578badfb337f))
|
|
61
|
+
* **stdlib:** Fix anchoring behavior in Regex.replaceAll ([#1440](https://github.com/grain-lang/grain/issues/1440)) ([d513eff](https://github.com/grain-lang/grain/commit/d513effe569d0aa0d44c974596fd285f1ad8d57d))
|
|
62
|
+
* **stdlib:** Fix handling of `NaN` and `Infinity` in Number module ([#1457](https://github.com/grain-lang/grain/issues/1457)) ([c24f6c1](https://github.com/grain-lang/grain/commit/c24f6c1cfae87632a003c0337c29ec98a80cfda2))
|
|
63
|
+
|
|
3
64
|
### [0.5.3](https://github.com/grain-lang/grain/compare/stdlib-v0.5.2...stdlib-v0.5.3) (2022-08-05)
|
|
4
65
|
|
|
5
66
|
|
package/array.gr
CHANGED
|
@@ -12,34 +12,27 @@ import WasmI32 from "runtime/unsafe/wasmi32"
|
|
|
12
12
|
import Memory from "runtime/unsafe/memory"
|
|
13
13
|
import { allocateArray, tagSimpleNumber } from "runtime/dataStructures"
|
|
14
14
|
import Exception from "runtime/exception"
|
|
15
|
+
import { coerceNumberToWasmI32 } from "runtime/numbers"
|
|
15
16
|
|
|
16
17
|
/**
|
|
17
18
|
* @section Values: Functions for working with the Array data type.
|
|
18
19
|
*/
|
|
19
20
|
|
|
20
|
-
@
|
|
21
|
-
let mut _ARRAY_LENGTH_OFFSET =
|
|
22
|
-
@
|
|
23
|
-
let mut _ARRAY_START_OFFSET =
|
|
21
|
+
@unsafe
|
|
22
|
+
let mut _ARRAY_LENGTH_OFFSET = 4n
|
|
23
|
+
@unsafe
|
|
24
|
+
let mut _ARRAY_START_OFFSET = 8n
|
|
24
25
|
|
|
25
|
-
@
|
|
26
|
-
let
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
initPtr()
|
|
31
|
-
|
|
32
|
-
@disableGC
|
|
33
|
-
let initLength = length => {
|
|
34
|
-
let length = WasmI32.fromGrain(length)
|
|
35
|
-
if (WasmI32.eqz(WasmI32.and(length, 1n))) {
|
|
26
|
+
@unsafe
|
|
27
|
+
let checkLength = length => {
|
|
28
|
+
let ptr = WasmI32.fromGrain(length)
|
|
29
|
+
if (WasmI32.eqz(WasmI32.and(ptr, 1n))) {
|
|
36
30
|
throw Exception.InvalidArgument("Length argument must be an integer")
|
|
37
31
|
}
|
|
38
|
-
let
|
|
39
|
-
if (WasmI32.ltS(
|
|
32
|
+
let len = WasmI32.shrS(ptr, 1n)
|
|
33
|
+
if (WasmI32.ltS(len, 0n)) {
|
|
40
34
|
throw Exception.InvalidArgument("Length argument must be non-negative")
|
|
41
35
|
}
|
|
42
|
-
length
|
|
43
36
|
}
|
|
44
37
|
|
|
45
38
|
/**
|
|
@@ -48,18 +41,15 @@ let initLength = length => {
|
|
|
48
41
|
* @param array: The array to inspect
|
|
49
42
|
* @returns The number of elements in the array
|
|
50
43
|
*
|
|
44
|
+
* @example Array.length([> 1, 2, 3, 4, 5]) == 5
|
|
51
45
|
* @since v0.1.0
|
|
52
46
|
*/
|
|
53
|
-
@
|
|
54
|
-
export let
|
|
47
|
+
@unsafe
|
|
48
|
+
export let length = array => {
|
|
55
49
|
let ptr = WasmI32.fromGrain(array: Array<a>)
|
|
56
|
-
|
|
57
|
-
Memory.decRef(ptr)
|
|
58
|
-
Memory.decRef(WasmI32.fromGrain(length))
|
|
59
|
-
ret
|
|
50
|
+
tagSimpleNumber(WasmI32.load(ptr, _ARRAY_LENGTH_OFFSET))
|
|
60
51
|
}
|
|
61
52
|
|
|
62
|
-
// [FIXME] (#793) Type annotation breaks compiler
|
|
63
53
|
/**
|
|
64
54
|
* Creates a new array of the specified length with each element being
|
|
65
55
|
* initialized with the given value.
|
|
@@ -68,18 +58,18 @@ export let rec length = array => {
|
|
|
68
58
|
* @param item: The value to store at each index
|
|
69
59
|
* @returns The new array
|
|
70
60
|
*
|
|
61
|
+
* @throws InvalidArgument(String): When `length` is not an integer
|
|
62
|
+
* @throws InvalidArgument(String): When `length` is negative
|
|
63
|
+
*
|
|
71
64
|
* @example Array.make(5, "foo") // [> "foo", "foo", "foo", "foo", "foo"]
|
|
72
65
|
*
|
|
73
66
|
* @since v0.1.0
|
|
74
67
|
*/
|
|
75
|
-
@
|
|
76
|
-
export let
|
|
77
|
-
(
|
|
78
|
-
length: Number,
|
|
79
|
-
item: a,
|
|
80
|
-
) => {
|
|
68
|
+
@unsafe
|
|
69
|
+
export let make: (Number, a) -> Array<a> = (length: Number, item: a) => {
|
|
81
70
|
let lengthArg = length
|
|
82
|
-
|
|
71
|
+
checkLength(length)
|
|
72
|
+
let length = coerceNumberToWasmI32(length)
|
|
83
73
|
let byteLength = WasmI32.mul(length, 4n)
|
|
84
74
|
let array = allocateArray(length)
|
|
85
75
|
for (let mut i = 0n; WasmI32.ltS(i, byteLength); i = WasmI32.add(i, 4n)) {
|
|
@@ -89,11 +79,7 @@ export let rec make /*: (Number, a) -> Array<a>*/ =
|
|
|
89
79
|
_ARRAY_START_OFFSET
|
|
90
80
|
)
|
|
91
81
|
}
|
|
92
|
-
|
|
93
|
-
Memory.decRef(WasmI32.fromGrain(lengthArg))
|
|
94
|
-
Memory.decRef(WasmI32.fromGrain(item))
|
|
95
|
-
Memory.decRef(WasmI32.fromGrain(make))
|
|
96
|
-
ret
|
|
82
|
+
WasmI32.toGrain(array): Array<a>
|
|
97
83
|
}
|
|
98
84
|
|
|
99
85
|
/**
|
|
@@ -105,34 +91,34 @@ export let rec make /*: (Number, a) -> Array<a>*/ =
|
|
|
105
91
|
* @param fn: The initializer function to call with each index, where the value returned will be used to initialize the element
|
|
106
92
|
* @returns The new array
|
|
107
93
|
*
|
|
94
|
+
* @throws InvalidArgument(String): When `length` is not an integer
|
|
95
|
+
* @throws InvalidArgument(String): When `length` is negative
|
|
96
|
+
*
|
|
108
97
|
* @example Array.init(5, n => n + 3) // [> 3, 4, 5, 6, 7]
|
|
109
98
|
*
|
|
110
99
|
* @since v0.1.0
|
|
111
100
|
*/
|
|
112
|
-
@
|
|
113
|
-
export let
|
|
101
|
+
@unsafe
|
|
102
|
+
export let init: (Number, Number -> a) -> Array<a> =
|
|
114
103
|
(
|
|
115
104
|
length: Number,
|
|
116
105
|
fn: Number -> a,
|
|
117
106
|
) => {
|
|
118
|
-
|
|
107
|
+
checkLength(length)
|
|
108
|
+
let length = coerceNumberToWasmI32(length)
|
|
119
109
|
let byteLength = WasmI32.mul(length, 4n)
|
|
120
110
|
let array = allocateArray(length)
|
|
121
111
|
let mut index = 0n
|
|
122
112
|
for (let mut i = 0n; WasmI32.ltS(i, byteLength); i = WasmI32.add(i, 4n)) {
|
|
123
|
-
Memory.incRef(WasmI32.fromGrain(fn))
|
|
124
113
|
// [FIXME] This line fails the array/map test suite (#815)
|
|
125
|
-
//assert !WasmI32.eqz(WasmI32.and(WasmI32.fromGrain(index), 1n)) // must be a simple int for next line to be correct
|
|
114
|
+
// assert !WasmI32.eqz(WasmI32.and(WasmI32.fromGrain(index), 1n)) // must be a simple int for next line to be correct
|
|
126
115
|
WasmI32.store(
|
|
127
116
|
WasmI32.add(array, i),
|
|
128
|
-
WasmI32.fromGrain(fn(tagSimpleNumber(index))),
|
|
117
|
+
Memory.incRef(WasmI32.fromGrain(fn(tagSimpleNumber(index)))),
|
|
129
118
|
_ARRAY_START_OFFSET
|
|
130
119
|
)
|
|
131
|
-
//WasmI32.store(WasmI32.add(array, i), WasmI32.fromGrain(4), _ARRAY_START_OFFSET)
|
|
132
120
|
index = WasmI32.add(index, 1n)
|
|
133
121
|
}
|
|
134
|
-
Memory.decRef(WasmI32.fromGrain(fn))
|
|
135
|
-
Memory.decRef(WasmI32.fromGrain(init))
|
|
136
122
|
WasmI32.toGrain(array): Array<a>
|
|
137
123
|
}
|
|
138
124
|
|
|
@@ -145,6 +131,7 @@ export let rec init /*: (Number, Number -> a) -> Array<a>*/ =
|
|
|
145
131
|
* @param index: The index to access
|
|
146
132
|
* @param array: The array to access
|
|
147
133
|
* @returns The element from the array
|
|
134
|
+
* @example Array.get(1,[> 1, 2, 3, 4, 5]) == 2
|
|
148
135
|
*
|
|
149
136
|
* @since v0.1.0
|
|
150
137
|
* @history v0.2.0: Argument order changed to data-last
|
|
@@ -162,6 +149,7 @@ export let get = (index, array) => {
|
|
|
162
149
|
* @param index: The index to update
|
|
163
150
|
* @param value: The value to store
|
|
164
151
|
* @param array: The array to update
|
|
152
|
+
* @example Array.set(1, 9, [> 1, 2, 3, 4, 5]) == [> 1, 9, 3, 4, 5]
|
|
165
153
|
*
|
|
166
154
|
* @since v0.1.0
|
|
167
155
|
* @history v0.2.0: Argument order changed to data-last
|
|
@@ -177,6 +165,7 @@ export let set = (index, value, array) => {
|
|
|
177
165
|
* @param array1: The array containing elements to appear first
|
|
178
166
|
* @param array2: The array containing elements to appear second
|
|
179
167
|
* @returns The new array containing elements from `array1` followed by elements from `array2`
|
|
168
|
+
* @example Array.append([> 1, 2], [> 3, 4, 5]) == [> 1, 2, 3, 4, 5]
|
|
180
169
|
*
|
|
181
170
|
* @since v0.1.0
|
|
182
171
|
*/
|
|
@@ -199,6 +188,7 @@ export let append = (array1, array2) => {
|
|
|
199
188
|
*
|
|
200
189
|
* @param arrays: A list containing all arrays to combine
|
|
201
190
|
* @returns The new array
|
|
191
|
+
* @example Array.concat([[> 1, 2], [> 3, 4], [> 5, 6]]) == [> 1, 2, 3, 4, 5, 6]
|
|
202
192
|
*
|
|
203
193
|
* @since v0.1.0
|
|
204
194
|
*/
|
|
@@ -420,7 +410,19 @@ export let reducei = (fn, initial, array) => {
|
|
|
420
410
|
* @since v0.3.0
|
|
421
411
|
*/
|
|
422
412
|
export let flatMap = (fn, array) => {
|
|
423
|
-
|
|
413
|
+
let nested = map(fn, array)
|
|
414
|
+
let arrLen = reduce((acc, arr) => acc + length(arr), 0, nested)
|
|
415
|
+
let mut outerI = 0
|
|
416
|
+
let mut innerI = 0
|
|
417
|
+
init(arrLen, i => {
|
|
418
|
+
if (innerI >= length(nested[outerI])) {
|
|
419
|
+
innerI = 0
|
|
420
|
+
outerI += 1
|
|
421
|
+
}
|
|
422
|
+
let res = nested[outerI][innerI]
|
|
423
|
+
innerI += 1
|
|
424
|
+
res
|
|
425
|
+
})
|
|
424
426
|
}
|
|
425
427
|
|
|
426
428
|
/**
|
|
@@ -434,9 +436,12 @@ export let flatMap = (fn, array) => {
|
|
|
434
436
|
* @since v0.3.0
|
|
435
437
|
*/
|
|
436
438
|
export let every = (fn, array) => {
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
439
|
+
let len = length(array)
|
|
440
|
+
let mut all = true
|
|
441
|
+
for (let mut index = 0; all && index < len; index += 1) {
|
|
442
|
+
all = fn(array[index])
|
|
443
|
+
}
|
|
444
|
+
all
|
|
440
445
|
}
|
|
441
446
|
|
|
442
447
|
/**
|
|
@@ -450,9 +455,12 @@ export let every = (fn, array) => {
|
|
|
450
455
|
* @since v0.3.0
|
|
451
456
|
*/
|
|
452
457
|
export let some = (fn, array) => {
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
458
|
+
let len = length(array)
|
|
459
|
+
let mut found = false
|
|
460
|
+
for (let mut index = 0; !found && index < len; index += 1) {
|
|
461
|
+
found = fn(array[index])
|
|
462
|
+
}
|
|
463
|
+
found
|
|
456
464
|
}
|
|
457
465
|
|
|
458
466
|
/**
|
|
@@ -794,12 +802,12 @@ export let unique = array => {
|
|
|
794
802
|
* The first tuple will contain the first item of each array, the second tuple
|
|
795
803
|
* will contain the second item of each array, and so on.
|
|
796
804
|
*
|
|
797
|
-
* Calling this function with arrays of different sizes will throw an error.
|
|
798
|
-
*
|
|
799
805
|
* @param array1: The array to provide values for the first tuple element
|
|
800
806
|
* @param array2: The array to provide values for the second tuple element
|
|
801
807
|
* @returns The new array containing indexed pairs of `(a, b)`
|
|
802
808
|
*
|
|
809
|
+
* @throws Failure(String): When the arrays have different sizes
|
|
810
|
+
*
|
|
803
811
|
* @since v0.4.0
|
|
804
812
|
*/
|
|
805
813
|
export let zip = (array1: Array<a>, array2: Array<b>) => {
|
|
@@ -819,7 +827,7 @@ export let zip = (array1: Array<a>, array2: Array<b>) => {
|
|
|
819
827
|
* applying the function to the first elements of each array, the second element
|
|
820
828
|
* will contain the result of applying the function to the second elements of
|
|
821
829
|
* each array, and so on.
|
|
822
|
-
*
|
|
830
|
+
*
|
|
823
831
|
* Calling this function with arrays of different sizes will cause the returned
|
|
824
832
|
* array to have the length of the smaller array.
|
|
825
833
|
*
|
|
@@ -830,7 +838,7 @@ export let zip = (array1: Array<a>, array2: Array<b>) => {
|
|
|
830
838
|
*
|
|
831
839
|
* @example Array.zipWith((a, b) => a + b, [> 1, 2, 3], [> 4, 5, 6]) // [> 5, 7, 9]
|
|
832
840
|
* @example Array.zipWith((a, b) => a * b, [> 1, 2, 3], [> 4, 5]) // [> 4, 10]
|
|
833
|
-
*
|
|
841
|
+
*
|
|
834
842
|
* @since v0.5.3
|
|
835
843
|
*/
|
|
836
844
|
export let zipWith = (fn, array1: Array<a>, array2: Array<b>) => {
|
package/array.md
CHANGED
|
@@ -50,6 +50,12 @@ Returns:
|
|
|
50
50
|
|----|-----------|
|
|
51
51
|
|`Number`|The number of elements in the array|
|
|
52
52
|
|
|
53
|
+
Examples:
|
|
54
|
+
|
|
55
|
+
```grain
|
|
56
|
+
Array.length([> 1, 2, 3, 4, 5]) == 5
|
|
57
|
+
```
|
|
58
|
+
|
|
53
59
|
### Array.**make**
|
|
54
60
|
|
|
55
61
|
<details disabled>
|
|
@@ -77,6 +83,13 @@ Returns:
|
|
|
77
83
|
|----|-----------|
|
|
78
84
|
|`Array<a>`|The new array|
|
|
79
85
|
|
|
86
|
+
Throws:
|
|
87
|
+
|
|
88
|
+
`InvalidArgument(String)`
|
|
89
|
+
|
|
90
|
+
* When `length` is not an integer
|
|
91
|
+
* When `length` is negative
|
|
92
|
+
|
|
80
93
|
Examples:
|
|
81
94
|
|
|
82
95
|
```grain
|
|
@@ -111,6 +124,13 @@ Returns:
|
|
|
111
124
|
|----|-----------|
|
|
112
125
|
|`Array<a>`|The new array|
|
|
113
126
|
|
|
127
|
+
Throws:
|
|
128
|
+
|
|
129
|
+
`InvalidArgument(String)`
|
|
130
|
+
|
|
131
|
+
* When `length` is not an integer
|
|
132
|
+
* When `length` is negative
|
|
133
|
+
|
|
114
134
|
Examples:
|
|
115
135
|
|
|
116
136
|
```grain
|
|
@@ -153,6 +173,12 @@ Returns:
|
|
|
153
173
|
|----|-----------|
|
|
154
174
|
|`a`|The element from the array|
|
|
155
175
|
|
|
176
|
+
Examples:
|
|
177
|
+
|
|
178
|
+
```grain
|
|
179
|
+
Array.get(1,[> 1, 2, 3, 4, 5]) == 2
|
|
180
|
+
```
|
|
181
|
+
|
|
156
182
|
### Array.**set**
|
|
157
183
|
|
|
158
184
|
<details>
|
|
@@ -184,6 +210,12 @@ Parameters:
|
|
|
184
210
|
|`value`|`a`|The value to store|
|
|
185
211
|
|`array`|`Array<a>`|The array to update|
|
|
186
212
|
|
|
213
|
+
Examples:
|
|
214
|
+
|
|
215
|
+
```grain
|
|
216
|
+
Array.set(1, 9, [> 1, 2, 3, 4, 5]) == [> 1, 9, 3, 4, 5]
|
|
217
|
+
```
|
|
218
|
+
|
|
187
219
|
### Array.**append**
|
|
188
220
|
|
|
189
221
|
<details disabled>
|
|
@@ -211,6 +243,12 @@ Returns:
|
|
|
211
243
|
|----|-----------|
|
|
212
244
|
|`Array<a>`|The new array containing elements from `array1` followed by elements from `array2`|
|
|
213
245
|
|
|
246
|
+
Examples:
|
|
247
|
+
|
|
248
|
+
```grain
|
|
249
|
+
Array.append([> 1, 2], [> 3, 4, 5]) == [> 1, 2, 3, 4, 5]
|
|
250
|
+
```
|
|
251
|
+
|
|
214
252
|
### Array.**concat**
|
|
215
253
|
|
|
216
254
|
<details disabled>
|
|
@@ -237,6 +275,12 @@ Returns:
|
|
|
237
275
|
|----|-----------|
|
|
238
276
|
|`Array<a>`|The new array|
|
|
239
277
|
|
|
278
|
+
Examples:
|
|
279
|
+
|
|
280
|
+
```grain
|
|
281
|
+
Array.concat([[> 1, 2], [> 3, 4], [> 5, 6]]) == [> 1, 2, 3, 4, 5, 6]
|
|
282
|
+
```
|
|
283
|
+
|
|
240
284
|
### Array.**copy**
|
|
241
285
|
|
|
242
286
|
<details disabled>
|
|
@@ -520,7 +564,7 @@ No other changes yet.
|
|
|
520
564
|
</details>
|
|
521
565
|
|
|
522
566
|
```grain
|
|
523
|
-
flatMap : ((
|
|
567
|
+
flatMap : ((b -> Array<a>), Array<b>) -> Array<a>
|
|
524
568
|
```
|
|
525
569
|
|
|
526
570
|
Produces a new array by calling a function on each element
|
|
@@ -532,14 +576,14 @@ Parameters:
|
|
|
532
576
|
|
|
533
577
|
|param|type|description|
|
|
534
578
|
|-----|----|-----------|
|
|
535
|
-
|`fn`|`
|
|
536
|
-
|`array`|`Array<
|
|
579
|
+
|`fn`|`b -> Array<a>`|The function to be called on each element, where the value returned will be an array that gets appended to the new array|
|
|
580
|
+
|`array`|`Array<b>`|The array to iterate|
|
|
537
581
|
|
|
538
582
|
Returns:
|
|
539
583
|
|
|
540
584
|
|type|description|
|
|
541
585
|
|----|-----------|
|
|
542
|
-
|`Array<
|
|
586
|
+
|`Array<a>`|The new array|
|
|
543
587
|
|
|
544
588
|
### Array.**every**
|
|
545
589
|
|
|
@@ -969,8 +1013,6 @@ Produces a new array filled with tuples of elements from both given arrays.
|
|
|
969
1013
|
The first tuple will contain the first item of each array, the second tuple
|
|
970
1014
|
will contain the second item of each array, and so on.
|
|
971
1015
|
|
|
972
|
-
Calling this function with arrays of different sizes will throw an error.
|
|
973
|
-
|
|
974
1016
|
Parameters:
|
|
975
1017
|
|
|
976
1018
|
|param|type|description|
|
|
@@ -984,6 +1026,12 @@ Returns:
|
|
|
984
1026
|
|----|-----------|
|
|
985
1027
|
|`Array<(a, b)>`|The new array containing indexed pairs of `(a, b)`|
|
|
986
1028
|
|
|
1029
|
+
Throws:
|
|
1030
|
+
|
|
1031
|
+
`Failure(String)`
|
|
1032
|
+
|
|
1033
|
+
* When the arrays have different sizes
|
|
1034
|
+
|
|
987
1035
|
### Array.**zipWith**
|
|
988
1036
|
|
|
989
1037
|
<details disabled>
|
package/buffer.gr
CHANGED
|
@@ -66,7 +66,7 @@ let autogrow = (len, buf) => {
|
|
|
66
66
|
|
|
67
67
|
/* Memcopies bytes from a source byte sequence to a destination byte sequence via pointers */
|
|
68
68
|
@unsafe
|
|
69
|
-
let
|
|
69
|
+
let appendBytes = (srcOff, dstOff, len, src, dst) => {
|
|
70
70
|
let (+) = WasmI32.add
|
|
71
71
|
Memory.copy(dst + _VALUE_OFFSET + dstOff, src + _VALUE_OFFSET + srcOff, len)
|
|
72
72
|
}
|
|
@@ -114,6 +114,8 @@ let addInt32help = (value, buffer) => {
|
|
|
114
114
|
* @param initialSize: The initial size of the buffer
|
|
115
115
|
* @returns The new buffer
|
|
116
116
|
*
|
|
117
|
+
* @throws InvalidArgument(String): When the `initialSize` is a negative number
|
|
118
|
+
*
|
|
117
119
|
* @since v0.4.0
|
|
118
120
|
*/
|
|
119
121
|
export let make = initialSize => {
|
|
@@ -168,6 +170,9 @@ export let reset = buffer => {
|
|
|
168
170
|
* @param length: The number of bytes to truncate the buffer to
|
|
169
171
|
* @param buffer: The buffer to truncate
|
|
170
172
|
*
|
|
173
|
+
* @throws IndexOutOfBounds: When the `length` is negative
|
|
174
|
+
* @throws IndexOutOfBounds: When the `length` is greater than the buffer size
|
|
175
|
+
*
|
|
171
176
|
* @since v0.4.0
|
|
172
177
|
*/
|
|
173
178
|
@unsafe
|
|
@@ -202,6 +207,10 @@ export let toBytes = buffer => {
|
|
|
202
207
|
* @param buffer: The buffer to copy from
|
|
203
208
|
* @returns A byte sequence with bytes copied from the buffer
|
|
204
209
|
*
|
|
210
|
+
* @throws IndexOutOfBounds: When `start` is negative
|
|
211
|
+
* @throws IndexOutOfBounds: When `start` is greater than or equal to the buffer size
|
|
212
|
+
* @throws IndexOutOfBounds: When `start + length` is greater than the buffer size
|
|
213
|
+
*
|
|
205
214
|
* @since v0.4.0
|
|
206
215
|
*/
|
|
207
216
|
export let toBytesSlice = (start, length, buffer) => {
|
|
@@ -329,6 +338,11 @@ export let addStringSlice = (start: Number, end, string, buffer) => {
|
|
|
329
338
|
* @param bytes: The byte sequence to append
|
|
330
339
|
* @param buffer: The buffer to mutate
|
|
331
340
|
*
|
|
341
|
+
* @throws IndexOutOfBounds: When the `start` is negative
|
|
342
|
+
* @throws IndexOutOfBounds: When the `start` is greater than or equal to the `bytes` size
|
|
343
|
+
* @throws IndexOutOfBounds: When the `length` is negative
|
|
344
|
+
* @throws IndexOutOfBounds: When the `length` is greater than the `bytes` length minus `start`
|
|
345
|
+
*
|
|
332
346
|
* @since v0.4.0
|
|
333
347
|
*/
|
|
334
348
|
@unsafe
|
|
@@ -410,6 +424,10 @@ export let addBufferSlice = (start, length, srcBuffer, dstBuffer) => {
|
|
|
410
424
|
* @param buffer: The buffer to access
|
|
411
425
|
* @returns A 32-bit integer representing a signed 8-bit integer that starts at the given index
|
|
412
426
|
*
|
|
427
|
+
* @throws IndexOutOfBounds: When `index` is negative
|
|
428
|
+
* @throws IndexOutOfBounds: When `index` is greater than or equal to the buffer size
|
|
429
|
+
* @throws IndexOutOfBounds: When `index + 1` is greater than the buffer size
|
|
430
|
+
*
|
|
413
431
|
* @since v0.4.0
|
|
414
432
|
*/
|
|
415
433
|
export let getInt8S = (index, buffer) => {
|
|
@@ -424,6 +442,10 @@ export let getInt8S = (index, buffer) => {
|
|
|
424
442
|
* @param buffer: The buffer to access
|
|
425
443
|
* @returns A 32-bit integer representing an unsigned 8-bit integer that starts at the given index
|
|
426
444
|
*
|
|
445
|
+
* @throws IndexOutOfBounds: When `index` is negative
|
|
446
|
+
* @throws IndexOutOfBounds: When `index` is greater than or equal to the buffer size
|
|
447
|
+
* @throws IndexOutOfBounds: When `index + 1` is greater than the buffer size
|
|
448
|
+
*
|
|
427
449
|
* @since v0.4.0
|
|
428
450
|
*/
|
|
429
451
|
export let getInt8U = (index, buffer) => {
|
|
@@ -438,6 +460,10 @@ export let getInt8U = (index, buffer) => {
|
|
|
438
460
|
* @param value: The value to set
|
|
439
461
|
* @param buffer: The buffer to mutate
|
|
440
462
|
*
|
|
463
|
+
* @throws IndexOutOfBounds: When `index` is negative
|
|
464
|
+
* @throws IndexOutOfBounds: When `index` is greater than or equal to the buffer size
|
|
465
|
+
* @throws IndexOutOfBounds: When `index + 1` is greater than the buffer size
|
|
466
|
+
*
|
|
441
467
|
* @since v0.4.0
|
|
442
468
|
*/
|
|
443
469
|
export let setInt8 = (index, value, buffer) => {
|
|
@@ -464,6 +490,10 @@ export let addInt8 = (value, buffer) => {
|
|
|
464
490
|
* @param buffer: The buffer to access
|
|
465
491
|
* @returns A 32-bit integer representing a signed 16-bit integer that starts at the given index
|
|
466
492
|
*
|
|
493
|
+
* @throws IndexOutOfBounds: When `index` is negative
|
|
494
|
+
* @throws IndexOutOfBounds: When `index` is greater than or equal to the buffer size
|
|
495
|
+
* @throws IndexOutOfBounds: When `index + 2` is greater than the buffer size
|
|
496
|
+
*
|
|
467
497
|
* @since v0.4.0
|
|
468
498
|
*/
|
|
469
499
|
export let getInt16S = (index, buffer) => {
|
|
@@ -478,6 +508,10 @@ export let getInt16S = (index, buffer) => {
|
|
|
478
508
|
* @param buffer: The buffer to access
|
|
479
509
|
* @returns A 32-bit integer representing an unsigned 16-bit integer that starts at the given index
|
|
480
510
|
*
|
|
511
|
+
* @throws IndexOutOfBounds: When `index` is negative
|
|
512
|
+
* @throws IndexOutOfBounds: When `index` is greater than or equal to the buffer size
|
|
513
|
+
* @throws IndexOutOfBounds: When `index + 2` is greater than the buffer size
|
|
514
|
+
*
|
|
481
515
|
* @since v0.4.0
|
|
482
516
|
*/
|
|
483
517
|
export let getInt16U = (index, buffer) => {
|
|
@@ -492,6 +526,10 @@ export let getInt16U = (index, buffer) => {
|
|
|
492
526
|
* @param value: The value to set
|
|
493
527
|
* @param buffer: The buffer to mutate
|
|
494
528
|
*
|
|
529
|
+
* @throws IndexOutOfBounds: When `index` is negative
|
|
530
|
+
* @throws IndexOutOfBounds: When `index` is greater than or equal to the buffer size
|
|
531
|
+
* @throws IndexOutOfBounds: When `index + 2` is greater than the buffer size
|
|
532
|
+
*
|
|
495
533
|
* @since v0.4.0
|
|
496
534
|
*/
|
|
497
535
|
export let setInt16 = (index, value, buffer) => {
|
|
@@ -518,6 +556,10 @@ export let addInt16 = (value, buffer) => {
|
|
|
518
556
|
* @param buffer: The buffer to access
|
|
519
557
|
* @returns A signed 32-bit integer that starts at the given index
|
|
520
558
|
*
|
|
559
|
+
* @throws IndexOutOfBounds: When `index` is negative
|
|
560
|
+
* @throws IndexOutOfBounds: When `index` is greater than or equal to the buffer size
|
|
561
|
+
* @throws IndexOutOfBounds: When `index + 4` is greater than the buffer size
|
|
562
|
+
*
|
|
521
563
|
* @since v0.4.0
|
|
522
564
|
*/
|
|
523
565
|
export let getInt32 = (index, buffer) => {
|
|
@@ -532,6 +574,10 @@ export let getInt32 = (index, buffer) => {
|
|
|
532
574
|
* @param value: The value to set
|
|
533
575
|
* @param buffer: The buffer to mutate
|
|
534
576
|
*
|
|
577
|
+
* @throws IndexOutOfBounds: When `index` is negative
|
|
578
|
+
* @throws IndexOutOfBounds: When `index` is greater than or equal to the buffer size
|
|
579
|
+
* @throws IndexOutOfBounds: When `index + 4` is greater than the buffer size
|
|
580
|
+
*
|
|
535
581
|
* @since v0.4.0
|
|
536
582
|
*/
|
|
537
583
|
export let setInt32 = (index, value, buffer) => {
|
|
@@ -558,6 +604,10 @@ export let addInt32 = (value, buffer) => {
|
|
|
558
604
|
* @param buffer: The buffer to access
|
|
559
605
|
* @returns A 32-bit float that starts at the given index
|
|
560
606
|
*
|
|
607
|
+
* @throws IndexOutOfBounds: When `index` is negative
|
|
608
|
+
* @throws IndexOutOfBounds: When `index` is greater than or equal to the buffer size
|
|
609
|
+
* @throws IndexOutOfBounds: When `index + 4` is greater than the buffer size
|
|
610
|
+
*
|
|
561
611
|
* @since v0.4.0
|
|
562
612
|
*/
|
|
563
613
|
export let getFloat32 = (index, buffer) => {
|
|
@@ -572,6 +622,10 @@ export let getFloat32 = (index, buffer) => {
|
|
|
572
622
|
* @param value: The value to set
|
|
573
623
|
* @param buffer: The buffer to mutate
|
|
574
624
|
*
|
|
625
|
+
* @throws IndexOutOfBounds: When `index` is negative
|
|
626
|
+
* @throws IndexOutOfBounds: When `index` is greater than or equal to the buffer size
|
|
627
|
+
* @throws IndexOutOfBounds: When `index + 4` is greater than the buffer size
|
|
628
|
+
*
|
|
575
629
|
* @since v0.4.0
|
|
576
630
|
*/
|
|
577
631
|
export let setFloat32 = (index, value, buffer) => {
|
|
@@ -601,6 +655,10 @@ export let addFloat32 = (value, buffer) => {
|
|
|
601
655
|
* @param buffer: The buffer to access
|
|
602
656
|
* @returns A signed 64-bit integer that starts at the given index
|
|
603
657
|
*
|
|
658
|
+
* @throws IndexOutOfBounds: When `index` is negative
|
|
659
|
+
* @throws IndexOutOfBounds: When `index` is greater than or equal to the buffer size
|
|
660
|
+
* @throws IndexOutOfBounds: When `index + 8` is greater than the buffer size
|
|
661
|
+
*
|
|
604
662
|
* @since v0.4.0
|
|
605
663
|
*/
|
|
606
664
|
export let getInt64 = (index, buffer) => {
|
|
@@ -615,6 +673,10 @@ export let getInt64 = (index, buffer) => {
|
|
|
615
673
|
* @param value: The value to set
|
|
616
674
|
* @param buffer: The buffer to mutate
|
|
617
675
|
*
|
|
676
|
+
* @throws IndexOutOfBounds: When `index` is negative
|
|
677
|
+
* @throws IndexOutOfBounds: When `index` is greater than or equal to the buffer size
|
|
678
|
+
* @throws IndexOutOfBounds: When `index + 8` is greater than the buffer size
|
|
679
|
+
*
|
|
618
680
|
* @since v0.4.0
|
|
619
681
|
*/
|
|
620
682
|
export let setInt64 = (index, value, buffer) => {
|
|
@@ -644,6 +706,10 @@ export let addInt64 = (value, buffer) => {
|
|
|
644
706
|
* @param buffer: The buffer to access
|
|
645
707
|
* @returns A 64-bit float that starts at the given index
|
|
646
708
|
*
|
|
709
|
+
* @throws IndexOutOfBounds: When `index` is negative
|
|
710
|
+
* @throws IndexOutOfBounds: When `index` is greater than or equal to the buffer size
|
|
711
|
+
* @throws IndexOutOfBounds: When `index + 8` is greater than the buffer size
|
|
712
|
+
*
|
|
647
713
|
* @since v0.4.0
|
|
648
714
|
*/
|
|
649
715
|
export let getFloat64 = (index, buffer) => {
|
|
@@ -658,6 +724,10 @@ export let getFloat64 = (index, buffer) => {
|
|
|
658
724
|
* @param value: The value to set
|
|
659
725
|
* @param buffer: The buffer to mutate
|
|
660
726
|
*
|
|
727
|
+
* @throws IndexOutOfBounds: When `index` is negative
|
|
728
|
+
* @throws IndexOutOfBounds: When `index` is greater than or equal to the buffer size
|
|
729
|
+
* @throws IndexOutOfBounds: When `index + 8` is greater than the buffer size
|
|
730
|
+
*
|
|
661
731
|
* @since v0.4.0
|
|
662
732
|
*/
|
|
663
733
|
export let setFloat64 = (index, value, buffer) => {
|