@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.gr CHANGED
@@ -1,5 +1,9 @@
1
- // Standard library for Set functionality
2
-
1
+ /**
2
+ * @module Set: 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.
3
+ * @example import Set from "set"
4
+ *
5
+ * @since 0.3.0
6
+ */
3
7
  import List from "list"
4
8
  import Array from "array"
5
9
  import { hash } from "hash"
@@ -15,14 +19,28 @@ record Set<k> {
15
19
  }
16
20
 
17
21
  // TODO: This could take an `eq` function to custom comparisons
18
- export let makeSized = (size) => {
19
- let buckets = Array.make(size, None);
22
+ /**
23
+ * 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.
24
+ *
25
+ * @param storageLength: The initial storage length of the set
26
+ * @returns An empty set with the given initial storage length
27
+ *
28
+ * @since 0.3.0
29
+ */
30
+ export let makeSized = (storageLength) => {
31
+ let buckets = Array.make(storageLength, None);
20
32
  {
21
33
  size: 0,
22
34
  buckets
23
35
  }
24
36
  }
25
-
37
+ /**
38
+ * Creates a new, empty set.
39
+ *
40
+ * @returns An empty set
41
+ *
42
+ * @since 0.3.0
43
+ */
26
44
  export let make = () => {
27
45
  makeSized(16)
28
46
  }
@@ -92,6 +110,14 @@ let rec nodeInBucket = (key, node) => {
92
110
  }
93
111
  }
94
112
 
113
+ /**
114
+ * Adds a new value to the set. If the value already exists, nothing happens.
115
+ *
116
+ * @param key: The value to add
117
+ * @param set: The set to update
118
+ *
119
+ * @since 0.3.0
120
+ */
95
121
  export let add = (key, set) => {
96
122
  let buckets = set.buckets;
97
123
  let idx = getBucketIndex(key, buckets)
@@ -116,6 +142,15 @@ export let add = (key, set) => {
116
142
  }
117
143
  }
118
144
 
145
+ /**
146
+ * Determines if the set contains the given value.
147
+ *
148
+ * @param key: The value to search for
149
+ * @param set: The set to search
150
+ * @returns `true` if the set contains the given value or `false` otherwise
151
+ *
152
+ * @since 0.3.0
153
+ */
119
154
  export let contains = (key, set) => {
120
155
  let buckets = set.buckets;
121
156
  let idx = getBucketIndex(key, buckets);
@@ -140,6 +175,14 @@ let rec removeInBucket = (key, node) => {
140
175
  }
141
176
  }
142
177
 
178
+ /**
179
+ * Removes the given value from the set. If the value doesn't exist, nothing happens.
180
+ *
181
+ * @param key: The value to remove
182
+ * @param set: The set to update
183
+ *
184
+ * @since 0.3.0
185
+ */
143
186
  export let remove = (key, set) => {
144
187
  let buckets = set.buckets;
145
188
  let idx = getBucketIndex(key, buckets);
@@ -160,14 +203,37 @@ export let remove = (key, set) => {
160
203
  }
161
204
  }
162
205
 
206
+ /**
207
+ * Returns the number of values within the set.
208
+ *
209
+ * @param set: The set to inspect
210
+ * @returns The number of elements in the set
211
+ *
212
+ * @since 0.3.0
213
+ */
163
214
  export let size = (set) => {
164
215
  set.size
165
216
  }
166
217
 
218
+ /**
219
+ * Determines if the set contains no elements.
220
+ *
221
+ * @param set: The set to inspect
222
+ * @returns `true` if the given set is empty or `false` otherwise
223
+ *
224
+ * @since 0.3.0
225
+ */
167
226
  export let isEmpty = (set) => {
168
227
  size(set) == 0
169
228
  }
170
229
 
230
+ /**
231
+ * Resets the set by removing all values.
232
+ *
233
+ * @param set: The set to reset
234
+ *
235
+ * @since 0.3.0
236
+ */
171
237
  export let clear = (set) => {
172
238
  set.size = 0;
173
239
  let buckets = set.buckets;
@@ -186,6 +252,14 @@ let rec forEachBucket = (fn, node) => {
186
252
  }
187
253
  }
188
254
 
