@grain/stdlib 0.4.2 → 0.4.6

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.
Files changed (61) hide show
  1. package/CHANGELOG.md +52 -0
  2. package/LICENSE +1 -1
  3. package/array.gr +200 -89
  4. package/array.md +81 -5
  5. package/buffer.gr +93 -36
  6. package/bytes.gr +10 -10
  7. package/char.gr +112 -56
  8. package/char.md +200 -0
  9. package/float32.gr +120 -4
  10. package/float32.md +315 -0
  11. package/float64.gr +120 -4
  12. package/float64.md +315 -0
  13. package/hash.gr +42 -15
  14. package/hash.md +44 -0
  15. package/int32.gr +370 -75
  16. package/int32.md +833 -0
  17. package/int64.gr +370 -75
  18. package/int64.md +833 -0
  19. package/list.gr +121 -50
  20. package/map.gr +106 -110
  21. package/number.gr +37 -1
  22. package/number.md +66 -0
  23. package/option.gr +260 -53
  24. package/option.md +579 -0
  25. package/package.json +1 -1
  26. package/pervasives.gr +32 -20
  27. package/queue.gr +102 -30
  28. package/queue.md +191 -0
  29. package/range.gr +26 -26
  30. package/range.md +1 -1
  31. package/regex.md +9 -9
  32. package/result.gr +216 -70
  33. package/result.md +446 -0
  34. package/runtime/dataStructures.gr +28 -29
  35. package/runtime/debug.gr +0 -1
  36. package/runtime/equal.gr +37 -16
  37. package/runtime/exception.gr +28 -15
  38. package/runtime/gc.gr +33 -20
  39. package/runtime/malloc.gr +19 -11
  40. package/runtime/numberUtils.gr +208 -103
  41. package/runtime/numbers.gr +217 -118
  42. package/runtime/string.gr +98 -39
  43. package/runtime/stringUtils.gr +176 -0
  44. package/runtime/unsafe/conv.gr +10 -10
  45. package/runtime/unsafe/memory.gr +14 -3
  46. package/runtime/unsafe/printWasm.gr +4 -4
  47. package/runtime/unsafe/tags.gr +2 -2
  48. package/runtime/unsafe/wasmf32.gr +9 -2
  49. package/runtime/unsafe/wasmf64.gr +9 -2
  50. package/runtime/unsafe/wasmi32.gr +65 -47
  51. package/runtime/unsafe/wasmi64.gr +78 -50
  52. package/runtime/wasi.gr +199 -45
  53. package/set.gr +281 -119
  54. package/set.md +502 -0
  55. package/stack.gr +26 -26
  56. package/string.gr +657 -341
  57. package/string.md +815 -0
  58. package/sys/file.gr +356 -177
  59. package/sys/process.gr +10 -6
  60. package/sys/random.gr +3 -6
  61. package/sys/time.gr +3 -3
package/queue.gr CHANGED
@@ -1,46 +1,118 @@
1
+ /**
2
+ * @module Queue: An immutable queue implementation. A queue is a FIFO (first-in-first-out) data structure where new values are added to the end and retrieved or removed from the beginning.
3
+ * @example import Queue from "queue"
4
+ * @since v0.2.0
5
+ */
1
6
  import List from "list"
2
7
 
3
- record Queue<a> { forwards: List<a>, backwards: List<a> }
8
+ record Queue<a> {
9
+ forwards: List<a>,
10
+ backwards: List<a>,
11
+ }
12
+
13
+ /**
14
+ * @section Values: Functions for working with queues.
15
+ */
4
16
 
17
+ /**
18
+ * Creates an empty queue.
19
+ *
20
+ * @returns An empty queue
21
+ * @since v0.2.0
22
+ */
5
23
  export let make = () => {
6
- { forwards: [], backwards: [] }
24
+ { forwards: [], backwards: [] }
7
25
  }
8
26
 
