@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/range.md
CHANGED
|
@@ -2,45 +2,178 @@
|
|
|
2
2
|
title: Range
|
|
3
3
|
---
|
|
4
4
|
|
|
5
|
-
Utilities for working with ranges.
|
|
5
|
+
Utilities for working with ranges.
|
|
6
|
+
|
|
7
|
+
A range represents an interval—a set of values with a beginning and an end.
|
|
8
|
+
|
|
9
|
+
All functions in this module treat ranges as exclusive, but inclusive versions
|
|
10
|
+
of all APIs are available in the `Inclusive` submodule.
|
|
11
|
+
|
|
12
|
+
<details>
|
|
13
|
+
<summary>Added in <code>0.3.0</code></summary>
|
|
14
|
+
<table>
|
|
15
|
+
<thead>
|
|
16
|
+
<tr><th>version</th><th>changes</th></tr>
|
|
17
|
+
</thead>
|
|
18
|
+
<tbody>
|
|
19
|
+
<tr><td><code>0.6.0</code></td><td>Treats all ranges as exclusive</td></tr>
|
|
20
|
+
</tbody>
|
|
21
|
+
</table>
|
|
22
|
+
</details>
|
|
23
|
+
|
|
24
|
+
```grain
|
|
25
|
+
from "range" include Range
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Values
|
|
29
|
+
|
|
30
|
+
Functions and constants included in the Range module.
|
|
31
|
+
|
|
32
|
+
### Range.**inRange**
|
|
6
33
|
|
|
7
|
-
<details
|
|
8
|
-
<summary
|
|
9
|
-
|
|
34
|
+
<details>
|
|
35
|
+
<summary>Added in <code>0.3.0</code></summary>
|
|
36
|
+
<table>
|
|
37
|
+
<thead>
|
|
38
|
+
<tr><th>version</th><th>changes</th></tr>
|
|
39
|
+
</thead>
|
|
40
|
+
<tbody>
|
|
41
|
+
<tr><td><code>0.6.0</code></td><td>Treats all ranges as exclusive</td></tr>
|
|
42
|
+
</tbody>
|
|
43
|
+
</table>
|
|
10
44
|
</details>
|
|
11
45
|
|
|
12
46
|
```grain
|
|
13
|
-
|
|
47
|
+
inRange : (value: Number, range: Range<Number>) => Bool
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Checks if the given number is within the range.
|
|
51
|
+
|
|
52
|
+
Parameters:
|
|
53
|
+
|
|
54
|
+
|param|type|description|
|
|
55
|
+
|-----|----|-----------|
|
|
56
|
+
|`value`|`Number`|The number being checked|
|
|
57
|
+
|`range`|`Range<Number>`|The range to check within|
|
|
58
|
+
|
|
59
|
+
Returns:
|
|
60
|
+
|
|
61
|
+
|type|description|
|
|
62
|
+
|----|-----------|
|
|
63
|
+
|`Bool`|Whether or not the value is within range|
|
|
64
|
+
|
|
65
|
+
Examples:
|
|
66
|
+
|
|
67
|
+
```grain
|
|
68
|
+
Range.inRange(1, { rangeStart: 0, rangeEnd: 2 }) == true
|
|
14
69
|
```
|
|
15
70
|
|
|
16
|
-
|
|
71
|
+
```grain
|
|
72
|
+
Range.inRange(10, { rangeStart: 0, rangeEnd: 2 }) == false
|
|
73
|
+
```
|
|
17
74
|
|
|
18
|
-
|
|
75
|
+
### Range.**forEach**
|
|
19
76
|
|
|
20
|
-
|
|
77
|
+
<details>
|
|
78
|
+
<summary>Added in <code>0.3.0</code></summary>
|
|
79
|
+
<table>
|
|
80
|
+
<thead>
|
|
81
|
+
<tr><th>version</th><th>changes</th></tr>
|
|
82
|
+
</thead>
|
|
83
|
+
<tbody>
|
|
84
|
+
<tr><td><code>0.6.0</code></td><td>Treats all ranges as exclusive</td></tr>
|
|
85
|
+
</tbody>
|
|
86
|
+
</table>
|
|
87
|
+
</details>
|
|
21
88
|
|
|
22
89
|
```grain
|
|
23
|
-
|
|
24
|
-
Inclusive(Number, Number),
|
|
25
|
-
Exclusive(Number, Number),
|
|
26
|
-
}
|
|
90
|
+
forEach : (fn: (Number => Void), range: Range<Number>) => Void
|
|
27
91
|
```
|
|
28
92
|
|
|
29
|
-
|
|
93
|
+
Calls the given function with each number in the range.
|
|
30
94
|
|
|
31
|
-
|
|
95
|
+
For increasing ranges, the value is increased by `1` in each iteration,
|
|
96
|
+
and for decreasing ranges, the value is decreased by `1`. The value is
|
|
97
|
+
always changed by `1`, even if non-integer values were provided in the range.
|
|
32
98
|
|
|
33
|
-
|
|
99
|
+
Parameters:
|
|
34
100
|
|
|
35
|
-
|
|
101
|
+
|param|type|description|
|
|
102
|
+
|-----|----|-----------|
|
|
103
|
+
|`fn`|`Number => Void`|The function to be executed on each number in the range|
|
|
104
|
+
|`range`|`Range<Number>`|The range to iterate|
|
|
36
105
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
106
|
+
Examples:
|
|
107
|
+
|
|
108
|
+
```grain
|
|
109
|
+
Range.forEach(val => print(val), { rangeStart: 0, rangeEnd: 2 })
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### Range.**map**
|
|
113
|
+
|
|
114
|
+
<details>
|
|
115
|
+
<summary>Added in <code>0.3.2</code></summary>
|
|
116
|
+
<table>
|
|
117
|
+
<thead>
|
|
118
|
+
<tr><th>version</th><th>changes</th></tr>
|
|
119
|
+
</thead>
|
|
120
|
+
<tbody>
|
|
121
|
+
<tr><td><code>0.6.0</code></td><td>Treats all ranges as exclusive</td></tr>
|
|
122
|
+
</tbody>
|
|
123
|
+
</table>
|
|
40
124
|
</details>
|
|
41
125
|
|
|
42
126
|
```grain
|
|
43
|
-
|
|
127
|
+
map : (fn: (Number => a), range: Range<Number>) => List<a>
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
Produces a list by calling the given function on each number included in the range.
|
|
131
|
+
|
|
132
|
+
For increasing ranges, the value is increased by `1` in each iteration,
|
|
133
|
+
and for decreasing ranges, the value is decreased by `1`. The value is
|
|
134
|
+
always changed by `1`, even if non-integer values were provided in the range.
|
|
135
|
+
|
|
136
|
+
Parameters:
|
|
137
|
+
|
|
138
|
+
|param|type|description|
|
|
139
|
+
|-----|----|-----------|
|
|
140
|
+
|`fn`|`Number => a`|The function called on each number in the range that returns the value for the output list|
|
|
141
|
+
|`range`|`Range<Number>`|The range to iterate|
|
|
142
|
+
|
|
143
|
+
Returns:
|
|
144
|
+
|
|
145
|
+
|type|description|
|
|
146
|
+
|----|-----------|
|
|
147
|
+
|`List<a>`|A list containing all values returned from the `fn`|
|
|
148
|
+
|
|
149
|
+
Examples:
|
|
150
|
+
|
|
151
|
+
```grain
|
|
152
|
+
Range.map(val => val * 2, { rangeStart: 0, rangeEnd: 3 }) == [0, 2, 4]
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
## Range.Inclusive
|
|
156
|
+
|
|
157
|
+
### Values
|
|
158
|
+
|
|
159
|
+
Functions and constants included in the Range.Inclusive module.
|
|
160
|
+
|
|
161
|
+
#### Range.Inclusive.**inRange**
|
|
162
|
+
|
|
163
|
+
<details>
|
|
164
|
+
<summary>Added in <code>0.6.0</code></summary>
|
|
165
|
+
<table>
|
|
166
|
+
<thead>
|
|
167
|
+
<tr><th>version</th><th>changes</th></tr>
|
|
168
|
+
</thead>
|
|
169
|
+
<tbody>
|
|
170
|
+
<tr><td><code>0.3.0</code></td><td>Root APIs originally handled Inclusive & Exclusive variants</td></tr>
|
|
171
|
+
</tbody>
|
|
172
|
+
</table>
|
|
173
|
+
</details>
|
|
174
|
+
|
|
175
|
+
```grain
|
|
176
|
+
inRange : (value: Number, range: Range<Number>) => Bool
|
|
44
177
|
```
|
|
45
178
|
|
|
46
179
|
Checks if the given number is within the range.
|
|
@@ -50,7 +183,7 @@ Parameters:
|
|
|
50
183
|
|param|type|description|
|
|
51
184
|
|-----|----|-----------|
|
|
52
185
|
|`value`|`Number`|The number being checked|
|
|
53
|
-
|`range`|`Range
|
|
186
|
+
|`range`|`Range<Number>`|The range to check within|
|
|
54
187
|
|
|
55
188
|
Returns:
|
|
56
189
|
|
|
@@ -61,58 +194,80 @@ Returns:
|
|
|
61
194
|
Examples:
|
|
62
195
|
|
|
63
196
|
```grain
|
|
64
|
-
Range.inRange(1,
|
|
197
|
+
Range.Inclusive.inRange(1, { rangeStart: 0, rangeEnd: 1 }) == true
|
|
65
198
|
```
|
|
66
199
|
|
|
67
200
|
```grain
|
|
68
|
-
Range.inRange(10,
|
|
201
|
+
Range.Inclusive.inRange(10, { rangeStart: 0, rangeEnd: 2 }) == false
|
|
69
202
|
```
|
|
70
203
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
<details
|
|
74
|
-
<summary
|
|
75
|
-
|
|
204
|
+
#### Range.Inclusive.**forEach**
|
|
205
|
+
|
|
206
|
+
<details>
|
|
207
|
+
<summary>Added in <code>0.3.0</code></summary>
|
|
208
|
+
<table>
|
|
209
|
+
<thead>
|
|
210
|
+
<tr><th>version</th><th>changes</th></tr>
|
|
211
|
+
</thead>
|
|
212
|
+
<tbody>
|
|
213
|
+
<tr><td><code>0.3.0</code></td><td>Root APIs originally handled Inclusive & Exclusive variants</td></tr>
|
|
214
|
+
</tbody>
|
|
215
|
+
</table>
|
|
76
216
|
</details>
|
|
77
217
|
|
|
78
218
|
```grain
|
|
79
|
-
forEach : ((Number
|
|
219
|
+
forEach : (fn: (Number => Void), range: Range<Number>) => Void
|
|
80
220
|
```
|
|
81
221
|
|
|
82
|
-
Calls the given function with each number in the range.
|
|
222
|
+
Calls the given function with each number in the range.
|
|
223
|
+
|
|
224
|
+
For increasing ranges, the value is increased by `1` in each iteration,
|
|
225
|
+
and for decreasing ranges, the value is decreased by `1`. The value is
|
|
226
|
+
always changed by `1`, even if non-integer values were provided in the range.
|
|
83
227
|
|
|
84
228
|
Parameters:
|
|
85
229
|
|
|
86
230
|
|param|type|description|
|
|
87
231
|
|-----|----|-----------|
|
|
88
|
-
|`fn`|`Number
|
|
89
|
-
|`range`|`Range
|
|
232
|
+
|`fn`|`Number => Void`|The function to be executed on each number in the range|
|
|
233
|
+
|`range`|`Range<Number>`|The range to iterate|
|
|
90
234
|
|
|
91
235
|
Examples:
|
|
92
236
|
|
|
93
237
|
```grain
|
|
94
|
-
Range.forEach(val => print(val),
|
|
238
|
+
Range.Inclusive.forEach(val => print(val), { rangeStart: 0, rangeEnd: 2 })
|
|
95
239
|
```
|
|
96
240
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
<details
|
|
100
|
-
<summary
|
|
101
|
-
|
|
241
|
+
#### Range.Inclusive.**map**
|
|
242
|
+
|
|
243
|
+
<details>
|
|
244
|
+
<summary>Added in <code>0.3.2</code></summary>
|
|
245
|
+
<table>
|
|
246
|
+
<thead>
|
|
247
|
+
<tr><th>version</th><th>changes</th></tr>
|
|
248
|
+
</thead>
|
|
249
|
+
<tbody>
|
|
250
|
+
<tr><td><code>0.3.0</code></td><td>Root APIs originally handled Inclusive & Exclusive variants</td></tr>
|
|
251
|
+
</tbody>
|
|
252
|
+
</table>
|
|
102
253
|
</details>
|
|
103
254
|
|
|
104
255
|
```grain
|
|
105
|
-
map : ((Number
|
|
256
|
+
map : (fn: (Number => a), range: Range<Number>) => List<a>
|
|
106
257
|
```
|
|
107
258
|
|
|
108
|
-
Produces a list by calling the given function on each number included in the range.
|
|
259
|
+
Produces a list by calling the given function on each number included in the range.
|
|
260
|
+
|
|
261
|
+
For increasing ranges, the value is increased by `1` in each iteration,
|
|
262
|
+
and for decreasing ranges, the value is decreased by `1`. The value is
|
|
263
|
+
always changed by `1`, even if non-integer values were provided in the range.
|
|
109
264
|
|
|
110
265
|
Parameters:
|
|
111
266
|
|
|
112
267
|
|param|type|description|
|
|
113
268
|
|-----|----|-----------|
|
|
114
|
-
|`fn`|`Number
|
|
115
|
-
|`range`|`Range
|
|
269
|
+
|`fn`|`Number => a`|The function called on each number in the range that returns the value for the output list|
|
|
270
|
+
|`range`|`Range<Number>`|The range to iterate|
|
|
116
271
|
|
|
117
272
|
Returns:
|
|
118
273
|
|
|
@@ -123,6 +278,6 @@ Returns:
|
|
|
123
278
|
Examples:
|
|
124
279
|
|
|
125
280
|
```grain
|
|
126
|
-
Range.map(val => val * 2,
|
|
281
|
+
Range.Inclusive.map(val => val * 2, { rangeStart: 0, rangeEnd: 2 }) == [0, 2, 4]
|
|
127
282
|
```
|
|
128
283
|
|
package/rational.gr
ADDED
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utilities for working with the Rational type.
|
|
3
|
+
* @example from "rational" include Rational
|
|
4
|
+
*
|
|
5
|
+
* @example 1/2r
|
|
6
|
+
* @example 3/4r
|
|
7
|
+
*
|
|
8
|
+
* @since v0.6.0
|
|
9
|
+
*/
|
|
10
|
+
module Rational
|
|
11
|
+
|
|
12
|
+
from "runtime/unsafe/wasmi32" include WasmI32
|
|
13
|
+
from "runtime/exception" include Exception
|
|
14
|
+
from "runtime/numbers" include Numbers
|
|
15
|
+
use Numbers.{
|
|
16
|
+
coerceNumberToRational as fromNumber,
|
|
17
|
+
coerceRationalToNumber as toNumber,
|
|
18
|
+
rationalNumerator as numerator,
|
|
19
|
+
rationalDenominator as denominator,
|
|
20
|
+
addSubRational,
|
|
21
|
+
timesDivideRational,
|
|
22
|
+
rationalsEqual,
|
|
23
|
+
cmpRationals,
|
|
24
|
+
isInteger,
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
provide { fromNumber, toNumber, numerator, denominator }
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Gets the numerator and denominator of the rational.
|
|
31
|
+
*
|
|
32
|
+
* @param x: The rational to split
|
|
33
|
+
* @returns The numerator and denominator of the rational
|
|
34
|
+
*
|
|
35
|
+
* @example Rational.toIntegerRatio(1/2r) == (1, 2)
|
|
36
|
+
* @example Rational.toIntegerRatio(2/8r) == (1, 4)
|
|
37
|
+
*
|
|
38
|
+
* @since v0.6.0
|
|
39
|
+
*/
|
|
40
|
+
provide let toIntegerRatio = x => {
|
|
41
|
+
(numerator(x), denominator(x))
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Creates a rational from a numerator and denominator.
|
|
46
|
+
*
|
|
47
|
+
* @param numerator: The numerator
|
|
48
|
+
* @param denominator: The denominator
|
|
49
|
+
* @returns The reduced rational
|
|
50
|
+
*
|
|
51
|
+
* @throws InvalidArgument(String): If the numerator is not an integer
|
|
52
|
+
* @throws InvalidArgument(String): If the denominator is not an integer
|
|
53
|
+
*
|
|
54
|
+
* @example Rational.fromIntegerRatio(1, 2) == 1/2r
|
|
55
|
+
* @example Rational.fromIntegerRatio(2, 8) == 1/4r
|
|
56
|
+
*
|
|
57
|
+
* @since v0.6.0
|
|
58
|
+
*/
|
|
59
|
+
@unsafe
|
|
60
|
+
provide let fromIntegerRatio = (numerator: Number, denominator: Number) => {
|
|
61
|
+
let numeratorptr = WasmI32.fromGrain(numerator)
|
|
62
|
+
let denominatorptr = WasmI32.fromGrain(denominator)
|
|
63
|
+
if (!isInteger(numeratorptr)) {
|
|
64
|
+
throw Exception.InvalidArgument("Numerator must be an integer")
|
|
65
|
+
}
|
|
66
|
+
if (!isInteger(denominatorptr)) {
|
|
67
|
+
throw Exception.InvalidArgument("Denominator must be an integer")
|
|
68
|
+
}
|
|
69
|
+
fromNumber(numerator / denominator)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Computes the sum of its operands.
|
|
74
|
+
*
|
|
75
|
+
* @param x: The first operand
|
|
76
|
+
* @param y: The second operand
|
|
77
|
+
* @returns The sum of the two operands
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* use Rational.{ (+) }
|
|
81
|
+
* assert 1/2r + 1/4r == 3/4r
|
|
82
|
+
*
|
|
83
|
+
* @since v0.6.0
|
|
84
|
+
*/
|
|
85
|
+
@unsafe
|
|
86
|
+
provide let (+) = (x: Rational, y: Rational) => {
|
|
87
|
+
let xptr = WasmI32.fromGrain(x)
|
|
88
|
+
let yptr = WasmI32.fromGrain(y)
|
|
89
|
+
let ptr = addSubRational(xptr, yptr, false, true)
|
|
90
|
+
WasmI32.toGrain(ptr): Rational
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Computes the difference of its operands.
|
|
95
|
+
*
|
|
96
|
+
* @param x: The first operand
|
|
97
|
+
* @param y: The second operand
|
|
98
|
+
* @returns The difference of the two operands
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* use Rational.{ (-) }
|
|
102
|
+
* assert 1/2r - 1/4r == 1/4r
|
|
103
|
+
*
|
|
104
|
+
* @since v0.6.0
|
|
105
|
+
*/
|
|
106
|
+
@unsafe
|
|
107
|
+
provide let (-) = (x: Rational, y: Rational) => {
|
|
108
|
+
let xptr = WasmI32.fromGrain(x)
|
|
109
|
+
let yptr = WasmI32.fromGrain(y)
|
|
110
|
+
let ptr = addSubRational(xptr, yptr, true, true)
|
|
111
|
+
WasmI32.toGrain(ptr): Rational
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Computes the product of its operands.
|
|
116
|
+
*
|
|
117
|
+
* @param x: The first operand
|
|
118
|
+
* @param y: The second operand
|
|
119
|
+
* @returns The product of the two operands
|
|
120
|
+
*
|
|
121
|
+
* @example
|
|
122
|
+
* use Rational.{ (*) }
|
|
123
|
+
* assert 1/2r * 1/4r == 1/8r
|
|
124
|
+
*
|
|
125
|
+
* @since v0.6.0
|
|
126
|
+
*/
|
|
127
|
+
@unsafe
|
|
128
|
+
provide let (*) = (x: Rational, y: Rational) => {
|
|
129
|
+
let xptr = WasmI32.fromGrain(x)
|
|
130
|
+
let yptr = WasmI32.fromGrain(y)
|
|
131
|
+
let ptr = timesDivideRational(xptr, yptr, false, true)
|
|
132
|
+
WasmI32.toGrain(ptr): Rational
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Computes the quotient of its operands.
|
|
137
|
+
*
|
|
138
|
+
* @param x: The first operand
|
|
139
|
+
* @param y: The second operand
|
|
140
|
+
* @returns The quotient of the two operands
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* use Rational.{ (/) }
|
|
144
|
+
* assert 1/2r / 1/4r == 2/1r
|
|
145
|
+
*
|
|
146
|
+
* @since v0.6.0
|
|
147
|
+
*/
|
|
148
|
+
@unsafe
|
|
149
|
+
provide let (/) = (x: Rational, y: Rational) => {
|
|
150
|
+
let xptr = WasmI32.fromGrain(x)
|
|
151
|
+
let yptr = WasmI32.fromGrain(y)
|
|
152
|
+
let ptr = timesDivideRational(xptr, yptr, true, true)
|
|
153
|
+
WasmI32.toGrain(ptr): Rational
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Checks if the first value is equal to the second value.
|
|
158
|
+
*
|
|
159
|
+
* @param x: The first value
|
|
160
|
+
* @param y: The second value
|
|
161
|
+
* @returns `true` if the first value is equal to the second value or `false` otherwise
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* use Rational.{ (==) }
|
|
165
|
+
* assert 1/2r == 1/2r
|
|
166
|
+
*
|
|
167
|
+
* @since v0.6.0
|
|
168
|
+
*/
|
|
169
|
+
@unsafe
|
|
170
|
+
provide let (==) = (x: Rational, y: Rational) => {
|
|
171
|
+
let x = WasmI32.fromGrain(x)
|
|
172
|
+
let y = WasmI32.fromGrain(y)
|
|
173
|
+
rationalsEqual(x, y)
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Checks if the first value is not equal to the second value.
|
|
178
|
+
*
|
|
179
|
+
* @param x: The first value
|
|
180
|
+
* @param y: The second value
|
|
181
|
+
* @returns `true` if the first value is not equal to the second value or `false` otherwise
|
|
182
|
+
*
|
|
183
|
+
* @example
|
|
184
|
+
* use Rational.{ (!=) }
|
|
185
|
+
* assert 1/2r != 1/4r
|
|
186
|
+
*
|
|
187
|
+
* @since v0.6.0
|
|
188
|
+
*/
|
|
189
|
+
@unsafe
|
|
190
|
+
provide let (!=) = (x: Rational, y: Rational) => {
|
|
191
|
+
let x = WasmI32.fromGrain(x)
|
|
192
|
+
let y = WasmI32.fromGrain(y)
|
|
193
|
+
!rationalsEqual(x, y)
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Checks if the first value is less than the second value.
|
|
198
|
+
*
|
|
199
|
+
* @param x: The first value
|
|
200
|
+
* @param y: The second value
|
|
201
|
+
* @returns `true` if the first value is less than the second value or `false` otherwise
|
|
202
|
+
*
|
|
203
|
+
* @example
|
|
204
|
+
* use Rational.{ (<) }
|
|
205
|
+
* assert 1/4r < 1/2r
|
|
206
|
+
*
|
|
207
|
+
* @since v0.6.0
|
|
208
|
+
*/
|
|
209
|
+
@unsafe
|
|
210
|
+
provide let (<) = (x: Rational, y: Rational) => {
|
|
211
|
+
use WasmI32.{ (<) }
|
|
212
|
+
let x = WasmI32.fromGrain(x)
|
|
213
|
+
let y = WasmI32.fromGrain(y)
|
|
214
|
+
cmpRationals(x, y) < 0n
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Checks if the first value is greater than the second value.
|
|
219
|
+
*
|
|
220
|
+
* @param x: The first value
|
|
221
|
+
* @param y: The second value
|
|
222
|
+
* @returns `true` if the first value is greater than the second value or `false` otherwise
|
|
223
|
+
*
|
|
224
|
+
* @example
|
|
225
|
+
* use Rational.{ (>) }
|
|
226
|
+
* assert 1/2r > 1/4r
|
|
227
|
+
*
|
|
228
|
+
* @since v0.6.0
|
|
229
|
+
*/
|
|
230
|
+
@unsafe
|
|
231
|
+
provide let (>) = (x: Rational, y: Rational) => {
|
|
232
|
+
use WasmI32.{ (>) }
|
|
233
|
+
let x = WasmI32.fromGrain(x)
|
|
234
|
+
let y = WasmI32.fromGrain(y)
|
|
235
|
+
cmpRationals(x, y) > 0n
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Checks if the first value is less than or equal to the second value.
|
|
240
|
+
*
|
|
241
|
+
* @param x: The first value
|
|
242
|
+
* @param y: The second value
|
|
243
|
+
* @returns `true` if the first value is less than or equal to the second value or `false` otherwise
|
|
244
|
+
*
|
|
245
|
+
* @example
|
|
246
|
+
* use Rational.{ (<=) }
|
|
247
|
+
* assert 1/4r <= 1/2r
|
|
248
|
+
* @example
|
|
249
|
+
* use Rational.{ (<=) }
|
|
250
|
+
* assert 1/2r <= 1/2r
|
|
251
|
+
*
|
|
252
|
+
* @since v0.6.0
|
|
253
|
+
*/
|
|
254
|
+
@unsafe
|
|
255
|
+
provide let (<=) = (x: Rational, y: Rational) => {
|
|
256
|
+
use WasmI32.{ (<=) }
|
|
257
|
+
let x = WasmI32.fromGrain(x)
|
|
258
|
+
let y = WasmI32.fromGrain(y)
|
|
259
|
+
cmpRationals(x, y) <= 0n
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Checks if the first value is greater than or equal to the second value.
|
|
264
|
+
*
|
|
265
|
+
* @param x: The first value
|
|
266
|
+
* @param y: The second value
|
|
267
|
+
* @returns `true` if the first value is greater than or equal to the second value or `false` otherwise
|
|
268
|
+
*
|
|
269
|
+
* @example
|
|
270
|
+
* use Rational.{ (>=) }
|
|
271
|
+
* assert 1/2r >= 1/4r
|
|
272
|
+
* @example
|
|
273
|
+
* use Rational.{ (>=) }
|
|
274
|
+
* assert 1/2r >= 1/2r
|
|
275
|
+
*
|
|
276
|
+
* @since v0.6.0
|
|
277
|
+
*/
|
|
278
|
+
@unsafe
|
|
279
|
+
provide let (>=) = (x: Rational, y: Rational) => {
|
|
280
|
+
use WasmI32.{ (>=) }
|
|
281
|
+
let x = WasmI32.fromGrain(x)
|
|
282
|
+
let y = WasmI32.fromGrain(y)
|
|
283
|
+
cmpRationals(x, y) >= 0n
|
|
284
|
+
}
|