@grain/stdlib 0.4.4 → 0.5.0
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 +87 -0
- package/LICENSE +1 -1
- package/array.gr +92 -73
- package/array.md +18 -18
- package/bigint.gr +497 -0
- package/bigint.md +811 -0
- package/buffer.gr +56 -217
- package/buffer.md +24 -17
- package/bytes.gr +103 -205
- package/bytes.md +19 -0
- package/char.gr +152 -166
- package/char.md +200 -0
- package/exception.md +6 -0
- package/float32.gr +159 -82
- package/float32.md +315 -0
- package/float64.gr +163 -82
- package/float64.md +315 -0
- package/hash.gr +53 -49
- package/int32.gr +479 -230
- package/int32.md +937 -0
- package/int64.gr +479 -230
- package/int64.md +937 -0
- package/list.gr +530 -116
- package/list.md +1141 -0
- package/map.gr +302 -121
- package/map.md +525 -0
- package/number.gr +51 -57
- package/number.md +37 -3
- package/option.gr +25 -25
- package/option.md +1 -1
- package/package.json +3 -3
- package/pervasives.gr +504 -52
- package/pervasives.md +1116 -0
- package/queue.gr +8 -1
- package/queue.md +10 -0
- package/random.gr +196 -0
- package/random.md +179 -0
- package/range.gr +26 -26
- package/regex.gr +1833 -842
- package/regex.md +11 -11
- package/result.md +1 -1
- package/runtime/bigint.gr +2045 -0
- package/runtime/bigint.md +326 -0
- package/runtime/dataStructures.gr +99 -279
- package/runtime/dataStructures.md +391 -0
- package/runtime/debug.gr +0 -1
- package/runtime/debug.md +6 -0
- package/runtime/equal.gr +40 -37
- package/runtime/equal.md +6 -0
- package/runtime/exception.gr +28 -15
- package/runtime/exception.md +30 -0
- package/runtime/gc.gr +50 -20
- package/runtime/gc.md +36 -0
- package/runtime/malloc.gr +32 -22
- package/runtime/malloc.md +55 -0
- package/runtime/numberUtils.gr +297 -142
- package/runtime/numberUtils.md +54 -0
- package/runtime/numbers.gr +1204 -453
- package/runtime/numbers.md +300 -0
- package/runtime/string.gr +193 -228
- package/runtime/string.md +24 -0
- package/runtime/stringUtils.gr +62 -38
- package/runtime/stringUtils.md +6 -0
- package/runtime/unsafe/constants.gr +17 -0
- package/runtime/unsafe/constants.md +72 -0
- package/runtime/unsafe/conv.gr +10 -10
- package/runtime/unsafe/conv.md +71 -0
- package/runtime/unsafe/errors.md +204 -0
- package/runtime/unsafe/memory.gr +14 -3
- package/runtime/unsafe/memory.md +54 -0
- package/runtime/unsafe/printWasm.gr +4 -4
- package/runtime/unsafe/printWasm.md +24 -0
- package/runtime/unsafe/tags.gr +11 -10
- package/runtime/unsafe/tags.md +120 -0
- package/runtime/unsafe/wasmf32.gr +9 -2
- package/runtime/unsafe/wasmf32.md +168 -0
- package/runtime/unsafe/wasmf64.gr +9 -2
- package/runtime/unsafe/wasmf64.md +168 -0
- package/runtime/unsafe/wasmi32.gr +65 -47
- package/runtime/unsafe/wasmi32.md +282 -0
- package/runtime/unsafe/wasmi64.gr +78 -50
- package/runtime/unsafe/wasmi64.md +300 -0
- package/runtime/utils/printing.gr +62 -0
- package/runtime/utils/printing.md +18 -0
- package/runtime/wasi.gr +200 -46
- package/runtime/wasi.md +839 -0
- package/set.gr +125 -121
- package/set.md +24 -21
- package/stack.gr +29 -29
- package/stack.md +4 -6
- package/string.gr +434 -415
- package/string.md +3 -3
- package/sys/file.gr +477 -482
- package/sys/process.gr +33 -47
- package/sys/random.gr +48 -20
- package/sys/random.md +38 -0
- package/sys/time.gr +12 -28
package/pervasives.gr
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @module Pervasives: This module is automatically imported into every Grain program. You can think of it as the global environment. Although it is automatically imported, it can still be imported manually.
|
|
3
|
+
*
|
|
4
|
+
* @example import Pervasives from "pervasives"
|
|
5
|
+
*
|
|
6
|
+
* @since v0.1.0
|
|
7
|
+
*/
|
|
2
8
|
|
|
3
9
|
import Exception from "runtime/exception"
|
|
4
10
|
import Memory from "runtime/unsafe/memory"
|
|
@@ -24,35 +30,383 @@ import {
|
|
|
24
30
|
(^),
|
|
25
31
|
(<<),
|
|
26
32
|
(>>>),
|
|
27
|
-
(>>)
|
|
33
|
+
(>>),
|
|
28
34
|
} from "runtime/numbers"
|
|
35
|
+
import { toString, print, concat as (++) } from "runtime/string"
|
|
29
36
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
37
|
+
/**
|
|
38
|
+
* @section Types: Type declarations included in the Pervasives module.
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The type of Grain lists.
|
|
43
|
+
*
|
|
44
|
+
* @example [1, 2, 3]
|
|
45
|
+
* @example []
|
|
46
|
+
*
|
|
47
|
+
* @since v0.1.0
|
|
48
|
+
*/
|
|
49
|
+
export enum List<a> {
|
|
50
|
+
[],
|
|
51
|
+
[...](a, List<a>),
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Grain's type representing something that may or may not contain data.
|
|
56
|
+
* Think of this like a better, type-safe "null".
|
|
57
|
+
*
|
|
58
|
+
* @since v0.2.0
|
|
59
|
+
*/
|
|
60
|
+
export enum Option<a> {
|
|
61
|
+
Some(a),
|
|
62
|
+
None,
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Grain's type representing the result of something that might error.
|
|
67
|
+
*
|
|
68
|
+
* @since v0.2.0
|
|
69
|
+
*/
|
|
70
|
+
export enum Result<t, e> {
|
|
71
|
+
Ok(t),
|
|
72
|
+
Err(e),
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* @section Boolean operations: Infix functions for working with Boolean values.
|
|
77
|
+
*/
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Computes the logical NOT (`!`) of the given operand.
|
|
81
|
+
* Inverts the given Boolean value.
|
|
82
|
+
*
|
|
83
|
+
* @param value: The operand
|
|
84
|
+
* @returns The inverted value
|
|
85
|
+
*
|
|
86
|
+
* @example !true // false
|
|
87
|
+
* @example !false // true
|
|
88
|
+
*
|
|
89
|
+
* @since v0.1.0
|
|
90
|
+
*/
|
|
91
|
+
export primitive (!): Bool -> Bool = "@not"
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Computes the logical AND (`&&`) of the given operands.
|
|
95
|
+
*
|
|
96
|
+
* If the first operand is `false`, returns `false` without evaluating the second operand.
|
|
97
|
+
* If the first operand is `true`, returns the value of the second operand.
|
|
98
|
+
*
|
|
99
|
+
* @param value1: The first operand
|
|
100
|
+
* @param value2: The second operand
|
|
101
|
+
* @returns The first operand if it is `false` or the value of the second operand otherwise
|
|
102
|
+
*
|
|
103
|
+
* @since v0.1.0
|
|
104
|
+
*/
|
|
105
|
+
export primitive (&&): (Bool, Bool) -> Bool = "@and"
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Computes the logical OR `||` of the given operands.
|
|
109
|
+
*
|
|
110
|
+
* If the first operand is `true`, returns `true` without evaluating the second operand.
|
|
111
|
+
* If the first operand is `false`, returns the value of the second operand.
|
|
112
|
+
*
|
|
113
|
+
* @param value1: The first operand
|
|
114
|
+
* @param value2: The second operand
|
|
115
|
+
* @returns The first operand if it is `true` or the value of the second operand otherwise
|
|
116
|
+
*
|
|
117
|
+
* @since v0.1.0
|
|
118
|
+
*/
|
|
119
|
+
export primitive (||): (Bool, Bool) -> Bool = "@or"
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* @section Comparison operations: Infix functions for comparing values.
|
|
123
|
+
*/
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Check that two values are equal. This checks for structural equality,
|
|
127
|
+
* so it also works for comparing things like tuples and lists.
|
|
128
|
+
*
|
|
129
|
+
* @param value1: The first operand
|
|
130
|
+
* @param value2: The second operand
|
|
131
|
+
* @returns `true` if the values are structurally equal or `false` otherwise
|
|
132
|
+
*
|
|
133
|
+
* @since v0.1.0
|
|
134
|
+
*/
|
|
135
|
+
export (==)
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Check that two values are **not** equal. This checks for structural equality,
|
|
139
|
+
* so it also works for comparing things like tuples and lists.
|
|
140
|
+
*
|
|
141
|
+
* @param value1: The first operand
|
|
142
|
+
* @param value2: The second operand
|
|
143
|
+
* @returns `false` if the values are structurally equal or `true` otherwise
|
|
144
|
+
*
|
|
145
|
+
* @since v0.2.0
|
|
146
|
+
*/
|
|
147
|
+
export let (!=): (a, a) -> Bool = (x, y) => !(x == y)
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Checks that two values are physically equal.
|
|
151
|
+
* Use this operator if you don’t need or want structural equality.
|
|
152
|
+
*
|
|
153
|
+
* @param value1: The first operand
|
|
154
|
+
* @param value2: The second operand
|
|
155
|
+
* @returns `true` if the values are physically equal or `false` otherwise
|
|
156
|
+
*
|
|
157
|
+
* @since v0.1.0
|
|
158
|
+
*/
|
|
159
|
+
export primitive (is): (a, a) -> Bool = "@is"
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Checks that two values are **not** physically equal.
|
|
163
|
+
* Use this operator if you don’t need or want structural equality.
|
|
164
|
+
*
|
|
165
|
+
* @param value1: The first operand
|
|
166
|
+
* @param value2: The second operand
|
|
167
|
+
* @returns `false` if the values are physically equal or `true` otherwise
|
|
168
|
+
*
|
|
169
|
+
* @since v0.2.0
|
|
170
|
+
*/
|
|
171
|
+
export let (isnt): (a, a) -> Bool = (x, y) => !(x is y)
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* @section Number comparisons: Infix functions for comparing Number values.
|
|
175
|
+
*/
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Checks if the first operand is less than the second operand.
|
|
179
|
+
*
|
|
180
|
+
* @param num1: The first operand
|
|
181
|
+
* @param num2: The second operand
|
|
182
|
+
* @returns `true` if the first operand is less than the second operand or `false` otherwise
|
|
183
|
+
*
|
|
184
|
+
* @since v0.1.0
|
|
185
|
+
*/
|
|
186
|
+
export (<)
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Checks if the first operand is greater than the second operand.
|
|
190
|
+
*
|
|
191
|
+
* @param num1: The first operand
|
|
192
|
+
* @param num2: The second operand
|
|
193
|
+
* @returns `true` if the first operand is greater than the second operand or `false` otherwise
|
|
194
|
+
*
|
|
195
|
+
* @since v0.1.0
|
|
196
|
+
*/
|
|
197
|
+
export (>)
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Checks if the first operand is less than or equal to the second operand.
|
|
201
|
+
*
|
|
202
|
+
* @param num1: The first operand
|
|
203
|
+
* @param num2: The second operand
|
|
204
|
+
* @returns `true` if the first operand is less than or equal to the second operand or `false` otherwise
|
|
205
|
+
*
|
|
206
|
+
* @since v0.1.0
|
|
207
|
+
*/
|
|
208
|
+
export (<=)
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Checks if the first operand is greater than or equal to the second operand.
|
|
212
|
+
*
|
|
213
|
+
* @param num1: The first operand
|
|
214
|
+
* @param num2: The second operand
|
|
215
|
+
* @returns `true` if the first operand is greater than or equal to the second operand or `false` otherwise
|
|
216
|
+
*
|
|
217
|
+
* @since v0.1.0
|
|
218
|
+
*/
|
|
219
|
+
export (>=)
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* @section Math operations: Infix functions for working with Number values.
|
|
223
|
+
*/
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Computes the sum of its operands.
|
|
227
|
+
*
|
|
228
|
+
* @param num1: The first operand
|
|
229
|
+
* @param num2: The second operand
|
|
230
|
+
* @returns The sum of the two operands
|
|
231
|
+
*
|
|
232
|
+
* @since v0.1.0
|
|
233
|
+
*/
|
|
33
234
|
export (+)
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Computes the difference of its operands.
|
|
238
|
+
*
|
|
239
|
+
* @param num1: The first operand
|
|
240
|
+
* @param num2: The second operand
|
|
241
|
+
* @returns The difference of the two operands
|
|
242
|
+
*
|
|
243
|
+
* @since v0.1.0
|
|
244
|
+
*/
|
|
34
245
|
export (-)
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Computes the product of its operands.
|
|
249
|
+
*
|
|
250
|
+
* @param num1: The first operand
|
|
251
|
+
* @param num2: The second operand
|
|
252
|
+
* @returns The product of the two operands
|
|
253
|
+
*
|
|
254
|
+
* @since v0.1.0
|
|
255
|
+
*/
|
|
35
256
|
export (*)
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Computes the quotient of its operands.
|
|
260
|
+
*
|
|
261
|
+
* @param num1: The first operand
|
|
262
|
+
* @param num2: The second operand
|
|
263
|
+
* @returns The quotient of the two operands
|
|
264
|
+
*
|
|
265
|
+
* @since v0.1.0
|
|
266
|
+
*/
|
|
36
267
|
export (/)
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Computes the remainder of the division of the first operand by the second.
|
|
271
|
+
* The result will have the sign of the second operand.
|
|
272
|
+
*
|
|
273
|
+
* @param num1: The first operand
|
|
274
|
+
* @param num2: The second operand
|
|
275
|
+
* @returns The modulus of its operands
|
|
276
|
+
*
|
|
277
|
+
* @since v0.1.0
|
|
278
|
+
*/
|
|
37
279
|
export (%)
|
|
38
280
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
281
|
+
/**
|
|
282
|
+
* Increments the value by one.
|
|
283
|
+
*
|
|
284
|
+
* @param value: The value to increment
|
|
285
|
+
* @returns The incremented value
|
|
286
|
+
*
|
|
287
|
+
* @since v0.1.0
|
|
288
|
+
*/
|
|
289
|
+
export incr
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* Decrements the value by one.
|
|
293
|
+
*
|
|
294
|
+
* @param value: The value to decrement
|
|
295
|
+
* @returns The decremented value
|
|
296
|
+
*
|
|
297
|
+
* @since v0.1.0
|
|
298
|
+
*/
|
|
299
|
+
export decr
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* @section String operations: Infix functions for operating on String values.
|
|
303
|
+
*/
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* Concatenate two strings.
|
|
307
|
+
*
|
|
308
|
+
* @param str1: The beginning string
|
|
309
|
+
* @param str2: The ending string
|
|
310
|
+
* @returns The combined string
|
|
311
|
+
*
|
|
312
|
+
* @example "Foo" ++ "Bar" == "FooBar"
|
|
313
|
+
*
|
|
314
|
+
* @since v0.2.0
|
|
315
|
+
*/
|
|
316
|
+
export (++)
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* @section Bitwise operations: Infix functions for operating on bits of Number values.
|
|
320
|
+
*/
|
|
44
321
|
|
|
45
|
-
|
|
322
|
+
/**
|
|
323
|
+
* Computes the bitwise NOT of the operand.
|
|
324
|
+
*
|
|
325
|
+
* @param value: The operand
|
|
326
|
+
* @returns Containing the inverted bits of the operand
|
|
327
|
+
*
|
|
328
|
+
* @since v0.2.0
|
|
329
|
+
*/
|
|
46
330
|
export lnot
|
|
47
331
|
|
|
332
|
+
/**
|
|
333
|
+
* Computes the bitwise AND (`&`) on the given operands.
|
|
334
|
+
*
|
|
335
|
+
* @param value1: The first operand
|
|
336
|
+
* @param value2: The second operand
|
|
337
|
+
* @returns Containing a `1` in each bit position for which the corresponding bits of both operands are `1`
|
|
338
|
+
*
|
|
339
|
+
* @since v0.3.0
|
|
340
|
+
* @history v0.2.0: Originally named `land`
|
|
341
|
+
* @history v0.3.0: Renamed to `&`
|
|
342
|
+
*/
|
|
48
343
|
export (&)
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* Computes the bitwise OR (`|`) on the given operands.
|
|
347
|
+
*
|
|
348
|
+
* @param value1: The first operand
|
|
349
|
+
* @param value2: The second operand
|
|
350
|
+
* @returns Containing a `1` in each bit position for which the corresponding bits of either or both operands are `1`
|
|
351
|
+
*
|
|
352
|
+
* @since v0.3.0
|
|
353
|
+
* @history v0.2.0: Originally named `lor`
|
|
354
|
+
* @history v0.3.0: Renamed to `|`
|
|
355
|
+
*/
|
|
49
356
|
export (|)
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* Computes the bitwise XOR (`^`) on the given operands.
|
|
360
|
+
*
|
|
361
|
+
* @param value1: The first operand
|
|
362
|
+
* @param value2: The second operand
|
|
363
|
+
* @returns Containing a `1` in each bit position for which the corresponding bits of either but not both operands are `1`
|
|
364
|
+
*
|
|
365
|
+
* @since v0.3.0
|
|
366
|
+
* @history v0.1.0: The `^` operator was originally an alias of `unbox`
|
|
367
|
+
* @history v0.2.0: Originally named `lxor`
|
|
368
|
+
* @history v0.3.0: Renamed to `^`
|
|
369
|
+
*/
|
|
50
370
|
export (^)
|
|
51
371
|
|
|
372
|
+
/**
|
|
373
|
+
* Shifts the bits of the value left by the given number of bits.
|
|
374
|
+
*
|
|
375
|
+
* @param value: The value to shift
|
|
376
|
+
* @param amount: The number of bits to shift by
|
|
377
|
+
* @returns The shifted value
|
|
378
|
+
*
|
|
379
|
+
* @since v0.3.0
|
|
380
|
+
* @history v0.2.0: Originally named `lsl`
|
|
381
|
+
* @history v0.3.0: Renamed to `<<`
|
|
382
|
+
*/
|
|
52
383
|
export (<<)
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* Shifts the bits of the value right by the given number of bits, preserving the sign bit.
|
|
387
|
+
*
|
|
388
|
+
* @param value: The value to shift
|
|
389
|
+
* @param amount: The amount to shift by
|
|
390
|
+
* @returns The shifted value
|
|
391
|
+
*
|
|
392
|
+
* @since v0.3.0
|
|
393
|
+
* @history v0.2.0: Originally named `lsr`
|
|
394
|
+
* @history v0.3.0: Renamed to `>>>`
|
|
395
|
+
*/
|
|
53
396
|
export (>>>)
|
|
54
|
-
export (>>)
|
|
55
397
|
|
|
398
|
+
/**
|
|
399
|
+
* Shifts the bits of the value right by the given number of bits.
|
|
400
|
+
*
|
|
401
|
+
* @param value: The value to shift
|
|
402
|
+
* @param amount: The amount to shift by
|
|
403
|
+
* @returns The shifted value
|
|
404
|
+
*
|
|
405
|
+
* @since v0.3.0
|
|
406
|
+
* @history v0.2.0: Originally named `asr`
|
|
407
|
+
* @history v0.3.0: Renamed to `>>`
|
|
408
|
+
*/
|
|
409
|
+
export (>>)
|
|
56
410
|
|
|
57
411
|
// Number coercions & conversions
|
|
58
412
|
|
|
@@ -60,59 +414,157 @@ export (>>)
|
|
|
60
414
|
// import foreign wasm convertExactToInexact : Number -> Number as inexact from "stdlib-external/runtime"
|
|
61
415
|
// import foreign wasm convertInexactToExact : Number -> Number as exact from "stdlib-external/runtime"
|
|
62
416
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
primitive (||) : (Bool, Bool) -> Bool = "@or"
|
|
417
|
+
/**
|
|
418
|
+
* @section Printing: Functions that deal with printing.
|
|
419
|
+
*/
|
|
67
420
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
421
|
+
/**
|
|
422
|
+
* Converts the given operand to a string.
|
|
423
|
+
* Provides a better representation of data types if those types are exported from the module.
|
|
424
|
+
*
|
|
425
|
+
* @param value: The operand
|
|
426
|
+
* @returns The operand, as a string
|
|
427
|
+
*
|
|
428
|
+
* @since v0.1.0
|
|
429
|
+
*/
|
|
430
|
+
export toString
|
|
71
431
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
432
|
+
/**
|
|
433
|
+
* Prints the given operand to the console. Works for any type. Internally, calls `toString`
|
|
434
|
+
* on the operand, so a better representation of data type will be printed if those types
|
|
435
|
+
* are exported from the module.
|
|
436
|
+
*
|
|
437
|
+
* @param value: The operand
|
|
438
|
+
*
|
|
439
|
+
* @since v0.1.0
|
|
440
|
+
*/
|
|
441
|
+
export print
|
|
75
442
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
primitive throw : Exception -> a = "@throw"
|
|
80
|
-
let fail : String -> a = msg => throw Failure(msg)
|
|
443
|
+
/**
|
|
444
|
+
* @section Type helpers: Functions that help with typechecking.
|
|
445
|
+
*/
|
|
81
446
|
|
|
82
|
-
|
|
447
|
+
/**
|
|
448
|
+
* Accepts any value and always returns `void`.
|
|
449
|
+
*
|
|
450
|
+
* @param value: The value to ignore
|
|
451
|
+
*
|
|
452
|
+
* @since v0.1.0
|
|
453
|
+
*/
|
|
454
|
+
export primitive ignore: a -> Void = "@ignore"
|
|
83
455
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
export print
|
|
456
|
+
/**
|
|
457
|
+
* @section Assertions: Functions that raise if conditions are not met.
|
|
458
|
+
*/
|
|
88
459
|
|
|
89
|
-
|
|
90
|
-
|
|
460
|
+
/**
|
|
461
|
+
* Assert that the given Boolean condition is `true`.
|
|
462
|
+
* Throws an `AssertionError` if the condition is `false`.
|
|
463
|
+
*
|
|
464
|
+
* @param condition: The condition to assert
|
|
465
|
+
*
|
|
466
|
+
* @example assert 3 > 2
|
|
467
|
+
* @example assert true
|
|
468
|
+
*
|
|
469
|
+
* @since v0.1.0
|
|
470
|
+
*/
|
|
471
|
+
export primitive assert: Bool -> Void = "@assert"
|
|
91
472
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
// Checks the given items for physical equality.
|
|
96
|
-
primitive (is) : (a, a) -> Bool = "@is"
|
|
97
|
-
// The opposite of is operator
|
|
98
|
-
let (isnt) : (a, a) -> Bool = (x, y) => !(x is y)
|
|
473
|
+
/**
|
|
474
|
+
* @section Failures: Functions that throw an Exception unconditionally.
|
|
475
|
+
*/
|
|
99
476
|
|
|
100
|
-
|
|
477
|
+
// Exceptions
|
|
478
|
+
export exception Failure(String)
|
|
479
|
+
export exception InvalidArgument(String)
|
|
101
480
|
|
|
102
481
|
/**
|
|
103
|
-
*
|
|
482
|
+
* Throw an exception. Currently, exceptions cannot be caught and will crash your program.
|
|
483
|
+
*
|
|
484
|
+
* @param exception: The exception to be thrown
|
|
485
|
+
* @returns Anything and nothing—your program won't continue past a throw
|
|
486
|
+
*
|
|
487
|
+
* @since v0.3.0
|
|
104
488
|
*/
|
|
105
|
-
|
|
106
|
-
let empty = [] // <- for parity with `cons`, but should be deleted as well
|
|
489
|
+
export primitive throw: Exception -> a = "@throw"
|
|
107
490
|
|
|
108
|
-
|
|
109
|
-
|
|
491
|
+
/**
|
|
492
|
+
* Unconditionally throw a `Failure` exception with a message.
|
|
493
|
+
* Currently, Exceptions cannot be caught and will crash your program.
|
|
494
|
+
*
|
|
495
|
+
* @param message: The reason for the failure
|
|
496
|
+
* @returns Anything and nothing—your program won't continue past a fail expression
|
|
497
|
+
*/
|
|
498
|
+
export let fail: String -> a = msg => throw Failure(msg)
|
|
110
499
|
|
|
111
|
-
|
|
112
|
-
|
|
500
|
+
/**
|
|
501
|
+
* @section Other: Other functions on values.
|
|
502
|
+
*/
|
|
113
503
|
|
|
114
|
-
|
|
115
|
-
|
|
504
|
+
/**
|
|
505
|
+
* Provides the operand untouched.
|
|
506
|
+
*
|
|
507
|
+
* @param value: The value to return
|
|
508
|
+
* @returns The value untouched
|
|
509
|
+
*
|
|
510
|
+
* @since v0.2.0
|
|
511
|
+
*/
|
|
512
|
+
export let identity = x => x
|
|
513
|
+
|
|
514
|
+
/**
|
|
515
|
+
* @section Box operations: Functions for working with Box values.
|
|
516
|
+
*/
|
|
517
|
+
|
|
518
|
+
/**
|
|
519
|
+
* Creates a box containing the given initial value.
|
|
520
|
+
* Values inside a box can be swapped out with the `:=` operator.
|
|
521
|
+
* Generally, `let mut` expressions are preferable to using a Box.
|
|
522
|
+
*
|
|
523
|
+
* @param initial: The initial value inside the box
|
|
524
|
+
* @returns The box containing the initial value
|
|
525
|
+
*
|
|
526
|
+
* @since v0.1.0
|
|
527
|
+
*/
|
|
528
|
+
export primitive box: a -> Box<a> = "@box"
|
|
529
|
+
|
|
530
|
+
/**
|
|
531
|
+
* Retrieves the current value from a box.
|
|
532
|
+
*
|
|
533
|
+
* @param box: The box to unwrap
|
|
534
|
+
* @returns The value inside the box
|
|
535
|
+
*
|
|
536
|
+
* @since v0.1.0
|
|
537
|
+
*/
|
|
538
|
+
export primitive unbox: Box<a> -> a = "@unbox"
|
|
539
|
+
|
|
540
|
+
/**
|
|
541
|
+
* @section List operations: Functions for working with List values.
|
|
542
|
+
*/
|
|
543
|
+
|
|
544
|
+
/**
|
|
545
|
+
* The list spread syntax (`[a, ...b]`) provided as a function.
|
|
546
|
+
*
|
|
547
|
+
* @param a: The head of the list
|
|
548
|
+
* @param b: The tail of the list
|
|
549
|
+
* @returns The new list
|
|
550
|
+
*
|
|
551
|
+
* @deprecated This will be removed in a future release of Grain.
|
|
552
|
+
*
|
|
553
|
+
* @since v0.4.0
|
|
554
|
+
*/
|
|
555
|
+
export let cons = (a, b) =>
|
|
556
|
+
[
|
|
557
|
+
a,
|
|
558
|
+
...b
|
|
559
|
+
] // <- workaround for (grain-lang/grain#802) [TODO] fix #802 and delete
|
|
560
|
+
/**
|
|
561
|
+
* The empty list syntax (`[]`) provided as a value.
|
|
562
|
+
*
|
|
563
|
+
* @deprecated This will be removed in a future release of Grain.
|
|
564
|
+
*
|
|
565
|
+
* @since v0.4.0
|
|
566
|
+
*/
|
|
567
|
+
export let empty = [] // <- for parity with `cons`, but should be deleted as well
|
|
116
568
|
|
|
117
569
|
// Setup exception printing
|
|
118
570
|
@disableGC
|
|
@@ -121,7 +573,7 @@ let rec setupExceptions = () => {
|
|
|
121
573
|
match (e) {
|
|
122
574
|
Failure(msg) => Some("Failure: " ++ msg),
|
|
123
575
|
InvalidArgument(msg) => Some("Invalid argument: " ++ msg),
|
|
124
|
-
_ => None
|
|
576
|
+
_ => None,
|
|
125
577
|
}
|
|
126
578
|
})
|
|
127
579
|
|