@grain/stdlib 0.5.3 → 0.5.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.
@@ -0,0 +1,479 @@
1
+ ---
2
+ title: ImmutableMap
3
+ ---
4
+
5
+ An ImmutableMap holds key-value pairs. Any value may be used as a key or value. Operations on an ImmutableMap do not mutate the map's internal state.
6
+
7
+ <details disabled>
8
+ <summary tabindex="-1">Added in <code>0.5.4</code></summary>
9
+ No other changes yet.
10
+ </details>
11
+
12
+ ```grain
13
+ import ImmutableMap from "immutablemap"
14
+ ```
15
+
16
+ ## Types
17
+
18
+ Type declarations included in the ImmutableMap module.
19
+
20
+ ### ImmutableMap.**ImmutableMap**
21
+
22
+ ```grain
23
+ type ImmutableMap<k, v>
24
+ ```
25
+
26
+ ## Values
27
+
28
+ Functions and constants for working with ImmutableMaps.
29
+
30
+ ### ImmutableMap.**empty**
31
+
32
+ <details disabled>
33
+ <summary tabindex="-1">Added in <code>0.5.4</code></summary>
34
+ No other changes yet.
35
+ </details>
36
+
37
+ ```grain
38
+ empty : ImmutableMap<a, b>
39
+ ```
40
+
41
+ An empty map
42
+
43
+ ### ImmutableMap.**size**
44
+
45
+ <details disabled>
46
+ <summary tabindex="-1">Added in <code>0.5.4</code></summary>
47
+ No other changes yet.
48
+ </details>
49
+
50
+ ```grain
51
+ size : ImmutableMap<a, b> -> Number
52
+ ```
53
+
54
+ Provides the count of key-value pairs stored within the map.
55
+
56
+ Parameters:
57
+
58
+ |param|type|description|
59
+ |-----|----|-----------|
60
+ |`map`|`ImmutableMap<a, b>`|The map to inspect|
61
+
62
+ Returns:
63
+
64
+ |type|description|
65
+ |----|-----------|
66
+ |`Number`|The count of key-value pairs in the map|
67
+
68
+ ### ImmutableMap.**isEmpty**
69
+
70
+ <details disabled>
71
+ <summary tabindex="-1">Added in <code>0.5.4</code></summary>
72
+ No other changes yet.
73
+ </details>
74
+
75
+ ```grain
76
+ isEmpty : ImmutableMap<a, b> -> Bool
77
+ ```
78
+
79
+ Determines if the map contains no key-value pairs.
80
+
81
+ Parameters:
82
+
83
+ |param|type|description|
84
+ |-----|----|-----------|
85
+ |`map`|`ImmutableMap<a, b>`|The map to inspect|
86
+
87
+ Returns:
88
+
89
+ |type|description|
90
+ |----|-----------|
91
+ |`Bool`|`true` if the given map is empty or `false` otherwise|
92
+
93
+ ### ImmutableMap.**set**
94
+
95
+ <details disabled>
96
+ <summary tabindex="-1">Added in <code>0.5.4</code></summary>
97
+ No other changes yet.
98
+ </details>
99
+
100
+ ```grain
101
+ set : (a, b, ImmutableMap<a, b>) -> ImmutableMap<a, b>
102
+ ```
103
+
104
+ Produces a new map containing a new key-value pair. If the key already exists in the map, the value is replaced.
105
+
106
+ Parameters:
107
+
108
+ |param|type|description|
109
+ |-----|----|-----------|
110
+ |`key`|`a`|The unique key in the map|
111
+ |`value`|`b`|The value to store|
112
+ |`map`|`ImmutableMap<a, b>`|The base map|
113
+
114
+ Returns:
115
+
116
+ |type|description|
117
+ |----|-----------|
118
+ |`ImmutableMap<a, b>`|A new map containing the new key-value pair|
119
+
120
+ ### ImmutableMap.**get**
121
+
122
+ <details disabled>
123
+ <summary tabindex="-1">Added in <code>0.5.4</code></summary>
124
+ No other changes yet.
125
+ </details>
126
+
127
+ ```grain
128
+ get : (a, ImmutableMap<a, b>) -> Option<b>
129
+ ```
130
+
131
+ Retrieves the value for the given key.
132
+
133
+ Parameters:
134
+
135
+ |param|type|description|
136
+ |-----|----|-----------|
137
+ |`key`|`a`|The key to access|
138
+ |`map`|`ImmutableMap<a, b>`|The map to access|
139
+
140
+ Returns:
141
+
142
+ |type|description|
143
+ |----|-----------|
144
+ |`Option<b>`|`Some(value)` if the key exists in the map or `None` otherwise|
145
+
146
+ ### ImmutableMap.**contains**
147
+
148
+ <details disabled>
149
+ <summary tabindex="-1">Added in <code>0.5.4</code></summary>
150
+ No other changes yet.
151
+ </details>
152
+
153
+ ```grain
154
+ contains : (a, ImmutableMap<a, b>) -> Bool
155
+ ```
156
+
157
+ Determines if the map contains the given key. In such a case, it will always contain a value for the given key.
158
+
159
+ Parameters:
160
+
161
+ |param|type|description|
162
+ |-----|----|-----------|
163
+ |`key`|`a`|The key to search for|
164
+ |`map`|`ImmutableMap<a, b>`|The map to search|
165
+
166
+ Returns:
167
+
168
+ |type|description|
169
+ |----|-----------|
170
+ |`Bool`|`true` if the map contains the given key or `false` otherwise|
171
+
172
+ ### ImmutableMap.**remove**
173
+
174
+ <details disabled>
175
+ <summary tabindex="-1">Added in <code>0.5.4</code></summary>
176
+ No other changes yet.
177
+ </details>
178
+
179
+ ```grain
180
+ remove : (a, ImmutableMap<a, b>) -> ImmutableMap<a, b>
181
+ ```
182
+
183
+ Produces a new map without the key-value pair corresponding to the given
184
+ key. If the key doesn't exist in the map, the map will be returned unmodified.
185
+
186
+ Parameters:
187
+
188
+ |param|type|description|
189
+ |-----|----|-----------|
190
+ |`key`|`a`|The key to exclude|
191
+ |`map`|`ImmutableMap<a, b>`|The map to exclude from|
192
+
193
+ Returns:
194
+
195
+ |type|description|
196
+ |----|-----------|
197
+ |`ImmutableMap<a, b>`|A new map without the given key|
198
+
199
+ ### ImmutableMap.**update**
200
+
201
+ <details disabled>
202
+ <summary tabindex="-1">Added in <code>0.5.4</code></summary>
203
+ No other changes yet.
204
+ </details>
205
+
206
+ ```grain
207
+ update :
208
+ (a, (Option<b> -> Option<b>), ImmutableMap<a, b>) -> ImmutableMap<a, b>
209
+ ```
210
+
211
+ Produces a new map by calling an updater function that receives the
212
+ previously stored value as an `Option` and returns the new value to be
213
+ stored as an `Option`. If the key didn't exist previously, the value
214
+ will be `None`. If `None` is returned from the updater function, the
215
+ key-value pair is excluded.
216
+
217
+ Parameters:
218
+
219
+ |param|type|description|
220
+ |-----|----|-----------|
221
+ |`key`|`a`|The unique key in the map|
222
+ |`fn`|`Option<b> -> Option<b>`|The updater function|
223
+ |`map`|`ImmutableMap<a, b>`|The base map|
224
+
225
+ Returns:
226
+
227
+ |type|description|
228
+ |----|-----------|
229
+ |`ImmutableMap<a, b>`|A new map with the value at the given key modified according to the function's output|
230
+
231
+ ### ImmutableMap.**forEach**
232
+
233
+ <details disabled>
234
+ <summary tabindex="-1">Added in <code>0.5.4</code></summary>
235
+ No other changes yet.
236
+ </details>
237
+
238
+ ```grain
239
+ forEach : (((a, b) -> Void), ImmutableMap<a, b>) -> Void
240
+ ```
241
+
242
+ Iterates the map, calling an iterator function with each key and value.
243
+
244
+ Parameters:
245
+
246
+ |param|type|description|
247
+ |-----|----|-----------|
248
+ |`fn`|`(a, b) -> Void`|The iterator function to call with each key and value|
249
+ |`map`|`ImmutableMap<a, b>`|The map to iterate|
250
+
251
+ ### ImmutableMap.**reduce**
252
+
253
+ <details disabled>
254
+ <summary tabindex="-1">Added in <code>0.5.4</code></summary>
255
+ No other changes yet.
256
+ </details>
257
+
258
+ ```grain
259
+ reduce : (((a, b, c) -> a), a, ImmutableMap<b, c>) -> a
260
+ ```
261
+
262
+ Combines all key-value pairs of a map using a reducer function.
263
+
264
+ Parameters:
265
+
266
+ |param|type|description|
267
+ |-----|----|-----------|
268
+ |`fn`|`(a, b, c) -> a`|The reducer function to call on each key and value, where the value returned will be the next accumulator value|
269
+ |`init`|`a`|The initial value to use for the accumulator on the first iteration|
270
+ |`map`|`ImmutableMap<b, c>`|The map to iterate|
271
+
272
+ Returns:
273
+
274
+ |type|description|
275
+ |----|-----------|
276
+ |`a`|The final accumulator returned from `fn`|
277
+
278
+ ### ImmutableMap.**keys**
279
+
280
+ <details disabled>
281
+ <summary tabindex="-1">Added in <code>0.5.4</code></summary>
282
+ No other changes yet.
283
+ </details>
284
+
285
+ ```grain
286
+ keys : ImmutableMap<a, b> -> List<a>
287
+ ```
288
+
289
+ Enumerates all keys in the given map.
290
+
291
+ Parameters:
292
+
293
+ |param|type|description|
294
+ |-----|----|-----------|
295
+ |`map`|`ImmutableMap<a, b>`|The map to enumerate|
296
+
297
+ Returns:
298
+
299
+ |type|description|
300
+ |----|-----------|
301
+ |`List<a>`|A list containing all keys from the given map|
302
+
303
+ ### ImmutableMap.**values**
304
+
305
+ <details disabled>
306
+ <summary tabindex="-1">Added in <code>0.5.4</code></summary>
307
+ No other changes yet.
308
+ </details>
309
+
310
+ ```grain
311
+ values : ImmutableMap<a, b> -> List<b>
312
+ ```
313
+
314
+ Enumerates all values in the given map.
315
+
316
+ Parameters:
317
+
318
+ |param|type|description|
319
+ |-----|----|-----------|
320
+ |`map`|`ImmutableMap<a, b>`|The map to enumerate|
321
+
322
+ Returns:
323
+
324
+ |type|description|
325
+ |----|-----------|
326
+ |`List<b>`|A list containing all values from the given map|
327
+
328
+ ### ImmutableMap.**filter**
329
+
330
+ <details disabled>
331
+ <summary tabindex="-1">Added in <code>0.5.4</code></summary>
332
+ No other changes yet.
333
+ </details>
334
+
335
+ ```grain
336
+ filter : (((a, b) -> Bool), ImmutableMap<a, b>) -> ImmutableMap<a, b>
337
+ ```
338
+
339
+ Produces a new map excluding the key-value pairs where a predicate function returns `false`.
340
+
341
+ Parameters:
342
+
343
+ |param|type|description|
344
+ |-----|----|-----------|
345
+ |`fn`|`(a, b) -> Bool`|The predicate function to indicate which key-value pairs to exclude from the map, where returning `false` indicates the key-value pair should be excluded|
346
+ |`map`|`ImmutableMap<a, b>`|The map to iterate|
347
+
348
+ Returns:
349
+
350
+ |type|description|
351
+ |----|-----------|
352
+ |`ImmutableMap<a, b>`|A new map excluding the key-value pairs not fulfilling the predicate|
353
+
354
+ ### ImmutableMap.**reject**
355
+
356
+ <details disabled>
357
+ <summary tabindex="-1">Added in <code>0.5.4</code></summary>
358
+ No other changes yet.
359
+ </details>
360
+
361
+ ```grain
362
+ reject : (((a, b) -> Bool), ImmutableMap<a, b>) -> ImmutableMap<a, b>
363
+ ```
364
+
365
+ Produces a new map excluding the key-value pairs where a predicate function returns `true`.
366
+
367
+ Parameters:
368
+
369
+ |param|type|description|
370
+ |-----|----|-----------|
371
+ |`fn`|`(a, b) -> Bool`|The predicate function to indicate which key-value pairs to exclude from the map, where returning `true` indicates the key-value pair should be excluded|
372
+ |`map`|`ImmutableMap<a, b>`|The map to iterate|
373
+
374
+ Returns:
375
+
376
+ |type|description|
377
+ |----|-----------|
378
+ |`ImmutableMap<a, b>`|A new map excluding the key-value pairs fulfilling the predicate|
379
+
380
+ ### ImmutableMap.**fromList**
381
+
382
+ <details disabled>
383
+ <summary tabindex="-1">Added in <code>0.5.4</code></summary>
384
+ No other changes yet.
385
+ </details>
386
+
387
+ ```grain
388
+ fromList : List<(a, b)> -> ImmutableMap<a, b>
389
+ ```
390
+
391
+ Creates a map from a list.
392
+
393
+ Parameters:
394
+
395
+ |param|type|description|
396
+ |-----|----|-----------|
397
+ |`list`|`List<(a, b)>`|The list to convert|
398
+
399
+ Returns:
400
+
401
+ |type|description|
402
+ |----|-----------|
403
+ |`ImmutableMap<a, b>`|A map containing all key-value pairs from the list|
404
+
405
+ ### ImmutableMap.**toList**
406
+
407
+ <details disabled>
408
+ <summary tabindex="-1">Added in <code>0.5.4</code></summary>
409
+ No other changes yet.
410
+ </details>
411
+
412
+ ```grain
413
+ toList : ImmutableMap<a, b> -> List<(a, b)>
414
+ ```
415
+
416
+ Enumerates all key-value pairs in the given map.
417
+
418
+ Parameters:
419
+
420
+ |param|type|description|
421
+ |-----|----|-----------|
422
+ |`map`|`ImmutableMap<a, b>`|The map to enumerate|
423
+
424
+ Returns:
425
+
426
+ |type|description|
427
+ |----|-----------|
428
+ |`List<(a, b)>`|A list containing all key-value pairs from the given map|
429
+
430
+ ### ImmutableMap.**fromArray**
431
+
432
+ <details disabled>
433
+ <summary tabindex="-1">Added in <code>0.5.4</code></summary>
434
+ No other changes yet.
435
+ </details>
436
+
437
+ ```grain
438
+ fromArray : Array<(a, b)> -> ImmutableMap<a, b>
439
+ ```
440
+
441
+ Creates a map from an array.
442
+
443
+ Parameters:
444
+
445
+ |param|type|description|
446
+ |-----|----|-----------|
447
+ |`array`|`Array<(a, b)>`|The array to convert|
448
+
449
+ Returns:
450
+
451
+ |type|description|
452
+ |----|-----------|
453
+ |`ImmutableMap<a, b>`|A map containing all key-value pairs from the array|
454
+
455
+ ### ImmutableMap.**toArray**
456
+
457
+ <details disabled>
458
+ <summary tabindex="-1">Added in <code>0.5.4</code></summary>
459
+ No other changes yet.
460
+ </details>
461
+
462
+ ```grain
463
+ toArray : ImmutableMap<a, b> -> Array<(a, b)>
464
+ ```
465
+
466
+ Converts a map into an array of its key-value pairs.
467
+
468
+ Parameters:
469
+
470
+ |param|type|description|
471
+ |-----|----|-----------|
472
+ |`map`|`ImmutableMap<a, b>`|The map to convert|
473
+
474
+ Returns:
475
+
476
+ |type|description|
477
+ |----|-----------|
478
+ |`Array<(a, b)>`|An array containing all key-value pairs from the given map|
479
+
@@ -7,6 +7,7 @@
7
7
  */