255
+ /**
256
+ * Iterates the set, calling an iterator function on each element.
257
+ *
258
+ * @param fn: The iterator function to call with each element
259
+ * @param set: The set to iterate
260
+ *
261
+ * @since 0.3.0
262
+ */
189
263
  export let forEach = (fn, set) => {
190
264
  let buckets = set.buckets;
191
265
  Array.forEach((bucket) => {
@@ -201,6 +275,16 @@ let rec reduceEachBucket = (fn, node, acc) => {
201
275
  }
202
276
  }
203
277
 
278
+ /**
279
+ * Combines all elements of a set using a reducer function.
280
+ *
281
+ * @param fn: The reducer function to call on each element, where the value returned will be the next accumulator value
282
+ * @param init: The initial value to use for the accumulator on the first iteration
283
+ * @param set: The set to iterate
284
+ * @returns The final accumulator returned from `fn`
285
+ *
286
+ * @since 0.3.0
287
+ */
204
288
  export let reduce = (fn, init, set) => {
205
289
  let buckets = set.buckets;
206
290
  let mut acc = init;
@@ -210,6 +294,14 @@ export let reduce = (fn, init, set) => {
210
294
  acc
211
295
  }
212
296
 
297
+ /**
298
+ * Removes elements from a set where a predicate function returns `false`.
299
+ *
300
+ * @param fn: The predicate function to indicate which elements to remove from the set, where returning `false` indicates the value should be removed
301
+ * @param set: The set to iterate
302
+ *
303
+ * @since 0.3.0
304
+ */
213
305
  export let filter = (predicate, set) => {
214
306
  let keysToRemove = reduce((list, key) =>
215
307
  if (!predicate(key)) {
@@ -222,14 +314,38 @@ export let filter = (predicate, set) => {
222
314
  }, keysToRemove);
223
315
  }
224
316
 
317
+ /**
318
+ * Removes elements from a set where a predicate function returns `true`.
319
+ *
320
+ * @param fn: The predicate function to indicate which elements to remove from the set, where returning `true` indicates the value should be removed
321
+ * @param set: The set to iterate
322
+ *
323
+ * @since 0.3.0
324
+ */
225
325
  export let reject = (predicate, set) => {
226
326
  filter((key) => !predicate(key), set)
227
327
  }
228
328
 
329
+ /**
330
+ * Converts a set into a list of its elements.
331
+ *
332
+ * @param set: The set to convert
333
+ * @returns A list containing all set values
334
+ *
335
+ * @since 0.3.0
336
+ */
229
337
  export let toList = (set) => {
230
338
  reduce((list, key) => [key, ...list], [], set)
231
339
  }
232
340
 
341
+ /**
342
+ * Creates a set from a list.
343
+ *
344
+ * @param list: The list to convert
345
+ * @returns A set containing all list values
346
+ *
347
+ * @since 0.3.0
348
+ */
233
349
  export let fromList = (list) => {
234
350
  let set = make();
235
351
  List.forEach((key) => {
@@ -238,10 +354,26 @@ export let fromList = (list) => {
238
354
  set
239
355
  }
240
356
 
357
+ /**
358
+ * Converts a set into an array of its elements.
359
+ *
360
+ * @param set: The set to convert
361
+ * @returns An array containing all set values
362
+ *
363
+ * @since 0.3.0
364
+ */
241
365
  export let toArray = (set) => {
242
366
  Array.fromList(toList(set))
243
367
  }
244
368
 
369
+ /**
370
+ * Creates a set from an array.
371
+ *
372
+ * @param array: The array to convert
373
+ * @returns A set containing all array values
374
+ *
375
+ * @since 0.3.0
376
+ */
245
377
  export let fromArray = (array) => {
246
378
  let set = make();
247
379
  Array.forEach((key) => {
@@ -250,6 +382,15 @@ export let fromArray = (array) => {
250
382
  set
251
383
  }
252
384
 
385
+ /**
386
+ * Combines two sets into a single set containing all elements from both sets.
387
+ *
388
+ * @param set1: The first set to combine
389
+ * @param set2: The second set to combine
390
+ * @returns A set containing all elements of both sets
391
+ *
392
+ * @since 0.3.0
393
+ */
253
394
  export let union = (set1, set2) => {
254
395
  let set = make();
255
396
  forEach((key) => {
@@ -261,6 +402,15 @@ export let union = (set1, set2) => {
261
402
  set
262
403
  }
263
404
 
405
+ /**
406
+ * Combines two sets into a single set containing only the elements not shared between both sets.
407
+ *
408
+ * @param set1: The first set to combine
409
+ * @param set2: The second set to combine
410
+ * @returns A set containing only unshared elements from both sets
411
+ *
412
+ * @since 0.3.0
413
+ */
264
414
  export let diff = (set1, set2) => {
265
415
  let set = make();
266
416
  forEach((key) => {
@@ -276,6 +426,15 @@ export let diff = (set1, set2) => {
276
426
  set
277
427
  }
278
428
 
429
+ /**
430
+ * Combines two sets into a single set containing only the elements shared between both sets.
431
+ *
432
+ * @param set1: The first set to combine
433
+ * @param set2: The second set to combine
434
+ * @returns A set containing only shared elements from both sets
435
+ *
436
+ * @since 0.3.0
437
+ */
279
438
  export let intersect = (set1, set2) => {
280
439
  let set = make();
281
440
  forEach((key) => {
@@ -293,6 +452,14 @@ export let intersect = (set1, set2) => {
293
452
 
294
453
  // TODO: Should return a Record type instead of a Tuple
295
454
  // Waiting on https://github.com/grain-lang/grain/issues/190
455
+ /**
456
+ * Provides data representing the internal state state of the set.
457
+ *
458
+ * @param set: The set to inspect
459
+ * @returns The internal state of the set
460
+ *
461
+ * @since 0.3.0
462
+ */
296
463
  export let getInternalStats = (set) => {
297
464
  (set.size, Array.length(set.buckets))
298
465
  }