9
- export let isEmpty = (queue) => {
10
- match (queue) {
11
- { forwards: [], backwards: [] } => true,
12
- _ => false
13
- }
27
+ /**
28
+ * Checks if the given queue contains any values.
29
+ *
30
+ * @param queue: The queue to check
31
+ * @returns `true` if the given queue is empty or `false` otherwise
32
+ *
33
+ * @since v0.2.0
34
+ */
35
+ export let isEmpty = queue => {
36
+ match (queue) {
37
+ { forwards: [], backwards: [] } => true,
38
+ _ => false,
39
+ }
14
40
  }
15
41
 
16
- export let peek = (queue) => {
17
- match (queue) {
18
- { forwards: [], backwards: [] } => None,
19
- { forwards, backwards } => List.head(forwards)
20
- }
42
+ /**
43
+ * Returns the value at the beginning of the queue. It is not removed from the queue.
44
+ *
45
+ * @param queue: The queue to inspect
46
+ * @returns `Some(value)` containing the value at the beginning of the queue, or `None` if the queue is empty
47
+ *
48
+ * @since v0.3.2
49
+ * @history v0.2.0: Originally named `head`
50
+ * @history v0.3.2: Deprecated `head` function
51
+ * @history v0.4.0: Removed `head` function
52
+ */
53
+ export let peek = queue => {
54
+ match (queue) {
55
+ { forwards: [], backwards: [] } => None,
56
+ { forwards, backwards } => List.head(forwards),
57
+ }
21
58
  }
22
59
 
60
+ /**
61
+ * Adds a value to the end of the queue.
62
+ *
63
+ * @param value: The value to append
64
+ * @param queue: The queue to update
65
+ * @returns An updated queue
66
+ *
67
+ * @since v0.3.2
68
+ * @history v0.2.0: Originally named `enqueue`
69
+ * @history v0.3.2: Deprecated `enqueue` function
70
+ * @history v0.4.0: Removed `enqueue` function
71
+ */
23
72
  export let push = (value, queue) => {
24
- match (queue) {
25
- { forwards: [], backwards: [] } => { forwards: [value], backwards: [] },
26
- { forwards, backwards } => { forwards, backwards: [value, ...backwards] }
27
- }
73
+ match (queue) {
74
+ { forwards: [], backwards: [] } => { forwards: [value], backwards: [] },
75
+ { forwards, backwards } => { forwards, backwards: [value, ...backwards] },
76
+ }
28
77
  }
29
78
 
30
- export let pop = (queue) => {
31
- match (queue) {
32
- { forwards: [], backwards: [] } => queue,
33
- { forwards: [head], backwards: [] } => { forwards: [], backwards: [] },
34
- { forwards: [head], backwards } => { forwards: List.reverse(backwards), backwards: [] },
35
- { forwards: [head, ...ftail], backwards } => { forwards: ftail, backwards }
36
- }
79
+ /**
80
+ * Dequeues the next value in the queue.
81
+ *
82
+ * @param queue: The queue to change
83
+ * @returns An updated queue
84
+ *
85
+ * @since v0.3.2
86
+ * @history v0.2.0: Originally named `dequeue`
87
+ * @history v0.3.2: Deprecated `dequeue` function
88
+ * @history v0.4.0: Removed `dequeue` function
89
+ */
90
+ export let pop = queue => {
91
+ match (queue) {
92
+ { forwards: [], backwards: [] } => queue,
93
+ { forwards: [head], backwards: [] } => { forwards: [], backwards: [] },
94
+ { forwards: [head], backwards } =>
95
+ {
96
+ forwards: List.reverse(backwards),
97
+ backwards: [],
98
+ },
99
+ { forwards: [head, ...ftail], backwards } => { forwards: ftail, backwards },
100
+ }
37
101
  }
38
102
 
39
- export let size = (queue) => {
40
- match (queue) {
41
- { forwards: [], backwards: [] } => 0,
42
- { forwards, backwards: [] } => List.length(forwards),
43
- { forwards: [], backwards } => List.length(backwards),
44
- { forwards, backwards } => List.length(forwards) + List.length(backwards),
45
- }
103
+ /**
104
+ * Get the number of values in a queue.
105
+ *
106
+ * @param queue: The queue to inspect
107
+ * @returns The number of values in the queue
108
+ *
109
+ * @since v0.3.2
110
+ */
111
+ export let size = queue => {
112
+ match (queue) {
113
+ { forwards: [], backwards: [] } => 0,
114
+ { forwards, backwards: [] } => List.length(forwards),
115
+ { forwards: [], backwards } => List.length(backwards),
116
+ { forwards, backwards } => List.length(forwards) + List.length(backwards),
117
+ }
46
118
  }
package/queue.md ADDED
@@ -0,0 +1,191 @@
1
+ ---
2
+ title: Queue
3
+ ---
4
+
5
+ An immutable queue implementation. A queue is a FIFO (first-in-first-out) data structure where new values are added to the end and retrieved or removed from the beginning.
6
+
7
+ <details disabled>
8
+ <summary tabindex="-1">Added in <code>0.2.0</code></summary>
9
+ No other changes yet.
10
+ </details>
11
+
12
+ ```grain
13
+ import Queue from "queue"
14
+ ```
15
+
16
+ ## Values
17
+
18
+ Functions for working with queues.
19
+
20
+ ### Queue.**make**
21
+
22
+ <details disabled>
23
+ <summary tabindex="-1">Added in <code>0.2.0</code></summary>
24
+ No other changes yet.
25
+ </details>
26
+
27
+ ```grain
28
+ make : () -> Queue<a>
29
+ ```
30
+
31
+ Creates an empty queue.
32
+
33
+ Returns:
34
+
35
+ |type|description|
36
+ |----|-----------|
37
+ |`Queue<a>`|An empty queue|
38
+
39
+ ### Queue.**isEmpty**
40
+
41
+ <details disabled>
42
+ <summary tabindex="-1">Added in <code>0.2.0</code></summary>
43
+ No other changes yet.
44
+ </details>
45
+
46
+ ```grain
47
+ isEmpty : Queue<a> -> Bool
48
+ ```
49
+
50
+ Checks if the given queue contains any values.
51
+
52
+ Parameters:
53
+
54
+ |param|type|description|
55
+ |-----|----|-----------|
56
+ |`queue`|`Queue<a>`|The queue to check|
57
+
58
+ Returns:
59
+
60
+ |type|description|
61
+ |----|-----------|
62
+ |`Bool`|`true` if the given queue is empty or `false` otherwise|
63
+
64
+ ### Queue.**peek**
65
+
66
+ <details>
67
+ <summary>Added in <code>0.3.2</code></summary>
68
+ <table>
69
+ <thead>
70
+ <tr><th>version</th><th>changes</th></tr>
71
+ </thead>
72
+ <tbody>
73
+ <tr><td><code>0.2.0</code></td><td>Originally named `head`</td></tr>
74
+ <tr><td><code>0.3.2</code></td><td>Deprecated `head` function</td></tr>
75
+ <tr><td><code>0.4.0</code></td><td>Removed `head` function</td></tr>
76
+ </tbody>
77
+ </table>
78
+ </details>
79
+
80
+ ```grain
81
+ peek : Queue<a> -> Option<a>
82
+ ```
83
+
84
+ Returns the value at the beginning of the queue. It is not removed from the queue.
85
+
86
+ Parameters:
87
+
88
+ |param|type|description|
89
+ |-----|----|-----------|
90
+ |`queue`|`Queue<a>`|The queue to inspect|
91
+
92
+ Returns:
93
+
94
+ |type|description|
95
+ |----|-----------|
96
+ |`Option<a>`|`Some(value)` containing the value at the beginning of the queue, or `None` if the queue is empty|
97
+
98
+ ### Queue.**push**
99
+
100
+ <details>
101
+ <summary>Added in <code>0.3.2</code></summary>
102
+ <table>
103
+ <thead>
104
+ <tr><th>version</th><th>changes</th></tr>
105
+ </thead>
106
+ <tbody>
107
+ <tr><td><code>0.2.0</code></td><td>Originally named `enqueue`</td></tr>
108
+ <tr><td><code>0.3.2</code></td><td>Deprecated `enqueue` function</td></tr>
109
+ <tr><td><code>0.4.0</code></td><td>Removed `enqueue` function</td></tr>
110
+ </tbody>
111
+ </table>
112
+ </details>
113
+
114
+ ```grain
115
+ push : (a, Queue<a>) -> Queue<a>
116
+ ```
117
+
118
+ Adds a value to the end of the queue.
119
+
120
+ Parameters:
121
+
122
+ |param|type|description|
123
+ |-----|----|-----------|
124
+ |`value`|`a`|The value to append|
125
+ |`queue`|`Queue<a>`|The queue to update|
126
+
127
+ Returns:
128
+
129
+ |type|description|
130
+ |----|-----------|
131
+ |`Queue<a>`|An updated queue|
132
+
133
+ ### Queue.**pop**
134
+
135
+ <details>
136
+ <summary>Added in <code>0.3.2</code></summary>
137
+ <table>
138
+ <thead>
139
+ <tr><th>version</th><th>changes</th></tr>
140
+ </thead>
141
+ <tbody>
142
+ <tr><td><code>0.2.0</code></td><td>Originally named `dequeue`</td></tr>
143
+ <tr><td><code>0.3.2</code></td><td>Deprecated `dequeue` function</td></tr>
144
+ <tr><td><code>0.4.0</code></td><td>Removed `dequeue` function</td></tr>
145
+ </tbody>
146
+ </table>
147
+ </details>
148
+
149
+ ```grain
150
+ pop : Queue<a> -> Queue<a>
151
+ ```
152
+
153
+ Dequeues the next value in the queue.
154
+
155
+ Parameters:
156
+
157
+ |param|type|description|
158
+ |-----|----|-----------|
159
+ |`queue`|`Queue<a>`|The queue to change|
160
+
161
+ Returns:
162
+
163
+ |type|description|
164
+ |----|-----------|
165
+ |`Queue<a>`|An updated queue|
166
+
167
+ ### Queue.**size**
168
+
169
+ <details disabled>
170
+ <summary tabindex="-1">Added in <code>0.3.2</code></summary>
171
+ No other changes yet.
172
+ </details>
173
+
174
+ ```grain
175
+ size : Queue<a> -> Number
176
+ ```
177
+
178
+ Get the number of values in a queue.
179
+
180
+ Parameters:
181
+
182
+ |param|type|description|
183
+ |-----|----|-----------|
184
+ |`queue`|`Queue<a>`|The queue to inspect|
185
+
186
+ Returns:
187
+
188
+ |type|description|
189
+ |----|-----------|
190
+ |`Number`|The number of values in the queue|
191
+
package/range.gr CHANGED
@@ -38,7 +38,7 @@ export let inRange = (value, range) => {
38
38
  Inclusive(upper, lower) when value >= lower && value <= upper => true,
39
39
  Exclusive(lower, upper) when value >= lower && value < upper => true,
40
40
  Exclusive(upper, lower) when value >= lower && value < upper => true,
41
- _ => false
41
+ _ => false,
42
42
  }
43
43
  }
44
44
 
@@ -52,34 +52,34 @@ export let inRange = (value, range) => {
52
52
  *
53
53
  * @since v0.3.0
54
54
  */
55
- export let forEach = (fn: (Number) -> Void, range) => {
55
+ export let forEach = (fn: Number -> Void, range) => {
56
56
  match (range) {
57
57
  Inclusive(lower, upper) when lower <= upper => {
58
- let mut idx = lower;
58
+ let mut idx = lower
59
59
  while (idx <= upper) {
60
- fn(idx);
61
- idx += 1;
60
+ fn(idx)
61
+ idx += 1
62
62
  }
63
63
  },
64
64
  Inclusive(upper, lower) => {
65
- let mut idx = upper;
65
+ let mut idx = upper
66
66
  while (idx >= lower) {
67
- fn(idx);
68
- idx -= 1;
67
+ fn(idx)
68
+ idx -= 1
69
69
  }
70
70
  },
71
71
  Exclusive(lower, upper) when lower <= upper => {
72
- let mut idx = lower;
72
+ let mut idx = lower
73
73
  while (idx < upper) {
74
- fn(idx);
75
- idx += 1;
74
+ fn(idx)
75
+ idx += 1
76
76
  }
77
77
  },
78
78
  Exclusive(upper, lower) => {
79
- let mut idx = upper;
79
+ let mut idx = upper
80
80
  while (idx > lower) {
81
- fn(idx);
82
- idx -= 1;
81
+ fn(idx)
82
+ idx -= 1
83
83
  }
84
84
  },
85
85
  }
@@ -100,31 +100,31 @@ export let map = (fn, range) => {
100
100
  let mut result = []
101
101
  match (range) {
102
102
  Inclusive(lower, upper) when lower <= upper => {
103
- let mut idx = upper;
103
+ let mut idx = upper
104
104
  while (idx >= lower) {
105
- result = [fn(idx), ...result];
106
- idx -= 1;
105
+ result = [fn(idx), ...result]
106
+ idx -= 1
107
107
  }
108
108
  },
109
109
  Inclusive(upper, lower) => {
110
- let mut idx = lower;
110
+ let mut idx = lower
111
111
  while (idx <= upper) {
112
- result = [fn(idx), ...result];
113
- idx += 1;
112
+ result = [fn(idx), ...result]
113
+ idx += 1
114
114
  }
115
115
  },
116
116
  Exclusive(lower, upper) when lower <= upper => {
117
- let mut idx = upper - 1;
117
+ let mut idx = upper - 1
118
118
  while (idx >= lower) {
119
- result = [fn(idx), ...result];
120
- idx -= 1;
119
+ result = [fn(idx), ...result]
120
+ idx -= 1
121
121
  }
122
122
  },
123
123
  Exclusive(upper, lower) => {
124
- let mut idx = lower + 1;
124
+ let mut idx = lower + 1
125
125
  while (idx <= upper) {
126
- result = [fn(idx), ...result];
127
- idx += 1;
126
+ result = [fn(idx), ...result]
127
+ idx += 1
128
128
  }
129
129
  },
130
130
  }
package/range.md CHANGED
@@ -102,7 +102,7 @@ No other changes yet.
102
102
  </details>
103
103
 
104
104
  ```grain
105
- map : ((Number -> a), Range) -> List<b>
105
+ map : ((Number -> a), Range) -> List<a>
106
106
  ```
107
107
 
108
108
  Produces a list by calling the given function on each number included in the range. For increasing ranges, the value is increased by `1` in each iteration, and for decreasing ranges, the value is decreased by `1`. The value is always changed by `1`, even if non-integer values were provided in the range.
package/regex.md CHANGED
@@ -5,7 +5,7 @@ title: Regex
5
5
  Regular Expressions.
6
6
 
7
7
  <details disabled>
8
- <summary tabindex="-1">Added in <code>next</code></summary>
8
+ <summary tabindex="-1">Added in <code>0.4.3</code></summary>
9
9
  No other changes yet.
10
10
  </details>
11
11
 
@@ -20,7 +20,7 @@ Functions for working with regular expressions.
20
20
  ### Regex.**make**
21
21
 
22
22
  <details disabled>
23
- <summary tabindex="-1">Added in <code>next</code></summary>
23
+ <summary tabindex="-1">Added in <code>0.4.3</code></summary>
24
24
  No other changes yet.
25
25
  </details>
26
26
 
@@ -180,7 +180,7 @@ Returns the positions of all groups matched in this match object.
180
180
  ### Regex.**isMatch**
181
181
 
182
182
  <details disabled>
183
- <summary tabindex="-1">Added in <code>next</code></summary>
183
+ <summary tabindex="-1">Added in <code>0.4.3</code></summary>
184
184
  No other changes yet.
185
185
  </details>
186
186
 
@@ -212,7 +212,7 @@ assert Regex.isMatch(Result.unwrap(Regex.make("ca+[at]")), "caaat") == true
212
212
  ### Regex.**isMatchRange**
213
213
 
214
214
  <details disabled>
215
- <summary tabindex="-1">Added in <code>next</code></summary>
215
+ <summary tabindex="-1">Added in <code>0.4.3</code></summary>
216
216
  No other changes yet.
217
217
  </details>
218
218
 
@@ -250,7 +250,7 @@ assert Regex.isMatchRange(Result.unwrap(Regex.make("ca+[at]")), "caaat", 1, 5) =
250
250
  ### Regex.**find**
251
251
 
252
252
  <details disabled>
253
- <summary tabindex="-1">Added in <code>next</code></summary>
253
+ <summary tabindex="-1">Added in <code>0.4.3</code></summary>
254
254
  No other changes yet.
255
255
  </details>
256
256
 
@@ -282,7 +282,7 @@ Regex.find(Result.unwrap(Regex.make("ca+[at]")), "caaat")
282
282
  ### Regex.**findRange**
283
283
 
284
284
  <details disabled>
285
- <summary tabindex="-1">Added in <code>next</code></summary>
285
+ <summary tabindex="-1">Added in <code>0.4.3</code></summary>
286
286
  No other changes yet.
287
287
  </details>
288
288
 
@@ -339,7 +339,7 @@ Returns:
339
339
  ### Regex.**findAllRange**
340
340
 
341
341
  <details disabled>
342
- <summary tabindex="-1">Added in <code>next</code></summary>
342
+ <summary tabindex="-1">Added in <code>0.4.3</code></summary>
343
343
  No other changes yet.
344
344
  </details>
345
345
 
@@ -375,7 +375,7 @@ Regex.findAllRange(Result.unwrap(Regex.make("ca+[at]")), "caaat", 0, 5)
375
375
  ### Regex.**replace**
376
376
 
377
377
  <details disabled>
378
- <summary tabindex="-1">Added in <code>next</code></summary>
378
+ <summary tabindex="-1">Added in <code>0.4.3</code></summary>
379
379
  No other changes yet.
380
380
  </details>
381
381
 
@@ -416,7 +416,7 @@ assert Regex.replace(Result.unwrap(Regex.make("o")), "foo", "a") == "fao"
416
416
  ### Regex.**replaceAll**
417
417
 
418
418
  <details disabled>
419
- <summary tabindex="-1">Added in <code>next</code></summary>
419
+ <summary tabindex="-1">Added in <code>0.4.3</code></summary>
420
420
  No other changes yet.
421
421
  </details>
422
422