8
8
 
9
9
  import List from "list"
10
+ import Array from "array"
10
11
 
11
12
  // implementation based on immutable skew binomial queue with global root optimization
12
13
  // as described in the paper "Optimal Purely Functional Priority Queues" by Chris Okasaki.
@@ -40,9 +41,19 @@ record ImmutablePriorityQueue<a> {
40
41
  }
41
42
 
42
43
  /**
43
- * @section Values: Functions for working with ImmutablePriorityQueues.
44
+ * @section Values: Functions and constants for working with ImmutablePriorityQueues.
44
45
  */
45
46
 
47
+ /**
48
+ * An empty priority queue with the default `compare` comparator.
49
+ *
50
+ * @since v0.5.4
51
+ */
52
+ export let empty = {
53
+ let empty = { comp: compare, size: 0, root: None }
54
+ empty
55
+ }
56
+
46
57
  /**
47
58
  * Creates a new priority queue with a comparator function, which is used to
48
59
  * determine priority of elements. The comparator function takes two elements
@@ -131,8 +142,8 @@ export let push = (val, pq) => {
131
142
  None => { comp, size: 1, root: Some({ rootVal: val, pq: [] }) },
132
143
  Some({ rootVal, pq }) => {
133
144
  // make the new value the root if it has higher priority than the highest priority value
134
- let (morePriorityVal, lessPriorityVal) = if (comp(val, rootVal) <= 0)
135
- (val, rootVal) else (rootVal, val)
145
+ let (morePriorityVal, lessPriorityVal) =
146
+ if (comp(val, rootVal) <= 0) (val, rootVal) else (rootVal, val)
136
147
 
137
148
  let newRoot = Some(
138
149
  { rootVal: morePriorityVal, pq: skewInsert(comp, lessPriorityVal, pq) }
@@ -161,8 +172,8 @@ export let peek = pq => {
161
172
  let linkNodes = (comp, node1, node2) => {
162
173
  // make the node with higher priority the parent of the node with smaller
163
174
  // priority to presere heap-ordering
164
- let (morePriority, lessPriority) = if (comp(node1.val, node2.val) <= 0)
165
- (node1, node2) else (node2, node1)
175
+ let (morePriority, lessPriority) =
176
+ if (comp(node1.val, node2.val) <= 0) (node1, node2) else (node2, node1)
166
177
  {
167
178
  val: morePriority.val,
168
179
  rank: morePriority.rank + 1,
@@ -330,3 +341,20 @@ export let drain = pq => {
330
341
  export let fromList = (list, comp) => {
331
342
  List.reduce((pq, val) => push(val, pq), make(comp), list)
332
343
  }
344
+
345
+ /**
346
+ * Constructs a new priority queue initialized with the elements in the array
347
+ * using a custom comparator function, which is used to determine priority of
348
+ * elements. The comparator function takes two elements and must return 0 if
349
+ * both share priority, a positive number if the first has greater priority,
350
+ * and a negative number if the first has less priority.
351
+ *
352
+ * @param array: An array of values used to initialize the priority queue
353
+ * @param comp: A comparator function used to assign priority to elements
354
+ * @returns A priority queue containing the elements from the array
355
+ *
356
+ * @since v0.5.4
357
+ */
358
+ export let fromArray = (array, comp) => {
359
+ Array.reduce((pq, val) => push(val, pq), make(comp), array)
360
+ }
@@ -27,7 +27,20 @@ Immutable data structure which maintains a priority order for its elements.
27
27
 
