@grain/stdlib 0.4.3 → 0.4.4

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/set.md ADDED
@@ -0,0 +1,502 @@
1
+ ---
2
+ title: Set
3
+ ---
4
+
5
+ A Set is an unordered collection of unique values. Operations on a Set mutate the internal state, so it never needs to be re-assigned.
6
+
7
+ <details disabled>
8
+ <summary tabindex="-1">Added in <code>0.3.0</code></summary>
9
+ No other changes yet.
10
+ </details>
11
+
12
+ ```grain
13
+ import Set from "set"
14
+ ```
15
+
16
+ ### Set.**Bucket**
17
+
18
+ ```grain
19
+ record Bucket<t> {
20
+ key: t,
21
+ next: Option<Bucket<t>>,
22
+ }
23
+ ```
24
+
25
+ ### Set.**Set**
26
+
27
+ ```grain
28
+ record Set<k> {
29
+ size: Number,
30
+ buckets: Array<Option<Bucket<k>>>,
31
+ }
32
+ ```
33
+
34
+ ### Set.**makeSized**
35
+
36
+ <details disabled>
37
+ <summary tabindex="-1">Added in <code>0.3.0</code></summary>
38
+ No other changes yet.
39
+ </details>
40
+
41
+ ```grain
42
+ makeSized : Number -> Set<a>
43
+ ```
44
+
45
+ Creates a new empty set with an initial storage of the given length. As values are added or removed, the length may grow or shrink. Generally, you won't need to care about the length of your set and can use `Set.make()` instead.
46
+
47
+ Parameters:
48
+
49
+ |param|type|description|
50
+ |-----|----|-----------|
51
+ |`storageLength`|`Number`|The initial storage length of the set|
52
+
53
+ Returns:
54
+
55
+ |type|description|
56
+ |----|-----------|
57
+ |`Set<a>`|An empty set with the given initial storage length|
58
+
59
+ ### Set.**make**
60
+
61
+ <details disabled>
62
+ <summary tabindex="-1">Added in <code>0.3.0</code></summary>
63
+ No other changes yet.
64
+ </details>
65
+
66
+ ```grain
67
+ make : () -> Set<a>
68
+ ```
69
+
70
+ Creates a new, empty set.
71
+
72
+ Returns:
73
+
74
+ |type|description|
75
+ |----|-----------|
76
+ |`Set<a>`|An empty set|
77
+
78
+ ### Set.**add**
79
+
80
+ <details disabled>
81
+ <summary tabindex="-1">Added in <code>0.3.0</code></summary>
82
+ No other changes yet.
83
+ </details>
84
+
85
+ ```grain
86
+ add : (a, Set<a>) -> Void
87
+ ```
88
+
89
+ Adds a new value to the set. If the value already exists, nothing happens.
90
+
91
+ Parameters:
92
+
93
+ |param|type|description|
94
+ |-----|----|-----------|
95
+ |`key`|`a`|The value to add|
96
+ |`set`|`Set<a>`|The set to update|
97
+
98
+ ### Set.**contains**
99
+
100
+ <details disabled>
101
+ <summary tabindex="-1">Added in <code>0.3.0</code></summary>
102
+ No other changes yet.
103
+ </details>
104
+
105
+ ```grain
106
+ contains : (a, Set<a>) -> Bool
107
+ ```
108
+
109
+ Determines if the set contains the given value.
110
+
111
+ Parameters:
112
+
113
+ |param|type|description|
114
+ |-----|----|-----------|
115
+ |`key`|`a`|The value to search for|
116
+ |`set`|`Set<a>`|The set to search|
117
+
118
+ Returns:
119
+
120
+ |type|description|
121
+ |----|-----------|
122
+ |`Bool`|`true` if the set contains the given value or `false` otherwise|
123
+
124
+ ### Set.**remove**
125
+
126
+ <details disabled>
127
+ <summary tabindex="-1">Added in <code>0.3.0</code></summary>
128
+ No other changes yet.
129
+ </details>
130
+
131
+ ```grain
132
+ remove : (a, Set<a>) -> Void
133
+ ```
134
+
135
+ Removes the given value from the set. If the value doesn't exist, nothing happens.
136
+
137
+ Parameters:
138
+
139
+ |param|type|description|
140
+ |-----|----|-----------|
141
+ |`key`|`a`|The value to remove|
142
+ |`set`|`Set<a>`|The set to update|
143
+
144
+ ### Set.**size**
145
+
146
+ <details disabled>
147
+ <summary tabindex="-1">Added in <code>0.3.0</code></summary>
148
+ No other changes yet.
149
+ </details>
150
+
151
+ ```grain
152
+ size : Set<a> -> Number
153
+ ```
154
+
155
+ Returns the number of values within the set.
156
+
157
+ Parameters:
158
+
159
+ |param|type|description|
160
+ |-----|----|-----------|
161
+ |`set`|`Set<a>`|The set to inspect|
162
+
163
+ Returns:
164
+
165
+ |type|description|
166
+ |----|-----------|
167
+ |`Number`|The number of elements in the set|
168
+
169
+ ### Set.**isEmpty**
170
+
171
+ <details disabled>
172
+ <summary tabindex="-1">Added in <code>0.3.0</code></summary>
173
+ No other changes yet.
174
+ </details>
175
+
176
+ ```grain
177
+ isEmpty : Set<a> -> Bool
178
+ ```
179
+
180
+ Determines if the set contains no elements.
181
+
182
+ Parameters:
183
+
184
+ |param|type|description|
185
+ |-----|----|-----------|
186
+ |`set`|`Set<a>`|The set to inspect|
187
+
188
+ Returns:
189
+
190
+ |type|description|
191
+ |----|-----------|
192
+ |`Bool`|`true` if the given set is empty or `false` otherwise|
193
+
194
+ ### Set.**clear**
195
+
196
+ <details disabled>
197
+ <summary tabindex="-1">Added in <code>0.3.0</code></summary>
198
+ No other changes yet.
199
+ </details>
200
+
201
+ ```grain
202
+ clear : Set<a> -> Void
203
+ ```
204
+
205
+ Resets the set by removing all values.
206
+
207
+ Parameters:
208
+
209
+ |param|type|description|
210
+ |-----|----|-----------|
211
+ |`set`|`Set<a>`|The set to reset|
212
+
213
+ ### Set.**forEach**
214
+
215
+ <details disabled>
216
+ <summary tabindex="-1">Added in <code>0.3.0</code></summary>
217
+ No other changes yet.
218
+ </details>
219
+
220
+ ```grain
221
+ forEach : ((a -> b), Set<a>) -> Void
222
+ ```
223
+
224
+ Iterates the set, calling an iterator function on each element.
225
+
226
+ Parameters:
227
+
228
+ |param|type|description|
229
+ |-----|----|-----------|
230
+ |`fn`|`a -> b`|The iterator function to call with each element|
231
+ |`set`|`Set<a>`|The set to iterate|
232
+
233
+ ### Set.**reduce**
234
+
235
+ <details disabled>
236
+ <summary tabindex="-1">Added in <code>0.3.0</code></summary>
237
+ No other changes yet.
238
+ </details>
239
+
240
+ ```grain
241
+ reduce : (((a, b) -> a), a, Set<b>) -> a
242
+ ```
243
+
244
+ Combines all elements of a set using a reducer function.
245
+
246
+ Parameters:
247
+
248
+ |param|type|description|
249
+ |-----|----|-----------|
250
+ |`fn`|`(a, b) -> a`|The reducer function to call on each element, where the value returned will be the next accumulator value|
251
+ |`init`|`a`|The initial value to use for the accumulator on the first iteration|
252
+ |`set`|`Set<b>`|The set to iterate|
253
+
254
+ Returns:
255
+
256
+ |type|description|
257
+ |----|-----------|
258
+ |`a`|The final accumulator returned from `fn`|
259
+
260
+ ### Set.**filter**
261
+
262
+ <details disabled>
263
+ <summary tabindex="-1">Added in <code>0.3.0</code></summary>
264
+ No other changes yet.
265
+ </details>
266
+
267
+ ```grain
268
+ filter : ((a -> Bool), Set<a>) -> Void
269
+ ```
270
+
271
+ Removes elements from a set where a predicate function returns `false`.
272
+
273
+ Parameters:
274
+
275
+ |param|type|description|
276
+ |-----|----|-----------|
277
+ |`fn`|`a -> Bool`|The predicate function to indicate which elements to remove from the set, where returning `false` indicates the value should be removed|
278
+ |`set`|`Set<a>`|The set to iterate|
279
+
280
+ ### Set.**reject**
281
+
282
+ <details disabled>
283
+ <summary tabindex="-1">Added in <code>0.3.0</code></summary>
284
+ No other changes yet.
285
+ </details>
286
+
287
+ ```grain
288
+ reject : ((a -> Bool), Set<a>) -> Void
289
+ ```
290
+
291
+ Removes elements from a set where a predicate function returns `true`.
292
+
293
+ Parameters:
294
+
295
+ |param|type|description|
296
+ |-----|----|-----------|
297
+ |`fn`|`a -> Bool`|The predicate function to indicate which elements to remove from the set, where returning `true` indicates the value should be removed|
298
+ |`set`|`Set<a>`|The set to iterate|
299
+
300
+ ### Set.**toList**
301
+
302
+ <details disabled>
303
+ <summary tabindex="-1">Added in <code>0.3.0</code></summary>
304
+ No other changes yet.
305
+ </details>
306
+
307
+ ```grain
308
+ toList : Set<a> -> List<a>
309
+ ```
310
+
311
+ Converts a set into a list of its elements.
312
+
313
+ Parameters:
314
+
315
+ |param|type|description|
316
+ |-----|----|-----------|
317
+ |`set`|`Set<a>`|The set to convert|
318
+
319
+ Returns:
320
+
321
+ |type|description|
322
+ |----|-----------|
323
+ |`List<a>`|A list containing all set values|
324
+
325
+ ### Set.**fromList**
326
+
327
+ <details disabled>
328
+ <summary tabindex="-1">Added in <code>0.3.0</code></summary>
329
+ No other changes yet.
330
+ </details>
331
+
332
+ ```grain
333
+ fromList : List<a> -> Set<a>
334
+ ```
335
+
336
+ Creates a set from a list.
337
+
338
+ Parameters:
339
+
340
+ |param|type|description|
341
+ |-----|----|-----------|
342
+ |`list`|`List<a>`|The list to convert|
343
+
344
+ Returns:
345
+
346
+ |type|description|
347
+ |----|-----------|
348
+ |`Set<a>`|A set containing all list values|
349
+
350
+ ### Set.**toArray**
351
+
352
+ <details disabled>
353
+ <summary tabindex="-1">Added in <code>0.3.0</code></summary>
354
+ No other changes yet.
355
+ </details>
356
+
357
+ ```grain
358
+ toArray : Set<a> -> Array<a>
359
+ ```
360
+
361
+ Converts a set into an array of its elements.
362
+
363
+ Parameters:
364
+
365
+ |param|type|description|
366
+ |-----|----|-----------|
367
+ |`set`|`Set<a>`|The set to convert|
368
+
369
+ Returns:
370
+
371
+ |type|description|
372
+ |----|-----------|
373
+ |`Array<a>`|An array containing all set values|
374
+
375
+ ### Set.**fromArray**
376
+
377
+ <details disabled>
378
+ <summary tabindex="-1">Added in <code>0.3.0</code></summary>
379
+ No other changes yet.
380
+ </details>
381
+
382
+ ```grain
383
+ fromArray : Array<a> -> Set<a>
384
+ ```
385
+
386
+ Creates a set from an array.
387
+
388
+ Parameters:
389
+
390
+ |param|type|description|
391
+ |-----|----|-----------|
392
+ |`array`|`Array<a>`|The array to convert|
393
+
394
+ Returns:
395
+
396
+ |type|description|
397
+ |----|-----------|
398
+ |`Set<a>`|A set containing all array values|
399
+
400
+ ### Set.**union**
401
+
402
+ <details disabled>
403
+ <summary tabindex="-1">Added in <code>0.3.0</code></summary>
404
+ No other changes yet.
405
+ </details>
406
+
407
+ ```grain
408
+ union : (Set<a>, Set<a>) -> Set<a>
409
+ ```
410
+
411
+ Combines two sets into a single set containing all elements from both sets.
412
+
413
+ Parameters:
414
+
415
+ |param|type|description|
416
+ |-----|----|-----------|
417
+ |`set1`|`Set<a>`|The first set to combine|
418
+ |`set2`|`Set<a>`|The second set to combine|
419
+
420
+ Returns:
421
+
422
+ |type|description|
423
+ |----|-----------|
424
+ |`Set<a>`|A set containing all elements of both sets|
425
+
426
+ ### Set.**diff**
427
+
428
+ <details disabled>
429
+ <summary tabindex="-1">Added in <code>0.3.0</code></summary>
430
+ No other changes yet.
431
+ </details>
432
+
433
+ ```grain
434
+ diff : (Set<a>, Set<a>) -> Set<a>
435
+ ```
436
+
437
+ Combines two sets into a single set containing only the elements not shared between both sets.
438
+
439
+ Parameters:
440
+
441
+ |param|type|description|
442
+ |-----|----|-----------|
443
+ |`set1`|`Set<a>`|The first set to combine|
444
+ |`set2`|`Set<a>`|The second set to combine|
445
+
446
+ Returns:
447
+
448
+ |type|description|
449
+ |----|-----------|
450
+ |`Set<a>`|A set containing only unshared elements from both sets|
451
+
452
+ ### Set.**intersect**
453
+
454
+ <details disabled>
455
+ <summary tabindex="-1">Added in <code>0.3.0</code></summary>
456
+ No other changes yet.
457
+ </details>
458
+
459
+ ```grain
460
+ intersect : (Set<a>, Set<a>) -> Set<a>
461
+ ```
462
+
463
+ Combines two sets into a single set containing only the elements shared between both sets.
464
+
465
+ Parameters:
466
+
467
+ |param|type|description|
468
+ |-----|----|-----------|
469
+ |`set1`|`Set<a>`|The first set to combine|
470
+ |`set2`|`Set<a>`|The second set to combine|
471
+
472
+ Returns:
473
+
474
+ |type|description|
475
+ |----|-----------|
476
+ |`Set<a>`|A set containing only shared elements from both sets|
477
+
478
+ ### Set.**getInternalStats**
479
+
480
+ <details disabled>
481
+ <summary tabindex="-1">Added in <code>0.3.0</code></summary>
482
+ No other changes yet.
483
+ </details>
484
+
485
+ ```grain
486
+ getInternalStats : Set<a> -> (Number, Number)
487
+ ```
488
+
489
+ Provides data representing the internal state state of the set.
490
+
491
+ Parameters:
492
+
493
+ |param|type|description|
494
+ |-----|----|-----------|
495
+ |`set`|`Set<a>`|The set to inspect|
496
+
497
+ Returns:
498
+
499
+ |type|description|
500
+ |----|-----------|
501
+ |`(Number, Number)`|The internal state of the set|
502
+
package/string.gr CHANGED
@@ -382,6 +382,33 @@ export let rec implode = (arr: Array<Char>) => {
382
382
  ret
383
383
  }
384
384
 
385
+ // Helper to get the length in constant time without depending on Array
386
+ primitive arrayLength : Array<a> -> Number = "@array.length"
387
+
388
+ /**
389
+ * Create a string that is the given string reversed.
390
+ *
391
+ * @param string: The string to reverse
392
+ * @returns A string whose characters are in the reverse order of the given string
393
+ *
394
+ * @example String.reverse("olleH") == "Hello"
395
+ *
396
+ * @since v0.4.5
397
+ */
398
+ export let reverse = (string) => {
399
+ let mut arr = explode(string)
400
+ let len = arrayLength(arr)
401
+ let halfLen = len / 2
402
+ for (let mut i = 0; i < halfLen; i += 1) {
403
+ let lastIdx = len - i - 1
404
+ let last = arr[lastIdx]
405
+ let first = arr[i]
406
+ arr[i] = last
407
+ arr[lastIdx] = first
408
+ }
409
+ implode(arr)
410
+ }
411
+
385
412
  /**
386
413
  * Split a string by the given separator.
387
414
  *
@@ -1097,14 +1124,14 @@ let rec encodeAtHelp = (string: String, encoding: Encoding, includeBom: Bool, de
1097
1124
  }
1098
1125
  }
1099
1126
 
1100
- let ret = WasmI32.toGrain(bytes): Bytes
1101
1127
  Memory.decRef(WasmI32.fromGrain(string))
1102
1128
  Memory.decRef(WasmI32.fromGrain(encoding))
1103
1129
  Memory.decRef(WasmI32.fromGrain(includeBom))
1104
- Memory.decRef(WasmI32.fromGrain(dest))
1105
1130
  Memory.decRef(WasmI32.fromGrain(destPos))
1106
1131
  Memory.decRef(WasmI32.fromGrain(encodeAtHelp))
1107
- ret
1132
+
1133
+ // We don't decRef `dest` because we're returning it
1134
+ dest
1108
1135
  }
1109
1136
 
1110
1137
  /**
package/string.md CHANGED
@@ -263,6 +263,37 @@ Examples:
263
263
  String.implode([> 'H', 'e', 'l', 'l', 'o']) == "Hello"
264
264
  ```
265
265
 
266
+ ### String.**reverse**
267
+
268
+ <details disabled>
269
+ <summary tabindex="-1">Added in <code>next</code></summary>
270
+ No other changes yet.
271
+ </details>
272
+
273
+ ```grain
274
+ reverse : String -> String
275
+ ```
276
+
277
+ Create a string that is the given string reversed.
278
+
279
+ Parameters:
280
+
281
+ |param|type|description|
282
+ |-----|----|-----------|
283
+ |`string`|`String`|The string to reverse|
284
+
285
+ Returns:
286
+
287
+ |type|description|
288
+ |----|-----------|
289
+ |`String`|A string whose characters are in the reverse order of the given string|
290
+
291
+ Examples:
292
+
293
+ ```grain
294
+ String.reverse("olleH") == "Hello"
295
+ ```
296
+
266
297
  ### String.**split**
267
298
 
268
299
  ```grain