28
28
  ## Values
29
29
 
30
- Functions for working with ImmutablePriorityQueues.
30
+ Functions and constants for working with ImmutablePriorityQueues.
31
+
32
+ ### ImmutablePriorityQueue.**empty**
33
+
34
+ <details disabled>
35
+ <summary tabindex="-1">Added in <code>0.5.4</code></summary>
36
+ No other changes yet.
37
+ </details>
38
+
39
+ ```grain
40
+ empty : ImmutablePriorityQueue<a>
41
+ ```
42
+
43
+ An empty priority queue with the default `compare` comparator.
31
44
 
32
45
  ### ImmutablePriorityQueue.**make**
33
46
 
@@ -246,3 +259,33 @@ Returns:
246
259
  |----|-----------|
247
260
  |`ImmutablePriorityQueue<a>`|A priority queue containing the elements from the list|
248
261
 
262
+ ### ImmutablePriorityQueue.**fromArray**
263
+
264
+ <details disabled>
265
+ <summary tabindex="-1">Added in <code>0.5.4</code></summary>
266
+ No other changes yet.
267
+ </details>
268
+
269
+ ```grain
270
+ fromArray : (Array<a>, ((a, a) -> Number)) -> ImmutablePriorityQueue<a>
271
+ ```
272
+
273
+ Constructs a new priority queue initialized with the elements in the array
274
+ using a custom comparator function, which is used to determine priority of
275
+ elements. The comparator function takes two elements and must return 0 if
276
+ both share priority, a positive number if the first has greater priority,
277
+ and a negative number if the first has less priority.
278
+
279
+ Parameters:
280
+
281
+ |param|type|description|
282
+ |-----|----|-----------|
283
+ |`array`|`Array<a>`|An array of values used to initialize the priority queue|
284
+ |`comp`|`(a, a) -> Number`|A comparator function used to assign priority to elements|
285
+
286
+ Returns:
287
+
288
+ |type|description|
289
+ |----|-----------|
290
+ |`ImmutablePriorityQueue<a>`|A priority queue containing the elements from the array|
291
+