@grain/stdlib 0.5.13 → 0.6.1

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 (155) hide show
  1. package/CHANGELOG.md +201 -0
  2. package/LICENSE +1 -1
  3. package/README.md +25 -2
  4. package/array.gr +1512 -199
  5. package/array.md +2032 -94
  6. package/bigint.gr +239 -140
  7. package/bigint.md +450 -106
  8. package/buffer.gr +595 -102
  9. package/buffer.md +903 -145
  10. package/bytes.gr +401 -110
  11. package/bytes.md +551 -63
  12. package/char.gr +228 -49
  13. package/char.md +373 -7
  14. package/exception.gr +26 -12
  15. package/exception.md +29 -5
  16. package/float32.gr +130 -109
  17. package/float32.md +185 -57
  18. package/float64.gr +112 -99
  19. package/float64.md +185 -57
  20. package/hash.gr +62 -40
  21. package/hash.md +27 -3
  22. package/int16.gr +430 -0
  23. package/int16.md +618 -0
  24. package/int32.gr +200 -269
  25. package/int32.md +254 -289
  26. package/int64.gr +142 -225
  27. package/int64.md +254 -289
  28. package/int8.gr +511 -0
  29. package/int8.md +786 -0
  30. package/json.gr +2071 -0
  31. package/json.md +646 -0
  32. package/list.gr +120 -68
  33. package/list.md +125 -80
  34. package/map.gr +560 -57
  35. package/map.md +672 -56
  36. package/marshal.gr +239 -227
  37. package/marshal.md +36 -4
  38. package/number.gr +626 -676
  39. package/number.md +738 -153
  40. package/option.gr +33 -35
  41. package/option.md +58 -42
  42. package/package.json +2 -2
  43. package/path.gr +148 -187
  44. package/path.md +47 -96
  45. package/pervasives.gr +75 -416
  46. package/pervasives.md +85 -180
  47. package/priorityqueue.gr +433 -74
  48. package/priorityqueue.md +422 -54
  49. package/queue.gr +362 -80
  50. package/queue.md +433 -38
  51. package/random.gr +67 -75
  52. package/random.md +68 -40
  53. package/range.gr +135 -63
  54. package/range.md +198 -43
  55. package/rational.gr +284 -0
  56. package/rational.md +545 -0
  57. package/regex.gr +933 -1066
  58. package/regex.md +59 -60
  59. package/result.gr +23 -25
  60. package/result.md +54 -39
  61. package/runtime/atof/common.gr +78 -82
  62. package/runtime/atof/common.md +22 -10
  63. package/runtime/atof/decimal.gr +102 -127
  64. package/runtime/atof/decimal.md +28 -7
  65. package/runtime/atof/lemire.gr +56 -71
  66. package/runtime/atof/lemire.md +9 -1
  67. package/runtime/atof/parse.gr +83 -110
  68. package/runtime/atof/parse.md +12 -2
  69. package/runtime/atof/slow.gr +28 -35
  70. package/runtime/atof/slow.md +9 -1
  71. package/runtime/atof/table.gr +19 -18
  72. package/runtime/atof/table.md +10 -2
  73. package/runtime/atoi/parse.gr +153 -136
  74. package/runtime/atoi/parse.md +50 -1
  75. package/runtime/bigint.gr +410 -517
  76. package/runtime/bigint.md +71 -57
  77. package/runtime/compare.gr +176 -85
  78. package/runtime/compare.md +31 -1
  79. package/runtime/dataStructures.gr +144 -32
  80. package/runtime/dataStructures.md +267 -31
  81. package/runtime/debugPrint.gr +34 -15
  82. package/runtime/debugPrint.md +37 -5
  83. package/runtime/equal.gr +53 -52
  84. package/runtime/equal.md +30 -1
  85. package/runtime/exception.gr +38 -47
  86. package/runtime/exception.md +10 -8
  87. package/runtime/gc.gr +23 -152
  88. package/runtime/gc.md +13 -17
  89. package/runtime/malloc.gr +31 -31
  90. package/runtime/malloc.md +11 -3
  91. package/runtime/numberUtils.gr +193 -174
  92. package/runtime/numberUtils.md +29 -9
  93. package/runtime/numbers.gr +1695 -1021
  94. package/runtime/numbers.md +1098 -134
  95. package/runtime/string.gr +543 -245
  96. package/runtime/string.md +76 -6
  97. package/runtime/unsafe/constants.gr +30 -13
  98. package/runtime/unsafe/constants.md +80 -0
  99. package/runtime/unsafe/conv.gr +55 -28
  100. package/runtime/unsafe/conv.md +41 -9
  101. package/runtime/unsafe/memory.gr +10 -30
  102. package/runtime/unsafe/memory.md +15 -19
  103. package/runtime/unsafe/tags.gr +37 -21
  104. package/runtime/unsafe/tags.md +88 -8
  105. package/runtime/unsafe/wasmf32.gr +30 -36
  106. package/runtime/unsafe/wasmf32.md +64 -56
  107. package/runtime/unsafe/wasmf64.gr +30 -36
  108. package/runtime/unsafe/wasmf64.md +64 -56
  109. package/runtime/unsafe/wasmi32.gr +49 -66
  110. package/runtime/unsafe/wasmi32.md +102 -94
  111. package/runtime/unsafe/wasmi64.gr +52 -79
  112. package/runtime/unsafe/wasmi64.md +108 -100
  113. package/runtime/utils/printing.gr +13 -15
  114. package/runtime/utils/printing.md +11 -3
  115. package/runtime/wasi.gr +294 -295
  116. package/runtime/wasi.md +62 -42
  117. package/set.gr +574 -64
  118. package/set.md +634 -54
  119. package/stack.gr +181 -64
  120. package/stack.md +271 -42
  121. package/string.gr +453 -533
  122. package/string.md +241 -151
  123. package/uint16.gr +369 -0
  124. package/uint16.md +585 -0
  125. package/uint32.gr +470 -0
  126. package/uint32.md +737 -0
  127. package/uint64.gr +471 -0
  128. package/uint64.md +737 -0
  129. package/uint8.gr +369 -0
  130. package/uint8.md +585 -0
  131. package/uri.gr +1093 -0
  132. package/uri.md +477 -0
  133. package/{sys → wasi}/file.gr +914 -500
  134. package/{sys → wasi}/file.md +454 -50
  135. package/wasi/process.gr +292 -0
  136. package/{sys → wasi}/process.md +164 -6
  137. package/wasi/random.gr +77 -0
  138. package/wasi/random.md +80 -0
  139. package/{sys → wasi}/time.gr +15 -22
  140. package/{sys → wasi}/time.md +5 -5
  141. package/immutablearray.gr +0 -929
  142. package/immutablearray.md +0 -1038
  143. package/immutablemap.gr +0 -493
  144. package/immutablemap.md +0 -479
  145. package/immutablepriorityqueue.gr +0 -360
  146. package/immutablepriorityqueue.md +0 -291
  147. package/immutableset.gr +0 -498
  148. package/immutableset.md +0 -449
  149. package/runtime/debug.gr +0 -2
  150. package/runtime/debug.md +0 -6
  151. package/runtime/unsafe/errors.gr +0 -36
  152. package/runtime/unsafe/errors.md +0 -204
  153. package/sys/process.gr +0 -254
  154. package/sys/random.gr +0 -79
  155. package/sys/random.md +0 -66
package/array.md CHANGED
@@ -4,6 +4,8 @@ title: Array
4
4
 
5
5
  Utilities for working with arrays.
6
6
 
7
+ An immutable array implementation is available in the `Immutable` submodule.
8
+
7
9
  <details>
8
10
  <summary>Added in <code>0.2.0</code></summary>
9
11
  <table>
@@ -18,12 +20,16 @@ Utilities for working with arrays.
18
20
  </details>
19
21
 
20
22
  ```grain
21
- import Array from "array"
23
+ from "array" include Array
24
+ ```
25
+
26
+ ```grain
27
+ [> 1, 2, 3]
22
28
  ```
23
29
 
24
30
  ## Values
25
31
 
26
- Functions for working with the Array data type.
32
+ Functions and constants included in the Array module.
27
33
 
28
34
  ### Array.**length**
29
35
 
@@ -33,7 +39,7 @@ No other changes yet.
33
39
  </details>
34
40
 
35
41
  ```grain
36
- length : Array<a> -> Number
42
+ length : (array: Array<a>) => Number
37
43
  ```
38
44
 
39
45
  Provides the length of the input array.
@@ -64,7 +70,7 @@ No other changes yet.
64
70
  </details>
65
71
 
66
72
  ```grain
67
- make : (Number, a) -> Array<a>
73
+ make : (length: Number, item: a) => Array<a>
68
74
  ```
69
75
 
70
76
  Creates a new array of the specified length with each element being
@@ -93,7 +99,7 @@ Throws:
93
99
  Examples:
94
100
 
95
101
  ```grain
96
- Array.make(5, "foo") // [> "foo", "foo", "foo", "foo", "foo"]
102
+ Array.make(5, "foo") == [> "foo", "foo", "foo", "foo", "foo"]
97
103
  ```
98
104
 
99
105
  ### Array.**init**
@@ -104,7 +110,7 @@ No other changes yet.
104
110
  </details>
105
111
 
106
112
  ```grain
107
- init : (Number, (Number -> a)) -> Array<a>
113
+ init : (length: Number, fn: (Number => a)) => Array<a>
108
114
  ```
109
115
 
110
116
  Creates a new array of the specified length where each element is
@@ -116,7 +122,7 @@ Parameters:
116
122
  |param|type|description|
117
123
  |-----|----|-----------|
118
124
  |`length`|`Number`|The length of the new array|
119
- |`fn`|`Number -> a`|The initializer function to call with each index, where the value returned will be used to initialize the element|
125
+ |`fn`|`Number => a`|The initializer function to call with each index, where the value returned will be used to initialize the element|
120
126
 
121
127
  Returns:
122
128
 
@@ -134,7 +140,7 @@ Throws:
134
140
  Examples:
135
141
 
136
142
  ```grain
137
- Array.init(5, n => n + 3) // [> 3, 4, 5, 6, 7]
143
+ Array.init(5, n => n + 3) == [> 3, 4, 5, 6, 7]
138
144
  ```
139
145
 
140
146
  ### Array.**get**
@@ -152,7 +158,7 @@ Array.init(5, n => n + 3) // [> 3, 4, 5, 6, 7]
152
158
  </details>
153
159
 
154
160
  ```grain
155
- get : (Number, Array<a>) -> a
161
+ get : (index: Number, array: Array<a>) => a
156
162
  ```
157
163
 
158
164
  An alias for normal syntactic array access, i.e. `array[n]`.
@@ -173,10 +179,17 @@ Returns:
173
179
  |----|-----------|
174
180
  |`a`|The element from the array|
175
181
 
182
+ Throws:
183
+
184
+ `IndexOutOfBounds`
185
+
186
+ * When `index` is not an integer
187
+ * When `index` is out of bounds
188
+
176
189
  Examples:
177
190
 
178
191
  ```grain
179
- Array.get(1,[> 1, 2, 3, 4, 5]) == 2
192
+ Array.get(1, [> 1, 2, 3, 4, 5]) == 2
180
193
  ```
181
194
 
182
195
  ### Array.**set**
@@ -194,7 +207,7 @@ Array.get(1,[> 1, 2, 3, 4, 5]) == 2
194
207
  </details>
195
208
 
196
209
  ```grain
197
- set : (Number, a, Array<a>) -> Void
210
+ set : (index: Number, value: a, array: Array<a>) => Void
198
211
  ```
199
212
 
200
213
  An alias for normal syntactic array set, i.e. `array[n] = value`.
@@ -210,10 +223,19 @@ Parameters:
210
223
  |`value`|`a`|The value to store|
211
224
  |`array`|`Array<a>`|The array to update|
212
225
 
226
+ Throws:
227
+
228
+ `IndexOutOfBounds`
229
+
230
+ * When `index` is not an integer
231
+ * When `index` is out of bounds
232
+
213
233
  Examples:
214
234
 
215
235
  ```grain
216
- Array.set(1, 9, [> 1, 2, 3, 4, 5]) == [> 1, 9, 3, 4, 5]
236
+ let array = [> 1, 2, 3, 4, 5]
237
+ Array.set(1, 9, array)
238
+ assert array == [> 1, 9, 3, 4, 5]
217
239
  ```
218
240
 
219
241
  ### Array.**append**
@@ -224,7 +246,7 @@ No other changes yet.
224
246
  </details>
225
247
 
226
248
  ```grain
227
- append : (Array<a>, Array<a>) -> Array<a>
249
+ append : (array1: Array<a>, array2: Array<a>) => Array<a>
228
250
  ```
229
251
 
230
252
  Creates a new array with the elements of the first array followed by
@@ -243,6 +265,12 @@ Returns:
243
265
  |----|-----------|
244
266
  |`Array<a>`|The new array containing elements from `array1` followed by elements from `array2`|
245
267
 
268
+ Throws:
269
+
270
+ `InvalidArgument(String)`
271
+
272
+ * When the combined length of the two arrays is not an integer
273
+
246
274
  Examples:
247
275
 
248
276
  ```grain
@@ -257,7 +285,7 @@ No other changes yet.
257
285
  </details>
258
286
 
259
287
  ```grain
260
- concat : List<Array<a>> -> Array<a>
288
+ concat : (arrays: List<Array<a>>) => Array<a>
261
289
  ```
262
290
 
263
291
  Creates a single array containing the elements of all arrays in the
@@ -275,6 +303,12 @@ Returns:
275
303
  |----|-----------|
276
304
  |`Array<a>`|The new array|
277
305
 
306
+ Throws:
307
+
308
+ `InvalidArgument(String)`
309
+
310
+ * When the combined length of all arrays is not an integer
311
+
278
312
  Examples:
279
313
 
280
314
  ```grain
@@ -289,7 +323,7 @@ No other changes yet.
289
323
  </details>
290
324
 
291
325
  ```grain
292
- copy : Array<a> -> Array<a>
326
+ copy : (array: Array<a>) => Array<a>
293
327
  ```
294
328
 
295
329
  Produces a shallow copy of the input array. The new array contains the
@@ -307,6 +341,12 @@ Returns:
307
341
  |----|-----------|
308
342
  |`Array<a>`|The new array containing the elements from the input|
309
343
 
344
+ Examples:
345
+
346
+ ```grain
347
+ Array.copy([> 1, 2, 3]) == [> 1, 2, 3]
348
+ ```
349
+
310
350
  ### Array.**cycle**
311
351
 
312
352
  <details disabled>
@@ -315,7 +355,7 @@ No other changes yet.
315
355
  </details>
316
356
 
317
357
  ```grain
318
- cycle : ((a -> Void), Number, Array<a>) -> Void
358
+ cycle : (fn: (a => Void), n: Number, array: Array<a>) => Void
319
359
  ```
320
360
 
321
361
  Iterates an array a given number of times, calling an iterator function on each element.
@@ -324,10 +364,18 @@ Parameters:
324
364
 
325
365
  |param|type|description|
326
366
  |-----|----|-----------|
327
- |`fn`|`a -> Void`|The iterator function to call with each element|
367
+ |`fn`|`a => Void`|The iterator function to call with each element|
328
368
  |`n`|`Number`|The number of times to iterate the given array|
329
369
  |`array`|`Array<a>`|The array to iterate|
330
370
 
371
+ Examples:
372
+
373
+ ```grain
374
+ let mut str = ""
375
+ Array.cycle(s => str = str ++ s, 2, [> "a", "b", "c"])
376
+ assert str == "abcabc"
377
+ ```
378
+
331
379
  ### Array.**forEach**
332
380
 
333
381
  <details>
@@ -343,7 +391,7 @@ Parameters:
343
391
  </details>
344
392
 
345
393
  ```grain
346
- forEach : ((a -> Void), Array<a>) -> Void
394
+ forEach : (fn: (a => Void), array: Array<a>) => Void
347
395
  ```
348
396
 
349
397
  Iterates an array, calling an iterator function on each element.
@@ -352,9 +400,17 @@ Parameters:
352
400
 
353
401
  |param|type|description|
354
402
  |-----|----|-----------|
355
- |`fn`|`a -> Void`|The iterator function to call with each element|
403
+ |`fn`|`a => Void`|The iterator function to call with each element|
356
404
  |`array`|`Array<a>`|The array to iterate|
357
405
 
406
+ Examples:
407
+
408
+ ```grain
409
+ let mut str = ""
410
+ Array.forEach(s => str = str ++ s, [> "a", "b", "c"])
411
+ assert str == "abc"
412
+ ```
413
+
358
414
  ### Array.**forEachi**
359
415
 
360
416
  <details>
@@ -370,7 +426,7 @@ Parameters:
370
426
  </details>
371
427
 
372
428
  ```grain
373
- forEachi : (((a, Number) -> Void), Array<a>) -> Void
429
+ forEachi : (fn: ((a, Number) => Void), array: Array<a>) => Void
374
430
  ```
375
431
 
376
432
  Iterates an array, calling an iterator function on each element.
@@ -380,9 +436,17 @@ Parameters:
380
436
 
381
437
  |param|type|description|
382
438
  |-----|----|-----------|
383
- |`fn`|`(a, Number) -> Void`|The iterator function to call with each element|
439
+ |`fn`|`(a, Number) => Void`|The iterator function to call with each element|
384
440
  |`array`|`Array<a>`|The array to iterate|
385
441
 
442
+ Examples:
443
+
444
+ ```grain
445
+ let mut str = ""
446
+ Array.forEachi((s, i) => str = str ++ s ++ toString(i), [> "a", "b", "c"])
447
+ assert str == "a0b1c2"
448
+ ```
449
+
386
450
  ### Array.**map**
387
451
 
388
452
  <details>
@@ -398,7 +462,7 @@ Parameters:
398
462
  </details>
399
463
 
400
464
  ```grain
401
- map : ((a -> b), Array<a>) -> Array<b>
465
+ map : (fn: (a => b), array: Array<a>) => Array<b>
402
466
  ```
403
467
 
404
468
  Produces a new array initialized with the results of a mapper function
@@ -408,7 +472,7 @@ Parameters:
408
472
 
409
473
  |param|type|description|
410
474
  |-----|----|-----------|
411
- |`fn`|`a -> b`|The mapper function to call on each element, where the value returned will be used to initialize the element in the new array|
475
+ |`fn`|`a => b`|The mapper function to call on each element, where the value returned will be used to initialize the element in the new array|
412
476
  |`array`|`Array<a>`|The array to iterate|
413
477
 
414
478
  Returns:
@@ -417,6 +481,12 @@ Returns:
417
481
  |----|-----------|
418
482
  |`Array<b>`|The new array with mapped values|
419
483
 
484
+ Examples:
485
+
486
+ ```grain
487
+ Array.map(x => x * 2, [> 1, 2, 3]) == [> 2, 4, 6]
488
+ ```
489
+
420
490
  ### Array.**mapi**
421
491
 
422
492
  <details disabled>
@@ -425,7 +495,7 @@ No other changes yet.
425
495
  </details>
426
496
 
427
497
  ```grain
428
- mapi : (((a, Number) -> b), Array<a>) -> Array<b>
498
+ mapi : (fn: ((a, Number) => b), array: Array<a>) => Array<b>
429
499
  ```
430
500
 
431
501
  Produces a new array initialized with the results of a mapper function
@@ -435,7 +505,7 @@ Parameters:
435
505
 
436
506
  |param|type|description|
437
507
  |-----|----|-----------|
438
- |`fn`|`(a, Number) -> b`|The mapper function to call on each element, where the value returned will be used to initialize the element in the new array|
508
+ |`fn`|`(a, Number) => b`|The mapper function to call on each element, where the value returned will be used to initialize the element in the new array|
439
509
  |`array`|`Array<a>`|The array to iterate|
440
510
 
441
511
  Returns:
@@ -444,6 +514,12 @@ Returns:
444
514
  |----|-----------|
445
515
  |`Array<b>`|The new array with mapped values|
446
516
 
517
+ Examples:
518
+
519
+ ```grain
520
+ Array.mapi((x, i) => (x * 2, i), [> 1, 2, 3]) == [> (2, 0), (4, 1), (6, 2)]
521
+ ```
522
+
447
523
  ### Array.**reduce**
448
524
 
449
525
  <details disabled>
@@ -452,7 +528,7 @@ No other changes yet.
452
528
  </details>
453
529
 
454
530
  ```grain
455
- reduce : (((a, b) -> a), a, Array<b>) -> a
531
+ reduce : (fn: ((a, b) => a), initial: a, array: Array<b>) => a
456
532
  ```
457
533
 
458
534
  Combines all elements of an array using a reducer function,
@@ -467,7 +543,7 @@ Parameters:
467
543
 
468
544
  |param|type|description|
469
545
  |-----|----|-----------|
470
- |`fn`|`(a, b) -> a`|The reducer function to call on each element, where the value returned will be the next accumulator value|
546
+ |`fn`|`(a, b) => a`|The reducer function to call on each element, where the value returned will be the next accumulator value|
471
547
  |`initial`|`a`|The initial value to use for the accumulator on the first iteration|
472
548
  |`array`|`Array<b>`|The array to iterate|
473
549
 
@@ -480,7 +556,11 @@ Returns:
480
556
  Examples:
481
557
 
482
558
  ```grain
483
- Array.reduce((a, b) => a + b, 0, [> 1, 2, 3]) // 6
559
+ Array.reduce((acc, el) => acc + el, 0, [> 1, 2, 3]) == 6
560
+ ```
561
+
562
+ ```grain
563
+ Array.reduce((acc, el) => acc ++ el, "", [> "baz", "bar", "foo"]) == "bazbarfoo"
484
564
  ```
485
565
 
486
566
  ### Array.**reduceRight**
@@ -491,7 +571,7 @@ No other changes yet.
491
571
  </details>
492
572
 
493
573
  ```grain
494
- reduceRight : (((a, b) -> b), b, Array<a>) -> b
574
+ reduceRight : (fn: ((a, b) => b), initial: b, array: Array<a>) => b
495
575
  ```
496
576
 
497
577
  Combines all elements of an array using a reducer function,
@@ -506,7 +586,7 @@ Parameters:
506
586
 
507
587
  |param|type|description|
508
588
  |-----|----|-----------|
509
- |`fn`|`(a, b) -> b`|The reducer function to call on each element, where the value returned will be the next accumulator value|
589
+ |`fn`|`(a, b) => b`|The reducer function to call on each element, where the value returned will be the next accumulator value|
510
590
  |`initial`|`b`|The initial value to use for the accumulator on the first iteration|
511
591
  |`array`|`Array<a>`|The array to iterate|
512
592
 
@@ -519,7 +599,7 @@ Returns:
519
599
  Examples:
520
600
 
521
601
  ```grain
522
- Array.reduceRight((a, b) => b ++ a, "", [> "baz", "bar", "foo"]) // "foobarbaz"
602
+ Array.reduceRight((el, acc) => acc ++ el, "", [> "baz", "bar", "foo"]) == "foobarbaz"
523
603
  ```
524
604
 
525
605
  ### Array.**reducei**
@@ -530,7 +610,7 @@ No other changes yet.
530
610
  </details>
531
611
 
532
612
  ```grain
533
- reducei : (((a, b, Number) -> a), a, Array<b>) -> a
613
+ reducei : (fn: ((a, b, Number) => a), initial: a, array: Array<b>) => a
534
614
  ```
535
615
 
536
616
  Combines all elements of an array using a reducer function,
@@ -546,7 +626,7 @@ Parameters:
546
626
 
547
627
  |param|type|description|
548
628
  |-----|----|-----------|
549
- |`fn`|`(a, b, Number) -> a`|The reducer function to call on each element, where the value returned will be the next accumulator value|
629
+ |`fn`|`(a, b, Number) => a`|The reducer function to call on each element, where the value returned will be the next accumulator value|
550
630
  |`initial`|`a`|The initial value to use for the accumulator on the first iteration|
551
631
  |`array`|`Array<b>`|The array to iterate|
552
632
 
@@ -556,6 +636,19 @@ Returns:
556
636
  |----|-----------|
557
637
  |`a`|The final accumulator returned from `fn`|
558
638
 
639
+ Examples:
640
+
641
+ ```grain
642
+ Array.reducei((acc, el, index) => acc + el + index, 0, [> 1, 2, 3]) == 9
643
+ ```
644
+
645
+ ```grain
646
+ let output = Array.reducei((acc, el, index) => {
647
+ acc ++ el ++ toString(index)
648
+ }, "", [> "baz", "bar", "foo"])
649
+ assert output == "baz0bar1foo2"
650
+ ```
651
+
559
652
  ### Array.**flatMap**
560
653
 
561
654
  <details disabled>
@@ -564,7 +657,7 @@ No other changes yet.
564
657
  </details>
565
658
 
566
659
  ```grain
567
- flatMap : ((b -> Array<a>), Array<b>) -> Array<a>
660
+ flatMap : (fn: (b => Array<a>), array: Array<b>) => Array<a>
568
661
  ```
569
662
 
570
663
  Produces a new array by calling a function on each element
@@ -576,7 +669,7 @@ Parameters:
576
669
 
577
670
  |param|type|description|
578
671
  |-----|----|-----------|
579
- |`fn`|`b -> Array<a>`|The function to be called on each element, where the value returned will be an array that gets appended to the new array|
672
+ |`fn`|`b => Array<a>`|The function to be called on each element, where the value returned will be an array that gets appended to the new array|
580
673
  |`array`|`Array<b>`|The array to iterate|
581
674
 
582
675
  Returns:
@@ -585,6 +678,18 @@ Returns:
585
678
  |----|-----------|
586
679
  |`Array<a>`|The new array|
587
680
 
681
+ Throws:
682
+
683
+ `InvalidArgument(String)`
684
+
685
+ * When the combined length of all arrays is not an integer
686
+
687
+ Examples:
688
+
689
+ ```grain
690
+ Array.flatMap(e => [> 1, e], [> 1, 2, 3]) == [> 1, 1, 1, 2, 1, 3]
691
+ ```
692
+
588
693
  ### Array.**every**
589
694
 
590
695
  <details disabled>
@@ -593,7 +698,7 @@ No other changes yet.
593
698
  </details>
594
699
 
595
700
  ```grain
596
- every : ((a -> Bool), Array<a>) -> Bool
701
+ every : (fn: (a => Bool), array: Array<a>) => Bool
597
702
  ```
598
703
 
599
704
  Checks that the given condition is satisfied for all
@@ -603,14 +708,24 @@ Parameters:
603
708
 
604
709
  |param|type|description|
605
710
  |-----|----|-----------|
606
- |`fn`|`a -> Bool`|The function to call on each element, where the returned value indicates if the element satisfies the condition|
711
+ |`fn`|`a => Bool`|The function to call on each element, where the returned value indicates if the element satisfies the condition|
607
712
  |`array`|`Array<a>`|The array to check|
608
713
 
609
714
  Returns:
610
715
 
611
716
  |type|description|
612
717
  |----|-----------|
613
- |`Bool`|`true` if all elements satify the condition or `false` otherwise|
718
+ |`Bool`|`true` if all elements satisfy the condition or `false` otherwise|
719
+
720
+ Examples:
721
+
722
+ ```grain
723
+ Array.every(e => e % 2 == 0, [> 2, 4, 6]) == true
724
+ ```
725
+
726
+ ```grain
727
+ Array.every(e => e % 2 == 0, [> 2, 4, 7]) == false
728
+ ```
614
729
 
615
730
  ### Array.**some**
616
731
 
@@ -620,7 +735,7 @@ No other changes yet.
620
735
  </details>
621
736
 
622
737
  ```grain
623
- some : ((a -> Bool), Array<a>) -> Bool
738
+ some : (fn: (a => Bool), array: Array<a>) => Bool
624
739
  ```
625
740
 
626
741
  Checks that the given condition is satisfied **at least
@@ -630,14 +745,28 @@ Parameters:
630
745
 
631
746
  |param|type|description|
632
747
  |-----|----|-----------|
633
- |`fn`|`a -> Bool`|The function to call on each element, where the returned value indicates if the element satisfies the condition|
748
+ |`fn`|`a => Bool`|The function to call on each element, where the returned value indicates if the element satisfies the condition|
634
749
  |`array`|`Array<a>`|The array to iterate|
635
750
 
636
751
  Returns:
637
752
 
638
753
  |type|description|
639
754
  |----|-----------|
640
- |`Bool`|`true` if one or more elements satify the condition or `false` otherwise|
755
+ |`Bool`|`true` if one or more elements satisfy the condition or `false` otherwise|
756
+
757
+ Examples:
758
+
759
+ ```grain
760
+ Array.some(e => e % 2 == 0, [> 2, 4, 6]) == true
761
+ ```
762
+
763
+ ```grain
764
+ Array.some(e => e % 2 == 0, [> 2, 4, 7]) == true
765
+ ```
766
+
767
+ ```grain
768
+ Array.some(e => e % 2 == 0, [> 3, 5, 7]) == false
769
+ ```
641
770
 
642
771
  ### Array.**fill**
643
772
 
@@ -647,7 +776,7 @@ No other changes yet.
647
776
  </details>
648
777
 
649
778
  ```grain
650
- fill : (a, Array<a>) -> Void
779
+ fill : (value: a, array: Array<a>) => Void
651
780
  ```
652
781
 
653
782
  Replaces all elements in an array with the new value provided.
@@ -659,6 +788,14 @@ Parameters:
659
788
  |`value`|`a`|The value replacing each element|
660
789
  |`array`|`Array<a>`|The array to update|
661
790
 
791
+ Examples:
792
+
793
+ ```grain
794
+ let arr = [> 2, 4, 6]
795
+ Array.fill(0, arr)
796
+ assert arr == [> 0, 0, 0]
797
+ ```
798
+
662
799
  ### Array.**fillRange**
663
800
 
664
801
  <details disabled>
@@ -667,7 +804,7 @@ No other changes yet.
667
804
  </details>
668
805
 
669
806
  ```grain
670
- fillRange : (a, Number, Number, Array<a>) -> Void
807
+ fillRange : (value: a, start: Number, stop: Number, array: Array<a>) => Void
671
808
  ```
672
809
 
673
810
  Replaces all elements in the provided index range in the array
@@ -682,6 +819,21 @@ Parameters:
682
819
  |`stop`|`Number`|The (exclusive) index to end replacement|
683
820
  |`array`|`Array<a>`|The array to update|
684
821
 
822
+ Throws:
823
+
824
+ `IndexOutOfBounds`
825
+
826
+ * When the start index is out of bounds
827
+ * When the start index is greater then the stop index
828
+
829
+ Examples:
830
+
831
+ ```grain
832
+ let arr = [> 2, 4, 6, 8]
833
+ Array.fillRange(0, 1, 3, arr)
834
+ assert arr == [> 2, 0, 0, 8]
835
+ ```
836
+
685
837
  ### Array.**reverse**
686
838
 
687
839
  <details disabled>
@@ -690,7 +842,7 @@ No other changes yet.
690
842
  </details>
691
843
 
692
844
  ```grain
693
- reverse : Array<a> -> Array<a>
845
+ reverse : (array: Array<a>) => Array<a>
694
846
  ```
695
847
 
696
848
  Creates a new array with all elements in reverse order.
@@ -707,6 +859,12 @@ Returns:
707
859
  |----|-----------|
708
860
  |`Array<a>`|The new array|
709
861
 
862
+ Examples:
863
+
864
+ ```grain
865
+ Array.reverse([> 1, 2, 3]) == [> 3, 2, 1]
866
+ ```
867
+
710
868
  ### Array.**toList**
711
869
 
712
870
  <details disabled>
@@ -715,7 +873,7 @@ No other changes yet.
715
873
  </details>
716
874
 
717
875
  ```grain
718
- toList : Array<a> -> List<a>
876
+ toList : (array: Array<a>) => List<a>
719
877
  ```
720
878
 
721
879
  Converts the input array to a list.
@@ -732,6 +890,12 @@ Returns:
732
890
  |----|-----------|
733
891
  |`List<a>`|The list containing all elements from the array|
734
892
 
893
+ Examples:
894
+
895
+ ```grain
896
+ Array.toList([> 1, 2, 3]) == [1, 2, 3]
897
+ ```
898
+
735
899
  ### Array.**fromList**
736
900
 
737
901
  <details disabled>
@@ -740,7 +904,7 @@ No other changes yet.
740
904
  </details>
741
905
 
742
906
  ```grain
743
- fromList : List<a> -> Array<a>
907
+ fromList : (list: List<a>) => Array<a>
744
908
  ```
745
909
 
746
910
  Converts the input list to an array.
@@ -757,6 +921,12 @@ Returns:
757
921
  |----|-----------|
758
922
  |`Array<a>`|The array containing all elements from the list|
759
923
 
924
+ Examples:
925
+
926
+ ```grain
927
+ Array.fromList([1, 2, 3]) == [> 1, 2, 3]
928
+ ```
929
+
760
930
  ### Array.**contains**
761
931
 
762
932
  <details disabled>
@@ -765,7 +935,7 @@ No other changes yet.
765
935
  </details>
766
936
 
767
937
  ```grain
768
- contains : (a, Array<a>) -> Bool
938
+ contains : (search: a, array: Array<a>) => Bool
769
939
  ```
770
940
 
771
941
  Checks if the value is an element of the input array.
@@ -784,6 +954,16 @@ Returns:
784
954
  |----|-----------|
785
955
  |`Bool`|`true` if the value exists in the array or `false` otherwise|
786
956
 
957
+ Examples:
958
+
959
+ ```grain
960
+ Array.contains(1, [> 1, 2, 3]) == true
961
+ ```
962
+
963
+ ```grain
964
+ Array.contains(0, [> 1, 2, 3]) == false
965
+ ```
966
+
787
967
  ### Array.**find**
788
968
 
789
969
  <details disabled>
@@ -792,16 +972,16 @@ No other changes yet.
792
972
  </details>
793
973
 
794
974
  ```grain
795
- find : ((a -> Bool), Array<a>) -> Option<a>
975
+ find : (fn: (a => Bool), array: Array<a>) => Option<a>
796
976
  ```
797
977
 
798
- Finds the first element in an array that satifies the given condition.
978
+ Finds the first element in an array that satisfies the given condition.
799
979
 
800
980
  Parameters:
801
981
 
802
982
  |param|type|description|
803
983
  |-----|----|-----------|
804
- |`fn`|`a -> Bool`|The function to call on each element, where the returned value indicates if the element satisfies the condition|
984
+ |`fn`|`a => Bool`|The function to call on each element, where the returned value indicates if the element satisfies the condition|
805
985
  |`array`|`Array<a>`|The array to search|
806
986
 
807
987
  Returns:
@@ -810,6 +990,20 @@ Returns:
810
990
  |----|-----------|
811
991
  |`Option<a>`|`Some(element)` containing the first value found or `None` otherwise|
812
992
 
993
+ Examples:
994
+
995
+ ```grain
996
+ Array.find(e => e % 2 == 0, [> 1, 4, 3]) == Some(4)
997
+ ```
998
+
999
+ ```grain
1000
+ Array.find(e => e % 2 == 0, [> 1, 2, 3, 4]) == Some(2)
1001
+ ```
1002
+
1003
+ ```grain
1004
+ Array.find(e => e % 2 == 0, [> 1, 3, 5]) == None
1005
+ ```
1006
+
813
1007
  ### Array.**findIndex**
814
1008
 
815
1009
  <details disabled>
@@ -818,16 +1012,16 @@ No other changes yet.
818
1012
  </details>
819
1013
 
820
1014
  ```grain
821
- findIndex : ((a -> Bool), Array<a>) -> Option<Number>
1015
+ findIndex : (fn: (a => Bool), array: Array<a>) => Option<Number>
822
1016
  ```
823
1017
 
824
- Finds the first index in an array where the element satifies the given condition.
1018
+ Finds the first index in an array where the element satisfies the given condition.
825
1019
 
826
1020
  Parameters:
827
1021
 
828
1022
  |param|type|description|
829
1023
  |-----|----|-----------|
830
- |`fn`|`a -> Bool`|The function to call on each element, where the returned value indicates if the element satisfies the condition|
1024
+ |`fn`|`a => Bool`|The function to call on each element, where the returned value indicates if the element satisfies the condition|
831
1025
  |`array`|`Array<a>`|The array to search|
832
1026
 
833
1027
  Returns:
@@ -836,6 +1030,20 @@ Returns:
836
1030
  |----|-----------|
837
1031
  |`Option<Number>`|`Some(index)` containing the index of the first element found or `None` otherwise|
838
1032
 
1033
+ Examples:
1034
+
1035
+ ```grain
1036
+ Array.findIndex(e => e % 2 == 0, [> 1, 4, 3]) == Some(1)
1037
+ ```
1038
+
1039
+ ```grain
1040
+ Array.findIndex(e => e % 2 == 0, [> 1, 2, 3, 4]) == Some(1)
1041
+ ```
1042
+
1043
+ ```grain
1044
+ Array.findIndex(e => e % 2 == 0, [> 1, 3, 5]) == None
1045
+ ```
1046
+
839
1047
  ### Array.**product**
840
1048
 
841
1049
  <details disabled>
@@ -844,7 +1052,7 @@ No other changes yet.
844
1052
  </details>
845
1053
 
846
1054
  ```grain
847
- product : (Array<a>, Array<b>) -> Array<(a, b)>
1055
+ product : (array1: Array<a>, array2: Array<b>) => Array<(a, b)>
848
1056
  ```
849
1057
 
850
1058
  Combines two arrays into a Cartesian product of tuples containing
@@ -863,6 +1071,18 @@ Returns:
863
1071
  |----|-----------|
864
1072
  |`Array<(a, b)>`|The new array containing all pairs of `(a, b)`|
865
1073
 
1074
+ Throws:
1075
+
1076
+ `InvalidArgument(String)`
1077
+
1078
+ * When the multiplied array lengths are not an integer
1079
+
1080
+ Examples:
1081
+
1082
+ ```grain
1083
+ Array.product([> 1, 2], [> 3, 4]) == [> (1, 3), (1, 4), (2, 3), (2, 4)]
1084
+ ```
1085
+
866
1086
  ### Array.**count**
867
1087
 
868
1088
  <details disabled>
@@ -871,7 +1091,7 @@ No other changes yet.
871
1091
  </details>
872
1092
 
873
1093
  ```grain
874
- count : ((a -> Bool), Array<a>) -> Number
1094
+ count : (fn: (a => Bool), array: Array<a>) => Number
875
1095
  ```
876
1096
 
877
1097
  Counts the number of elements in an array that satisfy the given condition.
@@ -880,7 +1100,7 @@ Parameters:
880
1100
 
881
1101
  |param|type|description|
882
1102
  |-----|----|-----------|
883
- |`fn`|`a -> Bool`|The function to call on each element, where the returned value indicates if the element satisfies the condition|
1103
+ |`fn`|`a => Bool`|The function to call on each element, where the returned value indicates if the element satisfies the condition|
884
1104
  |`array`|`Array<a>`|The array to iterate|
885
1105
 
886
1106
  Returns:
@@ -889,6 +1109,12 @@ Returns:
889
1109
  |----|-----------|
890
1110
  |`Number`|The total number of elements that satisfy the condition|
891
1111
 
1112
+ Examples:
1113
+
1114
+ ```grain
1115
+ Array.count(e => e % 2 == 0, [> 1, 2, 3, 4]) == 2
1116
+ ```
1117
+
892
1118
  ### Array.**counti**
893
1119
 
894
1120
  <details disabled>
@@ -897,7 +1123,7 @@ No other changes yet.
897
1123
  </details>
898
1124
 
899
1125
  ```grain
900
- counti : (((a, Number) -> Bool), Array<a>) -> Number
1126
+ counti : (fn: ((a, Number) => Bool), array: Array<a>) => Number
901
1127
  ```
902
1128
 
903
1129
  Counts the number of elements in an array that satisfy the
@@ -907,7 +1133,7 @@ Parameters:
907
1133
 
908
1134
  |param|type|description|
909
1135
  |-----|----|-----------|
910
- |`fn`|`(a, Number) -> Bool`|The function to call on each element, where the returned value indicates if the element satisfies the condition|
1136
+ |`fn`|`(a, Number) => Bool`|The function to call on each element, where the returned value indicates if the element satisfies the condition|
911
1137
  |`array`|`Array<a>`|The array to iterate|
912
1138
 
913
1139
  Returns:
@@ -916,6 +1142,16 @@ Returns:
916
1142
  |----|-----------|
917
1143
  |`Number`|The total number of elements that satisfy the condition|
918
1144
 
1145
+ Examples:
1146
+
1147
+ ```grain
1148
+ Array.counti((e, i) => e % 2 == 0 && i % 2 == 0, [> 1, 2, 3, 4, 5]) == 0
1149
+ ```
1150
+
1151
+ ```grain
1152
+ Array.counti((e, i) => e % 2 == 0 && i % 2 == 0, [> 0, 1, 2, 3, 5]) == 2
1153
+ ```
1154
+
919
1155
  ### Array.**filter**
920
1156
 
921
1157
  <details disabled>
@@ -924,7 +1160,7 @@ No other changes yet.
924
1160
  </details>
925
1161
 
926
1162
  ```grain
927
- filter : ((a -> Bool), Array<a>) -> Array<a>
1163
+ filter : (fn: (a => Bool), array: Array<a>) => Array<a>
928
1164
  ```
929
1165
 
930
1166
  Produces a new array by calling a function on each element of
@@ -935,7 +1171,7 @@ Parameters:
935
1171
 
936
1172
  |param|type|description|
937
1173
  |-----|----|-----------|
938
- |`fn`|`a -> Bool`|The function to call on each element, where the returned value indicates if the element satisfies the condition|
1174
+ |`fn`|`a => Bool`|The function to call on each element, where the returned value indicates if the element satisfies the condition|
939
1175
  |`array`|`Array<a>`|The array to iterate|
940
1176
 
941
1177
  Returns:
@@ -944,6 +1180,12 @@ Returns:
944
1180
  |----|-----------|
945
1181
  |`Array<a>`|The new array containing elements where `fn` returned `true`|
946
1182
 
1183
+ Examples:
1184
+
1185
+ ```grain
1186
+ Array.filter(e => e % 2 == 0, [> 1, 2, 3, 4]) == [> 2, 4]
1187
+ ```
1188
+
947
1189
  ### Array.**filteri**
948
1190
 
949
1191
  <details disabled>
@@ -952,7 +1194,7 @@ No other changes yet.
952
1194
  </details>
953
1195
 
954
1196
  ```grain
955
- filteri : (((a, Number) -> Bool), Array<a>) -> Array<a>
1197
+ filteri : (fn: ((a, Number) => Bool), array: Array<a>) => Array<a>
956
1198
  ```
957
1199
 
958
1200
  Produces a new array by calling a function on each element of
@@ -963,7 +1205,7 @@ Parameters:
963
1205
 
964
1206
  |param|type|description|
965
1207
  |-----|----|-----------|
966
- |`fn`|`(a, Number) -> Bool`|The function to call on each element, where the returned value indicates if the element satisfies the condition|
1208
+ |`fn`|`(a, Number) => Bool`|The function to call on each element, where the returned value indicates if the element satisfies the condition|
967
1209
  |`array`|`Array<a>`|The array to iterate|
968
1210
 
969
1211
  Returns:
@@ -972,6 +1214,16 @@ Returns:
972
1214
  |----|-----------|
973
1215
  |`Array<a>`|The new array containing elements where `fn` returned `true`|
974
1216
 
1217
+ Examples:
1218
+
1219
+ ```grain
1220
+ Array.filteri((e, i) => e % 2 == 0, [> 1, 2, 3, 4]) == [> 2, 4]
1221
+ ```
1222
+
1223
+ ```grain
1224
+ Array.filteri((e, i) => e % 2 == 1 && i % 2 == 0, [> 1, 2, 3, 4, 5]) == [> 1, 3, 5]
1225
+ ```
1226
+
975
1227
  ### Array.**unique**
976
1228
 
977
1229
  <details disabled>
@@ -980,7 +1232,7 @@ No other changes yet.
980
1232
  </details>
981
1233
 
982
1234
  ```grain
983
- unique : Array<a> -> Array<a>
1235
+ unique : (array: Array<a>) => Array<a>
984
1236
  ```
985
1237
 
986
1238
  Produces a new array with any duplicates removed.
@@ -998,15 +1250,28 @@ Returns:
998
1250
  |----|-----------|
999
1251
  |`Array<a>`|The new array with only unique values|
1000
1252
 
1253
+ Examples:
1254
+
1255
+ ```grain
1256
+ Array.unique([> 1, 2, 1, 2, 3, 1]) == [> 1, 2, 3]
1257
+ ```
1258
+
1001
1259
  ### Array.**zip**
1002
1260
 
1003
- <details disabled>
1004
- <summary tabindex="-1">Added in <code>0.4.0</code></summary>
1005
- No other changes yet.
1261
+ <details>
1262
+ <summary>Added in <code>0.4.0</code></summary>
1263
+ <table>
1264
+ <thead>
1265
+ <tr><th>version</th><th>changes</th></tr>
1266
+ </thead>
1267
+ <tbody>
1268
+ <tr><td><code>0.6.0</code></td><td>Support zipping arrays of different sizes</td></tr>
1269
+ </tbody>
1270
+ </table>
1006
1271
  </details>
1007
1272
 
1008
1273
  ```grain
1009
- zip : (Array<a>, Array<b>) -> Array<(a, b)>
1274
+ zip : (array1: Array<a>, array2: Array<b>) => Array<(a, b)>
1010
1275
  ```
1011
1276
 
1012
1277
  Produces a new array filled with tuples of elements from both given arrays.
@@ -1028,10 +1293,16 @@ Returns:
1028
1293
 
1029
1294
  Throws:
1030
1295
 
1031
- `Failure(String)`
1296
+ `IndexOutOfBounds`
1032
1297
 
1033
1298
  * When the arrays have different sizes
1034
1299
 
1300
+ Examples:
1301
+
1302
+ ```grain
1303
+ Array.zip([> 1, 2, 3], [> 4, 5, 6]) == [> (1, 4), (2, 5), (3, 6)]
1304
+ ```
1305
+
1035
1306
  ### Array.**zipWith**
1036
1307
 
1037
1308
  <details disabled>
@@ -1040,7 +1311,7 @@ No other changes yet.
1040
1311
  </details>
1041
1312
 
1042
1313
  ```grain
1043
- zipWith : (((a, b) -> c), Array<a>, Array<b>) -> Array<c>
1314
+ zipWith : (fn: ((a, b) => c), array1: Array<a>, array2: Array<b>) => Array<c>
1044
1315
  ```
1045
1316
 
1046
1317
  Produces a new array filled with elements defined by applying a function on
@@ -1056,7 +1327,7 @@ Parameters:
1056
1327
 
1057
1328
  |param|type|description|
1058
1329
  |-----|----|-----------|
1059
- |`fn`|`(a, b) -> c`|The function to apply to pairs of elements|
1330
+ |`fn`|`(a, b) => c`|The function to apply to pairs of elements|
1060
1331
  |`array1`|`Array<a>`|The array whose elements will each be passed to the function as the first argument|
1061
1332
  |`array2`|`Array<b>`|The array whose elements will each be passed to the function as the second argument|
1062
1333
 
@@ -1066,14 +1337,20 @@ Returns:
1066
1337
  |----|-----------|
1067
1338
  |`Array<c>`|The new array containing elements derived from applying the function to pairs of input array elements|
1068
1339
 
1340
+ Throws:
1341
+
1342
+ `IndexOutOfBounds`
1343
+
1344
+ * When the arrays have different sizes
1345
+
1069
1346
  Examples:
1070
1347
 
1071
1348
  ```grain
1072
- Array.zipWith((a, b) => a + b, [> 1, 2, 3], [> 4, 5, 6]) // [> 5, 7, 9]
1349
+ Array.zipWith((a, b) => a + b, [> 1, 2, 3], [> 4, 5, 6]) == [> 5, 7, 9]
1073
1350
  ```
1074
1351
 
1075
1352
  ```grain
1076
- Array.zipWith((a, b) => a * b, [> 1, 2, 3], [> 4, 5]) // [> 4, 10]
1353
+ Array.zipWith((a, b) => a * b, [> 1, 2, 3], [> 4, 5]) == [> 4, 10]
1077
1354
  ```
1078
1355
 
1079
1356
  ### Array.**unzip**
@@ -1084,7 +1361,7 @@ No other changes yet.
1084
1361
  </details>
1085
1362
 
1086
1363
  ```grain
1087
- unzip : Array<(a, b)> -> (Array<a>, Array<b>)
1364
+ unzip : (array: Array<(a, b)>) => (Array<a>, Array<b>)
1088
1365
  ```
1089
1366
 
1090
1367
  Produces two arrays by splitting apart an array of tuples.
@@ -1101,6 +1378,12 @@ Returns:
1101
1378
  |----|-----------|
1102
1379
  |`(Array<a>, Array<b>)`|An array containing all elements from the first tuple element, and an array containing all elements from the second tuple element|
1103
1380
 
1381
+ Examples:
1382
+
1383
+ ```grain
1384
+ Array.unzip([> (1, 4), (2, 5), (3, 6)]) == ([> 1, 2, 3], [> 4, 5, 6])
1385
+ ```
1386
+
1104
1387
  ### Array.**join**
1105
1388
 
1106
1389
  <details disabled>
@@ -1109,7 +1392,7 @@ No other changes yet.
1109
1392
  </details>
1110
1393
 
1111
1394
  ```grain
1112
- join : (String, Array<String>) -> String
1395
+ join : (separator: String, items: Array<String>) => String
1113
1396
  ```
1114
1397
 
1115
1398
  Concatenates an array of strings into a single string, separated by a separator string.
@@ -1127,15 +1410,28 @@ Returns:
1127
1410
  |----|-----------|
1128
1411
  |`String`|The concatenated string|
1129
1412
 
1413
+ Examples:
1414
+
1415
+ ```grain
1416
+ Array.join(", ", [> "a", "b", "c"]) == "a, b, c"
1417
+ ```
1418
+
1130
1419
  ### Array.**slice**
1131
1420
 
1132
- <details disabled>
1133
- <summary tabindex="-1">Added in <code>0.4.0</code></summary>
1134
- No other changes yet.
1421
+ <details>
1422
+ <summary>Added in <code>0.4.0</code></summary>
1423
+ <table>
1424
+ <thead>
1425
+ <tr><th>version</th><th>changes</th></tr>
1426
+ </thead>
1427
+ <tbody>
1428
+ <tr><td><code>0.6.0</code></td><td>Default `end` to the Array length</td></tr>
1429
+ </tbody>
1430
+ </table>
1135
1431
  </details>
1136
1432
 
1137
1433
  ```grain
1138
- slice : (Number, Number, Array<a>) -> Array<a>
1434
+ slice : (start: Number, ?end: Number, array: Array<a>) => Array<a>
1139
1435
  ```
1140
1436
 
1141
1437
  Slices an array given zero-based start and end indexes. The value
@@ -1148,8 +1444,8 @@ Parameters:
1148
1444
 
1149
1445
  |param|type|description|
1150
1446
  |-----|----|-----------|
1151
- |`startIndex`|`Number`|The index of the array where the slice will begin (inclusive)|
1152
- |`endIndex`|`Number`|The index of the array where the slice will end (exclusive)|
1447
+ |`start`|`Number`|The index of the array where the slice will begin (inclusive)|
1448
+ |`?end`|`Number`|The index of the array where the slice will end (exclusive)|
1153
1449
  |`array`|`Array<a>`|The array to be sliced|
1154
1450
 
1155
1451
  Returns:
@@ -1158,15 +1454,32 @@ Returns:
1158
1454
  |----|-----------|
1159
1455
  |`Array<a>`|The subset of the array that was sliced|
1160
1456
 
1457
+ Examples:
1458
+
1459
+ ```grain
1460
+ Array.slice(1, end=3, [> 1, 2, 3, 4]) == [> 2, 3]
1461
+ ```
1462
+
1463
+ ```grain
1464
+ Array.slice(1, [> 1, 2, 3, 4]) == [> 2, 3, 4]
1465
+ ```
1466
+
1161
1467
  ### Array.**sort**
1162
1468
 
1163
- <details disabled>
1164
- <summary tabindex="-1">Added in <code>0.4.5</code></summary>
1165
- No other changes yet.
1469
+ <details>
1470
+ <summary>Added in <code>0.4.5</code></summary>
1471
+ <table>
1472
+ <thead>
1473
+ <tr><th>version</th><th>changes</th></tr>
1474
+ </thead>
1475
+ <tbody>
1476
+ <tr><td><code>0.6.0</code></td><td>Made `compare` a default argument</td></tr>
1477
+ </tbody>
1478
+ </table>
1166
1479
  </details>
1167
1480
 
1168
1481
  ```grain
1169
- sort : (((a, a) -> Number), Array<a>) -> Void
1482
+ sort : (?compare: ((num1: a, num2: a) => Number), array: Array<a>) => Void
1170
1483
  ```
1171
1484
 
1172
1485
  Sorts an array in-place.
@@ -1177,24 +1490,40 @@ Parameters:
1177
1490
 
1178
1491
  |param|type|description|
1179
1492
  |-----|----|-----------|
1180
- |`comp`|`(a, a) -> Number`|The comparator function used to indicate sort order|
1493
+ |`?compare`|`(num1: a, num2: a) => Number`|The comparator function used to indicate sort order|
1181
1494
  |`array`|`Array<a>`|The array to be sorted|
1182
1495
 
1183
- ### Array.**rotate**
1496
+ Examples:
1184
1497
 
1185
- <details disabled>
1186
- <summary tabindex="-1">Added in <code>0.4.5</code></summary>
1187
- No other changes yet.
1498
+ ```grain
1499
+ let arr = [> 3, 2, 4, 1]
1500
+ Array.sort(compare=(a, b) => a - b, arr)
1501
+ assert arr == [> 1, 2, 3, 4]
1502
+ ```
1503
+
1504
+ ### Array.**rotate**
1505
+
1506
+ <details>
1507
+ <summary>Added in <code>0.4.5</code></summary>
1508
+ <table>
1509
+ <thead>
1510
+ <tr><th>version</th><th>changes</th></tr>
1511
+ </thead>
1512
+ <tbody>
1513
+ <tr><td><code>0.6.0</code></td><td>Behavior changed from right-rotation to left-rotation</td></tr>
1514
+ </tbody>
1515
+ </table>
1188
1516
  </details>
1189
1517
 
1190
1518
  ```grain
1191
- rotate : (Number, Array<a>) -> Void
1519
+ rotate : (n: Number, arr: Array<a>) => Void
1192
1520
  ```
1193
1521
 
1194
- Rotates array elements by the specified amount to the right, in place.
1522
+ Rotates array elements in place by the specified amount to the left, such
1523
+ that the `n`th element becomes the first in the array.
1195
1524
 
1196
1525
  If value is negative, array elements will be rotated by the
1197
- specified amount to the left. See examples.
1526
+ specified amount to the right. See examples.
1198
1527
 
1199
1528
  Parameters:
1200
1529
 
@@ -1206,10 +1535,1619 @@ Parameters:
1206
1535
  Examples:
1207
1536
 
1208
1537
  ```grain
1209
- let array = [> 1, 2, 3, 4, 5]; rotate(2, arr); arr == [> 4, 5, 1, 2, 3]
1538
+ let array = [> 1, 2, 3, 4, 5]
1539
+ Array.rotate(2, array)
1540
+ assert array == [> 3, 4, 5, 1, 2]
1541
+ ```
1542
+
1543
+ ```grain
1544
+ let array = [> 1, 2, 3, 4, 5]
1545
+ Array.rotate(-1, array)
1546
+ assert array == [> 5, 1, 2, 3, 4]
1547
+ ```
1548
+
1549
+ ### Array.**chunk**
1550
+
1551
+ <details disabled>
1552
+ <summary tabindex="-1">Added in <code>0.6.0</code></summary>
1553
+ No other changes yet.
1554
+ </details>
1555
+
1556
+ ```grain
1557
+ chunk : (chunkSize: Number, arr: Array<a>) => Array<Array<a>>
1558
+ ```
1559
+
1560
+ Splits the given array into chunks of the provided size.
1561
+ If the array cannot be split evenly, the final chunk will contain the remaining elements.
1562
+
1563
+ Parameters:
1564
+
1565
+ |param|type|description|
1566
+ |-----|----|-----------|
1567
+ |`chunkSize`|`Number`|The maximum size of each chunk|
1568
+ |`arr`|`Array<a>`|The array to chunk|
1569
+
1570
+ Returns:
1571
+
1572
+ |type|description|
1573
+ |----|-----------|
1574
+ |`Array<Array<a>>`|An array of chunks|
1575
+
1576
+ Throws:
1577
+
1578
+ `InvalidArgument(String)`
1579
+
1580
+ * When `chunkSize` is not an integer
1581
+ * When `chunkSize` is less than one
1582
+
1583
+ Examples:
1584
+
1585
+ ```grain
1586
+ Array.chunk(2, [> 1, 2, 3, 4, 5]) == [> [> 1, 2], [> 3, 4], [> 5]]
1587
+ ```
1588
+
1589
+ ```grain
1590
+ Array.chunk(2, [> 1, 2, 3, 4]) == [> [> 1, 2], [> 3, 4]]
1591
+ ```
1592
+
1593
+ ## Array.Immutable
1594
+
1595
+ An immutable array implementation.
1596
+
1597
+ <details>
1598
+ <summary>Added in <code>0.6.0</code></summary>
1599
+ <table>
1600
+ <thead>
1601
+ <tr><th>version</th><th>changes</th></tr>
1602
+ </thead>
1603
+ <tbody>
1604
+ <tr><td><code>0.5.4</code></td><td>Originally in `"immutablearray"` module</td></tr>
1605
+ </tbody>
1606
+ </table>
1607
+ </details>
1608
+
1609
+ ```grain
1610
+ use Array.{ module Immutable }
1611
+ ```
1612
+
1613
+ ```grain
1614
+ Array.Immutable.empty
1615
+ ```
1616
+
1617
+ ```grain
1618
+ Immutable.fromList([1, 2, 3, 4, 5])
1619
+ ```
1620
+
1621
+ ### Types
1622
+
1623
+ Type declarations included in the Array.Immutable module.
1624
+
1625
+ #### Array.Immutable.**ImmutableArray**
1626
+
1627
+ ```grain
1628
+ type ImmutableArray<a>
1629
+ ```
1630
+
1631
+ ### Values
1632
+
1633
+ Functions and constants included in the Array.Immutable module.
1634
+
1635
+ #### Array.Immutable.**empty**
1636
+
1637
+ <details>
1638
+ <summary>Added in <code>0.6.0</code></summary>
1639
+ <table>
1640
+ <thead>
1641
+ <tr><th>version</th><th>changes</th></tr>
1642
+ </thead>
1643
+ <tbody>
1644
+ <tr><td><code>0.5.4</code></td><td>Originally in `"immutablearray"` module</td></tr>
1645
+ </tbody>
1646
+ </table>
1647
+ </details>
1648
+
1649
+ ```grain
1650
+ empty : ImmutableArray<a>
1651
+ ```
1652
+
1653
+ An empty array.
1654
+
1655
+ Examples:
1656
+
1657
+ ```grain
1658
+ Array.Immutable.empty == Array.Immutable.fromList([])
1659
+ ```
1660
+
1661
+ #### Array.Immutable.**isEmpty**
1662
+
1663
+ <details>
1664
+ <summary>Added in <code>0.6.0</code></summary>
1665
+ <table>
1666
+ <thead>
1667
+ <tr><th>version</th><th>changes</th></tr>
1668
+ </thead>
1669
+ <tbody>
1670
+ <tr><td><code>0.5.4</code></td><td>Originally in `"immutablearray"` module</td></tr>
1671
+ </tbody>
1672
+ </table>
1673
+ </details>
1674
+
1675
+ ```grain
1676
+ isEmpty : (array: ImmutableArray<a>) => Bool
1677
+ ```
1678
+
1679
+ Determines if the array contains no elements.
1680
+
1681
+ Parameters:
1682
+
1683
+ |param|type|description|
1684
+ |-----|----|-----------|
1685
+ |`array`|`ImmutableArray<a>`|The array to check|
1686
+
1687
+ Returns:
1688
+
1689
+ |type|description|
1690
+ |----|-----------|
1691
+ |`Bool`|`true` if the array is empty and `false` otherwise|
1692
+
1693
+ Examples:
1694
+
1695
+ ```grain
1696
+ use Array.{ module Immutable }
1697
+ assert Immutable.isEmpty(Immutable.fromList([1, 2, 3])) == false
1698
+ ```
1699
+
1700
+ ```grain
1701
+ use Array.{ module Immutable }
1702
+ assert Immutable.isEmpty(Immutable.fromList([])) == true
1703
+ ```
1704
+
1705
+ #### Array.Immutable.**length**
1706
+
1707
+ <details>
1708
+ <summary>Added in <code>0.6.0</code></summary>
1709
+ <table>
1710
+ <thead>
1711
+ <tr><th>version</th><th>changes</th></tr>
1712
+ </thead>
1713
+ <tbody>
1714
+ <tr><td><code>0.5.4</code></td><td>Originally in `"immutablearray"` module</td></tr>
1715
+ </tbody>
1716
+ </table>
1717
+ </details>
1718
+
1719
+ ```grain
1720
+ length : (array: ImmutableArray<a>) => Number
1721
+ ```
1722
+
1723
+ Provides the length of the input array.
1724
+
1725
+ Parameters:
1726
+
1727
+ |param|type|description|
1728
+ |-----|----|-----------|
1729
+ |`array`|`ImmutableArray<a>`|The array to inspect|
1730
+
1731
+ Returns:
1732
+
1733
+ |type|description|
1734
+ |----|-----------|
1735
+ |`Number`|The number of elements in the array|
1736
+
1737
+ Examples:
1738
+
1739
+ ```grain
1740
+ use Array.{ module Immutable }
1741
+ assert Immutable.length(Immutable.fromList([1, 2, 3, 4, 5])) == 5
1742
+ ```
1743
+
1744
+ #### Array.Immutable.**get**
1745
+
1746
+ <details>
1747
+ <summary>Added in <code>0.6.0</code></summary>
1748
+ <table>
1749
+ <thead>
1750
+ <tr><th>version</th><th>changes</th></tr>
1751
+ </thead>
1752
+ <tbody>
1753
+ <tr><td><code>0.5.4</code></td><td>Originally in `"immutablearray"` module</td></tr>
1754
+ </tbody>
1755
+ </table>
1756
+ </details>
1757
+
1758
+ ```grain
1759
+ get : (index: Number, array: ImmutableArray<a>) => a
1760
+ ```
1761
+
1762
+ Retrieves the element from the array at the specified index.
1763
+ A negative index is treated as an offset from the end of the array.
1764
+
1765
+ Parameters:
1766
+
1767
+ |param|type|description|
1768
+ |-----|----|-----------|
1769
+ |`index`|`Number`|The index to access|
1770
+ |`array`|`ImmutableArray<a>`|The array to access|
1771
+
1772
+ Returns:
1773
+
1774
+ |type|description|
1775
+ |----|-----------|
1776
+ |`a`|The element from the array|
1777
+
1778
+ Throws:
1779
+
1780
+ `IndexOutOfBounds`
1781
+
1782
+ * When the index being accessed is outside the array's bounds
1783
+
1784
+ Examples:
1785
+
1786
+ ```grain
1787
+ use Array.{ module Immutable }
1788
+ assert Immutable.get(1, Immutable.fromList([1, 2, 3, 4])) == 2
1789
+ ```
1790
+
1791
+ ```grain
1792
+ use Array.{ module Immutable }
1793
+ assert Immutable.get(-1, Immutable.fromList([1, 2, 3, 4])) == 4
1794
+ ```
1795
+
1796
+ #### Array.Immutable.**set**
1797
+
1798
+ <details>
1799
+ <summary>Added in <code>0.6.0</code></summary>
1800
+ <table>
1801
+ <thead>
1802
+ <tr><th>version</th><th>changes</th></tr>
1803
+ </thead>
1804
+ <tbody>
1805
+ <tr><td><code>0.5.4</code></td><td>Originally in `"immutablearray"` module</td></tr>
1806
+ </tbody>
1807
+ </table>
1808
+ </details>
1809
+
1810
+ ```grain
1811
+ set :
1812
+ (index: Number, value: a, array: ImmutableArray<a>) => ImmutableArray<a>
1813
+ ```
1814
+
1815
+ Creates a new array in which the element at the specified index is set to a
1816
+ new value. A negative index is treated as an offset from the end of the array.
1817
+
1818
+ Parameters:
1819
+
1820
+ |param|type|description|
1821
+ |-----|----|-----------|
1822
+ |`index`|`Number`|The index to update|
1823
+ |`value`|`a`|The value to store|
1824
+ |`array`|`ImmutableArray<a>`|The array to update|
1825
+
1826
+ Returns:
1827
+
1828
+ |type|description|
1829
+ |----|-----------|
1830
+ |`ImmutableArray<a>`|A new array containing the new element at the given index|
1831
+
1832
+ Throws:
1833
+
1834
+ `IndexOutOfBounds`
1835
+
1836
+ * When the index being updated is outside the array's bounds
1837
+
1838
+ Examples:
1839
+
1840
+ ```grain
1841
+ use Array.{ module Immutable }
1842
+ let arr = Immutable.fromList([1, 2, 3, 4, 5])
1843
+ let array = Immutable.set(1, 9, arr)
1844
+ assert arr == Immutable.fromList([1, 2, 3, 4, 5])
1845
+ assert array == Immutable.fromList([1, 9, 3, 4, 5])
1846
+ ```
1847
+
1848
+ #### Array.Immutable.**append**
1849
+
1850
+ <details>
1851
+ <summary>Added in <code>0.6.0</code></summary>
1852
+ <table>
1853
+ <thead>
1854
+ <tr><th>version</th><th>changes</th></tr>
1855
+ </thead>
1856
+ <tbody>
1857
+ <tr><td><code>0.5.4</code></td><td>Originally in `"immutablearray"` module</td></tr>
1858
+ </tbody>
1859
+ </table>
1860
+ </details>
1861
+
1862
+ ```grain
1863
+ append :
1864
+ (array1: ImmutableArray<a>, array2: ImmutableArray<a>) => ImmutableArray<a>
1865
+ ```
1866
+
1867
+ Creates a new array with the elements of the first array followed by
1868
+ the elements of the second array.
1869
+
1870
+ Parameters:
1871
+
1872
+ |param|type|description|
1873
+ |-----|----|-----------|
1874
+ |`array1`|`ImmutableArray<a>`|The array containing elements to appear first|
1875
+ |`array2`|`ImmutableArray<a>`|The array containing elements to appear second|
1876
+
1877
+ Returns:
1878
+
1879
+ |type|description|
1880
+ |----|-----------|
1881
+ |`ImmutableArray<a>`|The new array containing elements from `array1` followed by elements from `array2`|
1882
+
1883
+ Examples:
1884
+
1885
+ ```grain
1886
+ use Array.{ module Immutable }
1887
+ let arr1 = Immutable.fromList([1, 2])
1888
+ let arr2 = Immutable.fromList([3, 4, 5])
1889
+ assert Immutable.append(arr1, arr2) == Immutable.fromList([1, 2, 3, 4, 5])
1890
+ assert arr1 == Immutable.fromList([1, 2])
1891
+ assert arr2 == Immutable.fromList([3, 4, 5])
1892
+ ```
1893
+
1894
+ #### Array.Immutable.**concat**
1895
+
1896
+ <details>
1897
+ <summary>Added in <code>0.6.0</code></summary>
1898
+ <table>
1899
+ <thead>
1900
+ <tr><th>version</th><th>changes</th></tr>
1901
+ </thead>
1902
+ <tbody>
1903
+ <tr><td><code>0.5.4</code></td><td>Originally in `"immutablearray"` module</td></tr>
1904
+ </tbody>
1905
+ </table>
1906
+ </details>
1907
+
1908
+ ```grain
1909
+ concat : (arrays: List<ImmutableArray<a>>) => ImmutableArray<a>
1910
+ ```
1911
+
1912
+ Creates a single array containing the elements of all arrays in the
1913
+ provided list.
1914
+
1915
+ Parameters:
1916
+
1917
+ |param|type|description|
1918
+ |-----|----|-----------|
1919
+ |`arrays`|`List<ImmutableArray<a>>`|A list containing all arrays to combine|
1920
+
1921
+ Returns:
1922
+
1923
+ |type|description|
1924
+ |----|-----------|
1925
+ |`ImmutableArray<a>`|The new array|
1926
+
1927
+ Examples:
1928
+
1929
+ ```grain
1930
+ use Array.{ module Immutable }
1931
+ let arr1 = Immutable.fromList([1, 2])
1932
+ let arr2 = Immutable.fromList([3, 4])
1933
+ let arr3 = Immutable.fromList([5, 6])
1934
+ assert Immutable.concat([arr1, arr2, arr3]) == Immutable.fromList([1, 2, 3, 4, 5, 6])
1935
+ ```
1936
+
1937
+ #### Array.Immutable.**init**
1938
+
1939
+ <details>
1940
+ <summary>Added in <code>0.6.0</code></summary>
1941
+ <table>
1942
+ <thead>
1943
+ <tr><th>version</th><th>changes</th></tr>
1944
+ </thead>
1945
+ <tbody>
1946
+ <tr><td><code>0.5.4</code></td><td>Originally in `"immutablearray"` module</td></tr>
1947
+ </tbody>
1948
+ </table>
1949
+ </details>
1950
+
1951
+ ```grain
1952
+ init : (length: Number, fn: (Number => a)) => ImmutableArray<a>
1953
+ ```
1954
+
1955
+ Creates a new array of the specified length where each element is
1956
+ initialized with the result of an initializer function. The initializer
1957
+ is called with the index of each array element.
1958
+
1959
+ Parameters:
1960
+
1961
+ |param|type|description|
1962
+ |-----|----|-----------|
1963
+ |`length`|`Number`|The length of the new array|
1964
+ |`fn`|`Number => a`|The initializer function to call with each index, where the value returned will be used to initialize the element|
1965
+
1966
+ Returns:
1967
+
1968
+ |type|description|
1969
+ |----|-----------|
1970
+ |`ImmutableArray<a>`|The new array|
1971
+
1972
+ Examples:
1973
+
1974
+ ```grain
1975
+ use Array.{ module Immutable }
1976
+ assert Immutable.init(5, i => i) == Immutable.fromList([0, 1, 2, 3, 4])
1977
+ ```
1978
+
1979
+ ```grain
1980
+ use Array.{ module Immutable }
1981
+ assert Immutable.init(5, i => i + 3) == Immutable.fromList([3, 4, 5, 6, 7])
1982
+ ```
1983
+
1984
+ #### Array.Immutable.**make**
1985
+
1986
+ <details>
1987
+ <summary>Added in <code>0.6.0</code></summary>
1988
+ <table>
1989
+ <thead>
1990
+ <tr><th>version</th><th>changes</th></tr>
1991
+ </thead>
1992
+ <tbody>
1993
+ <tr><td><code>0.5.4</code></td><td>Originally in `"immutablearray"` module</td></tr>
1994
+ </tbody>
1995
+ </table>
1996
+ </details>
1997
+
1998
+ ```grain
1999
+ make : (length: Number, value: a) => ImmutableArray<a>
2000
+ ```
2001
+
2002
+ Creates a new array of the specified length with each element being
2003
+ initialized with the given value.
2004
+
2005
+ Parameters:
2006
+
2007
+ |param|type|description|
2008
+ |-----|----|-----------|
2009
+ |`length`|`Number`|The length of the new array|
2010
+ |`value`|`a`|The value to store at each index|
2011
+
2012
+ Returns:
2013
+
2014
+ |type|description|
2015
+ |----|-----------|
2016
+ |`ImmutableArray<a>`|The new array|
2017
+
2018
+ Examples:
2019
+
2020
+ ```grain
2021
+ use Array.{ module Immutable }
2022
+ assert Immutable.make(5, "🌾") == Immutable.fromList(["🌾", "🌾", "🌾", "🌾", "🌾"])
2023
+ ```
2024
+
2025
+ #### Array.Immutable.**forEach**
2026
+
2027
+ <details>
2028
+ <summary>Added in <code>0.6.0</code></summary>
2029
+ <table>
2030
+ <thead>
2031
+ <tr><th>version</th><th>changes</th></tr>
2032
+ </thead>
2033
+ <tbody>
2034
+ <tr><td><code>0.5.4</code></td><td>Originally in `"immutablearray"` module</td></tr>
2035
+ </tbody>
2036
+ </table>
2037
+ </details>
2038
+
2039
+ ```grain
2040
+ forEach : (fn: (a => Void), array: ImmutableArray<a>) => Void
2041
+ ```
2042
+
2043
+ Iterates an array, calling an iterator function on each element.
2044
+
2045
+ Parameters:
2046
+
2047
+ |param|type|description|
2048
+ |-----|----|-----------|
2049
+ |`fn`|`a => Void`|The iterator function to call with each element|
2050
+ |`array`|`ImmutableArray<a>`|The array to iterate|
2051
+
2052
+ Examples:
2053
+
2054
+ ```grain
2055
+ use Array.{ module Immutable }
2056
+ let arr = Immutable.fromList(["foo", "bar", "baz"])
2057
+ let mut str = ""
2058
+ Immutable.forEach(e => str = str ++ e, arr)
2059
+ assert str == "foobarbaz"
2060
+ ```
2061
+
2062
+ #### Array.Immutable.**cycle**
2063
+
2064
+ <details>
2065
+ <summary>Added in <code>0.6.0</code></summary>
2066
+ <table>
2067
+ <thead>
2068
+ <tr><th>version</th><th>changes</th></tr>
2069
+ </thead>
2070
+ <tbody>
2071
+ <tr><td><code>0.5.4</code></td><td>Originally in `"immutablearray"` module</td></tr>
2072
+ </tbody>
2073
+ </table>
2074
+ </details>
2075
+
2076
+ ```grain
2077
+ cycle : (fn: (a => Void), n: Number, array: ImmutableArray<a>) => Void
2078
+ ```
2079
+
2080
+ Iterates an array a given number of times, calling an iterator function on each element.
2081
+
2082
+ Parameters:
2083
+
2084
+ |param|type|description|
2085
+ |-----|----|-----------|
2086
+ |`fn`|`a => Void`|The iterator function to call with each element|
2087
+ |`n`|`Number`|The number of times to iterate the given array|
2088
+ |`array`|`ImmutableArray<a>`|The array to iterate|
2089
+
2090
+ Examples:
2091
+
2092
+ ```grain
2093
+ use Array.{ module Immutable }
2094
+ let arr = Immutable.fromList(["a", "b", "c"])
2095
+ let mut str = ""
2096
+ Immutable.cycle(e => str = str ++ e, 2, arr)
2097
+ assert str == "abcabc"
2098
+ ```
2099
+
2100
+ #### Array.Immutable.**map**
2101
+
2102
+ <details>
2103
+ <summary>Added in <code>0.6.0</code></summary>
2104
+ <table>
2105
+ <thead>
2106
+ <tr><th>version</th><th>changes</th></tr>
2107
+ </thead>
2108
+ <tbody>
2109
+ <tr><td><code>0.5.4</code></td><td>Originally in `"immutablearray"` module</td></tr>
2110
+ </tbody>
2111
+ </table>
2112
+ </details>
2113
+
2114
+ ```grain
2115
+ map : (fn: (a => b), array: ImmutableArray<a>) => ImmutableArray<b>
2116
+ ```
2117
+
2118
+ Produces a new array initialized with the results of a mapper function
2119
+ called on each element of the input array.
2120
+
2121
+ Parameters:
2122
+
2123
+ |param|type|description|
2124
+ |-----|----|-----------|
2125
+ |`fn`|`a => b`|The mapper function to call on each element, where the value returned will be used to initialize the element in the new array|
2126
+ |`array`|`ImmutableArray<a>`|The array to iterate|
2127
+
2128
+ Returns:
2129
+
2130
+ |type|description|
2131
+ |----|-----------|
2132
+ |`ImmutableArray<b>`|The new array with mapped values|
2133
+
2134
+ Examples:
2135
+
2136
+ ```grain
2137
+ use Array.{ module Immutable }
2138
+ let arr = Immutable.fromList(["foo", "bar", "baz"])
2139
+ let arr = Immutable.map(e => e ++ "_", arr)
2140
+ assert arr == Immutable.fromList(["foo_", "bar_", "baz_"])
2141
+ ```
2142
+
2143
+ #### Array.Immutable.**reduce**
2144
+
2145
+ <details>
2146
+ <summary>Added in <code>0.6.0</code></summary>
2147
+ <table>
2148
+ <thead>
2149
+ <tr><th>version</th><th>changes</th></tr>
2150
+ </thead>
2151
+ <tbody>
2152
+ <tr><td><code>0.5.4</code></td><td>Originally in `"immutablearray"` module</td></tr>
2153
+ </tbody>
2154
+ </table>
2155
+ </details>
2156
+
2157
+ ```grain
2158
+ reduce : (fn: ((a, b) => a), initial: a, array: ImmutableArray<b>) => a
2159
+ ```
2160
+
2161
+ Combines all elements of an array using a reducer function,
2162
+ starting from the "head", or left side, of the array.
2163
+
2164
+ In `ImmutableArray.reduce(fn, initial, array)`, `fn` is called with
2165
+ an accumulator and each element of the array, and returns
2166
+ a new accumulator. The final value is the last accumulator
2167
+ returned. The accumulator starts with value `initial`.
2168
+
2169
+ Parameters:
2170
+
2171
+ |param|type|description|
2172
+ |-----|----|-----------|
2173
+ |`fn`|`(a, b) => a`|The reducer function to call on each element, where the value returned will be the next accumulator value|
2174
+ |`initial`|`a`|The initial value to use for the accumulator on the first iteration|
2175
+ |`array`|`ImmutableArray<b>`|The array to iterate|
2176
+
2177
+ Returns:
2178
+
2179
+ |type|description|
2180
+ |----|-----------|
2181
+ |`a`|The final accumulator returned from `fn`|
2182
+
2183
+ Examples:
2184
+
2185
+ ```grain
2186
+ use Array.{ module Immutable }
2187
+ let arr = Immutable.fromList([1, 2, 3])
2188
+ assert Immutable.reduce((acc, x) => acc + x, 0, arr) == 6
2189
+ ```
2190
+
2191
+ #### Array.Immutable.**reduceRight**
2192
+
2193
+ <details>
2194
+ <summary>Added in <code>0.6.0</code></summary>
2195
+ <table>
2196
+ <thead>
2197
+ <tr><th>version</th><th>changes</th></tr>
2198
+ </thead>
2199
+ <tbody>
2200
+ <tr><td><code>0.5.4</code></td><td>Originally in `"immutablearray"` module</td></tr>
2201
+ </tbody>
2202
+ </table>
2203
+ </details>
2204
+
2205
+ ```grain
2206
+ reduceRight : (fn: ((a, b) => b), initial: b, array: ImmutableArray<a>) => b
2207
+ ```
2208
+
2209
+ Combines all elements of an array using a reducer function,
2210
+ starting from the "end", or right side, of the array.
2211
+
2212
+ In `ImmutableArray.reduceRight(fn, initial, array)`, `fn` is called with
2213
+ each element of the array and an accumulator, and returns
2214
+ a new accumulator. The final value is the last accumulator
2215
+ returned. The accumulator starts with value `initial`.
2216
+
2217
+ Parameters:
2218
+
2219
+ |param|type|description|
2220
+ |-----|----|-----------|
2221
+ |`fn`|`(a, b) => b`|The reducer function to call on each element, where the value returned will be the next accumulator value|
2222
+ |`initial`|`b`|The initial value to use for the accumulator on the first iteration|
2223
+ |`array`|`ImmutableArray<a>`|The array to iterate|
2224
+
2225
+ Returns:
2226
+
2227
+ |type|description|
2228
+ |----|-----------|
2229
+ |`b`|The final accumulator returned from `fn`|
2230
+
2231
+ Examples:
2232
+
2233
+ ```grain
2234
+ use Array.{ module Immutable }
2235
+ let arr = Immutable.fromList(["baz", "bar", "foo"])
2236
+ assert Immutable.reduceRight((x, acc) => acc ++ x, "", arr) == "foobarbaz"
2237
+ ```
2238
+
2239
+ #### Array.Immutable.**flatMap**
2240
+
2241
+ <details>
2242
+ <summary>Added in <code>0.6.0</code></summary>
2243
+ <table>
2244
+ <thead>
2245
+ <tr><th>version</th><th>changes</th></tr>
2246
+ </thead>
2247
+ <tbody>
2248
+ <tr><td><code>0.5.4</code></td><td>Originally in `"immutablearray"` module</td></tr>
2249
+ </tbody>
2250
+ </table>
2251
+ </details>
2252
+
2253
+ ```grain
2254
+ flatMap :
2255
+ (fn: (a => ImmutableArray<b>), array: ImmutableArray<a>) =>
2256
+ ImmutableArray<b>
2257
+ ```
2258
+
2259
+ Produces a new array by calling a function on each element
2260
+ of the input array. Each iteration produces an intermediate
2261
+ array, which are all appended to produce a "flattened" array
2262
+ of all results.
2263
+
2264
+ Parameters:
2265
+
2266
+ |param|type|description|
2267
+ |-----|----|-----------|
2268
+ |`fn`|`a => ImmutableArray<b>`|The function to be called on each element, where the value returned will be an array that gets appended to the new array|
2269
+ |`array`|`ImmutableArray<a>`|The array to iterate|
2270
+
2271
+ Returns:
2272
+
2273
+ |type|description|
2274
+ |----|-----------|
2275
+ |`ImmutableArray<b>`|The new array|
2276
+
2277
+ Examples:
2278
+
2279
+ ```grain
2280
+ use Array.{ module Immutable }
2281
+ let arr = Immutable.fromList([1, 3, 5])
2282
+ let arr = Immutable.flatMap(n => Immutable.fromList([n, n + 1]), arr)
2283
+ assert arr == Immutable.fromList([1, 2, 3, 4, 5, 6])
2284
+ ```
2285
+
2286
+ #### Array.Immutable.**fromList**
2287
+
2288
+ <details>
2289
+ <summary>Added in <code>0.6.0</code></summary>
2290
+ <table>
2291
+ <thead>
2292
+ <tr><th>version</th><th>changes</th></tr>
2293
+ </thead>
2294
+ <tbody>
2295
+ <tr><td><code>0.5.4</code></td><td>Originally in `"immutablearray"` module</td></tr>
2296
+ </tbody>
2297
+ </table>
2298
+ </details>
2299
+
2300
+ ```grain
2301
+ fromList : (list: List<a>) => ImmutableArray<a>
2302
+ ```
2303
+
2304
+ Converts the input list to an array.
2305
+
2306
+ Parameters:
2307
+
2308
+ |param|type|description|
2309
+ |-----|----|-----------|
2310
+ |`list`|`List<a>`|The list to convert|
2311
+
2312
+ Returns:
2313
+
2314
+ |type|description|
2315
+ |----|-----------|
2316
+ |`ImmutableArray<a>`|The array containing all elements from the list|
2317
+
2318
+ Examples:
2319
+
2320
+ ```grain
2321
+ use Array.{ module Immutable }
2322
+ let arr = Immutable.fromList([1, 2, 3])
2323
+ assert Immutable.get(1, arr) == 2
2324
+ ```
2325
+
2326
+ #### Array.Immutable.**toList**
2327
+
2328
+ <details>
2329
+ <summary>Added in <code>0.6.0</code></summary>
2330
+ <table>
2331
+ <thead>
2332
+ <tr><th>version</th><th>changes</th></tr>
2333
+ </thead>
2334
+ <tbody>
2335
+ <tr><td><code>0.5.4</code></td><td>Originally in `"immutablearray"` module</td></tr>
2336
+ </tbody>
2337
+ </table>
2338
+ </details>
2339
+
2340
+ ```grain
2341
+ toList : (array: ImmutableArray<a>) => List<a>
2342
+ ```
2343
+
2344
+ Converts the input array to a list.
2345
+
2346
+ Parameters:
2347
+
2348
+ |param|type|description|
2349
+ |-----|----|-----------|
2350
+ |`array`|`ImmutableArray<a>`|The array to convert|
2351
+
2352
+ Returns:
2353
+
2354
+ |type|description|
2355
+ |----|-----------|
2356
+ |`List<a>`|The list containing all elements from the array|
2357
+
2358
+ Examples:
2359
+
2360
+ ```grain
2361
+ use Array.{ module Immutable }
2362
+ let arr = Immutable.fromList(['a', 'b', 'c'])
2363
+ let arr = Immutable.set(0, 'd', arr)
2364
+ assert Immutable.toList(arr) == ['d', 'b', 'c']
2365
+ ```
2366
+
2367
+ #### Array.Immutable.**filter**
2368
+
2369
+ <details>
2370
+ <summary>Added in <code>0.6.0</code></summary>
2371
+ <table>
2372
+ <thead>
2373
+ <tr><th>version</th><th>changes</th></tr>
2374
+ </thead>
2375
+ <tbody>
2376
+ <tr><td><code>0.5.4</code></td><td>Originally in `"immutablearray"` module</td></tr>
2377
+ </tbody>
2378
+ </table>
2379
+ </details>
2380
+
2381
+ ```grain
2382
+ filter : (fn: (a => Bool), array: ImmutableArray<a>) => ImmutableArray<a>
2383
+ ```
2384
+
2385
+ Produces a new array by calling a function on each element of
2386
+ the input array and only including it in the result array if the element satisfies
2387
+ the condition.
2388
+
2389
+ Parameters:
2390
+
2391
+ |param|type|description|
2392
+ |-----|----|-----------|
2393
+ |`fn`|`a => Bool`|The function to call on each element, where the returned value indicates if the element satisfies the condition|
2394
+ |`array`|`ImmutableArray<a>`|The array to iterate|
2395
+
2396
+ Returns:
2397
+
2398
+ |type|description|
2399
+ |----|-----------|
2400
+ |`ImmutableArray<a>`|The new array containing elements where `fn` returned `true`|
2401
+
2402
+ Examples:
2403
+
2404
+ ```grain
2405
+ use Array.{ module Immutable }
2406
+ let arr = Immutable.fromList(['a', 'a', 'b', 'c'])
2407
+ let arr = Immutable.filter(e => e == 'a', arr)
2408
+ assert Immutable.toList(arr) == ['a', 'a']
2409
+ ```
2410
+
2411
+ #### Array.Immutable.**every**
2412
+
2413
+ <details>
2414
+ <summary>Added in <code>0.6.0</code></summary>
2415
+ <table>
2416
+ <thead>
2417
+ <tr><th>version</th><th>changes</th></tr>
2418
+ </thead>
2419
+ <tbody>
2420
+ <tr><td><code>0.5.4</code></td><td>Originally in `"immutablearray"` module</td></tr>
2421
+ </tbody>
2422
+ </table>
2423
+ </details>
2424
+
2425
+ ```grain
2426
+ every : (fn: (a => Bool), array: ImmutableArray<a>) => Bool
2427
+ ```
2428
+
2429
+ Checks that the given condition is satisfied for all
2430
+ elements in the input array.
2431
+
2432
+ Parameters:
2433
+
2434
+ |param|type|description|
2435
+ |-----|----|-----------|
2436
+ |`fn`|`a => Bool`|The function to call on each element, where the returned value indicates if the element satisfies the condition|
2437
+ |`array`|`ImmutableArray<a>`|The array to check|
2438
+
2439
+ Returns:
2440
+
2441
+ |type|description|
2442
+ |----|-----------|
2443
+ |`Bool`|`true` if all elements satify the condition or `false` otherwise|
2444
+
2445
+ Examples:
2446
+
2447
+ ```grain
2448
+ use Array.{ module Immutable }
2449
+ let arr = Immutable.fromList(['a', 'a'])
2450
+ assert Immutable.every(e => e == 'a', arr) == true
2451
+ ```
2452
+
2453
+ ```grain
2454
+ use Array.{ module Immutable }
2455
+ let arr = Immutable.fromList(['a', 'a', 'b', 'c'])
2456
+ assert Immutable.every(e => e == 'a', arr) == false
2457
+ ```
2458
+
2459
+ #### Array.Immutable.**some**
2460
+
2461
+ <details>
2462
+ <summary>Added in <code>0.6.0</code></summary>
2463
+ <table>
2464
+ <thead>
2465
+ <tr><th>version</th><th>changes</th></tr>
2466
+ </thead>
2467
+ <tbody>
2468
+ <tr><td><code>0.5.4</code></td><td>Originally in `"immutablearray"` module</td></tr>
2469
+ </tbody>
2470
+ </table>
2471
+ </details>
2472
+
2473
+ ```grain
2474
+ some : (fn: (a => Bool), array: ImmutableArray<a>) => Bool
2475
+ ```
2476
+
2477
+ Checks that the given condition is satisfied **at least
2478
+ once** by an element in the input array.
2479
+
2480
+ Parameters:
2481
+
2482
+ |param|type|description|
2483
+ |-----|----|-----------|
2484
+ |`fn`|`a => Bool`|The function to call on each element, where the returned value indicates if the element satisfies the condition|
2485
+ |`array`|`ImmutableArray<a>`|The array to iterate|
2486
+
2487
+ Returns:
2488
+
2489
+ |type|description|
2490
+ |----|-----------|
2491
+ |`Bool`|`true` if one or more elements satisfy the condition or `false` otherwise|
2492
+
2493
+ Examples:
2494
+
2495
+ ```grain
2496
+ use Array.{ module Immutable }
2497
+ let arr = Immutable.fromList(['a', 'a', 'b', 'c'])
2498
+ assert Immutable.every(e => e == 'a', arr) == false
2499
+ ```
2500
+
2501
+ ```grain
2502
+ use Array.{ module Immutable }
2503
+ let arr = Immutable.fromList(['b', 'c'])
2504
+ assert Immutable.some(e => e == 'a', arr) == false
2505
+ ```
2506
+
2507
+ #### Array.Immutable.**reverse**
2508
+
2509
+ <details>
2510
+ <summary>Added in <code>0.6.0</code></summary>
2511
+ <table>
2512
+ <thead>
2513
+ <tr><th>version</th><th>changes</th></tr>
2514
+ </thead>
2515
+ <tbody>
2516
+ <tr><td><code>0.5.4</code></td><td>Originally in `"immutablearray"` module</td></tr>
2517
+ </tbody>
2518
+ </table>
2519
+ </details>
2520
+
2521
+ ```grain
2522
+ reverse : (array: ImmutableArray<a>) => ImmutableArray<a>
2523
+ ```
2524
+
2525
+ Creates a new array with all elements in reverse order.
2526
+
2527
+ Parameters:
2528
+
2529
+ |param|type|description|
2530
+ |-----|----|-----------|
2531
+ |`array`|`ImmutableArray<a>`|The array to reverse|
2532
+
2533
+ Returns:
2534
+
2535
+ |type|description|
2536
+ |----|-----------|
2537
+ |`ImmutableArray<a>`|The new array|
2538
+
2539
+ Examples:
2540
+
2541
+ ```grain
2542
+ use Array.{ module Immutable }
2543
+ let arr = Immutable.fromList(['a', 'b', 'c'])
2544
+ let arr = Immutable.reverse(arr)
2545
+ assert Immutable.toList(arr) == ['c', 'b', 'a']
2546
+ ```
2547
+
2548
+ #### Array.Immutable.**contains**
2549
+
2550
+ <details>
2551
+ <summary>Added in <code>0.6.0</code></summary>
2552
+ <table>
2553
+ <thead>
2554
+ <tr><th>version</th><th>changes</th></tr>
2555
+ </thead>
2556
+ <tbody>
2557
+ <tr><td><code>0.5.4</code></td><td>Originally in `"immutablearray"` module</td></tr>
2558
+ </tbody>
2559
+ </table>
2560
+ </details>
2561
+
2562
+ ```grain
2563
+ contains : (search: a, array: ImmutableArray<a>) => Bool
2564
+ ```
2565
+
2566
+ Checks if the value is an element of the input array.
2567
+ Uses the generic `==` structural equality operator.
2568
+
2569
+ Parameters:
2570
+
2571
+ |param|type|description|
2572
+ |-----|----|-----------|
2573
+ |`search`|`a`|The value to compare|
2574
+ |`array`|`ImmutableArray<a>`|The array to inspect|
2575
+
2576
+ Returns:
2577
+
2578
+ |type|description|
2579
+ |----|-----------|
2580
+ |`Bool`|`true` if the value exists in the array or `false` otherwise|
2581
+
2582
+ Examples:
2583
+
2584
+ ```grain
2585
+ use Array.{ module Immutable }
2586
+ let arr = Immutable.fromList(['a', 'b', 'c'])
2587
+ assert Immutable.contains('a', arr) == true
2588
+ ```
2589
+
2590
+ ```grain
2591
+ use Array.{ module Immutable }
2592
+ let arr = Immutable.fromList(['b', 'c'])
2593
+ assert Immutable.contains('a', arr) == false
2594
+ ```
2595
+
2596
+ #### Array.Immutable.**find**
2597
+
2598
+ <details>
2599
+ <summary>Added in <code>0.6.0</code></summary>
2600
+ <table>
2601
+ <thead>
2602
+ <tr><th>version</th><th>changes</th></tr>
2603
+ </thead>
2604
+ <tbody>
2605
+ <tr><td><code>0.5.4</code></td><td>Originally in `"immutablearray"` module</td></tr>
2606
+ </tbody>
2607
+ </table>
2608
+ </details>
2609
+
2610
+ ```grain
2611
+ find : (fn: (a => Bool), array: ImmutableArray<a>) => Option<a>
2612
+ ```
2613
+
2614
+ Finds the first element in an array that satisfies the given condition.
2615
+
2616
+ Parameters:
2617
+
2618
+ |param|type|description|
2619
+ |-----|----|-----------|
2620
+ |`fn`|`a => Bool`|The function to call on each element, where the returned value indicates if the element satisfies the condition|
2621
+ |`array`|`ImmutableArray<a>`|The array to search|
2622
+
2623
+ Returns:
2624
+
2625
+ |type|description|
2626
+ |----|-----------|
2627
+ |`Option<a>`|`Some(element)` containing the first value found or `None` otherwise|
2628
+
2629
+ Examples:
2630
+
2631
+ ```grain
2632
+ use Array.{ module Immutable }
2633
+ let arr = Immutable.fromList([1, 2, 3])
2634
+ assert Immutable.find(e => e == 2, arr) == Some(2)
2635
+ ```
2636
+
2637
+ ```grain
2638
+ use Array.{ module Immutable }
2639
+ let arr = Immutable.fromList([1, 3])
2640
+ assert Immutable.find(e => e == 2, arr) == None
2641
+ ```
2642
+
2643
+ #### Array.Immutable.**findIndex**
2644
+
2645
+ <details>
2646
+ <summary>Added in <code>0.6.0</code></summary>
2647
+ <table>
2648
+ <thead>
2649
+ <tr><th>version</th><th>changes</th></tr>
2650
+ </thead>
2651
+ <tbody>
2652
+ <tr><td><code>0.5.4</code></td><td>Originally in `"immutablearray"` module</td></tr>
2653
+ </tbody>
2654
+ </table>
2655
+ </details>
2656
+
2657
+ ```grain
2658
+ findIndex : (fn: (a => Bool), array: ImmutableArray<a>) => Option<Number>
2659
+ ```
2660
+
2661
+ Finds the first index in an array where the element satisfies the given condition.
2662
+
2663
+ Parameters:
2664
+
2665
+ |param|type|description|
2666
+ |-----|----|-----------|
2667
+ |`fn`|`a => Bool`|The function to call on each element, where the returned value indicates if the element satisfies the condition|
2668
+ |`array`|`ImmutableArray<a>`|The array to search|
2669
+
2670
+ Returns:
2671
+
2672
+ |type|description|
2673
+ |----|-----------|
2674
+ |`Option<Number>`|`Some(index)` containing the index of the first element found or `None` otherwise|
2675
+
2676
+ Examples:
2677
+
2678
+ ```grain
2679
+ use Array.{ module Immutable }
2680
+ let arr = Immutable.fromList([1, 2, 3])
2681
+ assert Immutable.findIndex(e => e == 2, arr) == Some(1)
2682
+ ```
2683
+
2684
+ ```grain
2685
+ use Array.{ module Immutable }
2686
+ let arr = Immutable.fromList([1, 3])
2687
+ assert Immutable.findIndex(e => e == 2, arr) == None
2688
+ ```
2689
+
2690
+ #### Array.Immutable.**product**
2691
+
2692
+ <details>
2693
+ <summary>Added in <code>0.6.0</code></summary>
2694
+ <table>
2695
+ <thead>
2696
+ <tr><th>version</th><th>changes</th></tr>
2697
+ </thead>
2698
+ <tbody>
2699
+ <tr><td><code>0.5.4</code></td><td>Originally in `"immutablearray"` module</td></tr>
2700
+ </tbody>
2701
+ </table>
2702
+ </details>
2703
+
2704
+ ```grain
2705
+ product :
2706
+ (array1: ImmutableArray<a>, array2: ImmutableArray<b>) =>
2707
+ ImmutableArray<(a, b)>
2708
+ ```
2709
+
2710
+ Combines two arrays into a Cartesian product of tuples containing
2711
+ all ordered pairs `(a, b)`.
2712
+
2713
+ Parameters:
2714
+
2715
+ |param|type|description|
2716
+ |-----|----|-----------|
2717
+ |`array1`|`ImmutableArray<a>`|The array to provide values for the first tuple element|
2718
+ |`array2`|`ImmutableArray<b>`|The array to provide values for the second tuple element|
2719
+
2720
+ Returns:
2721
+
2722
+ |type|description|
2723
+ |----|-----------|
2724
+ |`ImmutableArray<(a, b)>`|The new array containing all pairs of `(a, b)`|
2725
+
2726
+ Examples:
2727
+
2728
+ ```grain
2729
+ use Array.{ module Immutable }
2730
+ let arr1 = Immutable.fromList([1, 2])
2731
+ let arr2 = Immutable.fromList([3, 4])
2732
+ assert Immutable.product(arr1, arr2) == Immutable.fromList([(1, 3), (1, 4), (2, 3), (2, 4)])
2733
+ ```
2734
+
2735
+ #### Array.Immutable.**count**
2736
+
2737
+ <details>
2738
+ <summary>Added in <code>0.6.0</code></summary>
2739
+ <table>
2740
+ <thead>
2741
+ <tr><th>version</th><th>changes</th></tr>
2742
+ </thead>
2743
+ <tbody>
2744
+ <tr><td><code>0.5.4</code></td><td>Originally in `"immutablearray"` module</td></tr>
2745
+ </tbody>
2746
+ </table>
2747
+ </details>
2748
+
2749
+ ```grain
2750
+ count : (fn: (a => Bool), array: ImmutableArray<a>) => Number
2751
+ ```
2752
+
2753
+ Counts the number of elements in an array that satisfy the given condition.
2754
+
2755
+ Parameters:
2756
+
2757
+ |param|type|description|
2758
+ |-----|----|-----------|
2759
+ |`fn`|`a => Bool`|The function to call on each element, where the returned value indicates if the element satisfies the condition|
2760
+ |`array`|`ImmutableArray<a>`|The array to iterate|
2761
+
2762
+ Returns:
2763
+
2764
+ |type|description|
2765
+ |----|-----------|
2766
+ |`Number`|The total number of elements that satisfy the condition|
2767
+
2768
+ Examples:
2769
+
2770
+ ```grain
2771
+ use Array.{ module Immutable }
2772
+ let arr = Immutable.fromList([1, 1, 2, 3, 4])
2773
+ assert Immutable.count(e => e == 1, arr) == 2
2774
+ ```
2775
+
2776
+ #### Array.Immutable.**unique**
2777
+
2778
+ <details>
2779
+ <summary>Added in <code>0.6.0</code></summary>
2780
+ <table>
2781
+ <thead>
2782
+ <tr><th>version</th><th>changes</th></tr>
2783
+ </thead>
2784
+ <tbody>
2785
+ <tr><td><code>0.5.4</code></td><td>Originally in `"immutablearray"` module</td></tr>
2786
+ </tbody>
2787
+ </table>
2788
+ </details>
2789
+
2790
+ ```grain
2791
+ unique : (array: ImmutableArray<a>) => ImmutableArray<a>
2792
+ ```
2793
+
2794
+ Produces a new array with any duplicates removed.
2795
+ Uses the generic `==` structural equality operator.
2796
+
2797
+ Parameters:
2798
+
2799
+ |param|type|description|
2800
+ |-----|----|-----------|
2801
+ |`array`|`ImmutableArray<a>`|The array to filter|
2802
+
2803
+ Returns:
2804
+
2805
+ |type|description|
2806
+ |----|-----------|
2807
+ |`ImmutableArray<a>`|The new array with only unique values|
2808
+
2809
+ Examples:
2810
+
2811
+ ```grain
2812
+ use Array.{ module Immutable }
2813
+ let arr = Immutable.fromList([1, 1, 2, 3, 2, 4])
2814
+ assert Immutable.unique(arr) == Immutable.fromList([1, 2, 3, 4])
2815
+ ```
2816
+
2817
+ #### Array.Immutable.**zip**
2818
+
2819
+ <details>
2820
+ <summary>Added in <code>0.6.0</code></summary>
2821
+ <table>
2822
+ <thead>
2823
+ <tr><th>version</th><th>changes</th></tr>
2824
+ </thead>
2825
+ <tbody>
2826
+ <tr><td><code>0.5.4</code></td><td>Originally in `"immutablearray"` module</td></tr>
2827
+ </tbody>
2828
+ </table>
2829
+ </details>
2830
+
2831
+ ```grain
2832
+ zip :
2833
+ (array1: ImmutableArray<a>, array2: ImmutableArray<b>) =>
2834
+ ImmutableArray<(a, b)>
2835
+ ```
2836
+
2837
+ Produces a new array filled with tuples of elements from both given arrays.
2838
+ The first tuple will contain the first item of each array, the second tuple
2839
+ will contain the second item of each array, and so on.
2840
+
2841
+ Calling this function with arrays of different sizes will cause the returned
2842
+ array to have the length of the smaller array.
2843
+
2844
+ Parameters:
2845
+
2846
+ |param|type|description|
2847
+ |-----|----|-----------|
2848
+ |`array1`|`ImmutableArray<a>`|The array to provide values for the first tuple element|
2849
+ |`array2`|`ImmutableArray<b>`|The array to provide values for the second tuple element|
2850
+
2851
+ Returns:
2852
+
2853
+ |type|description|
2854
+ |----|-----------|
2855
+ |`ImmutableArray<(a, b)>`|The new array containing indexed pairs of `(a, b)`|
2856
+
2857
+ Examples:
2858
+
2859
+ ```grain
2860
+ use Array.{ module Immutable }
2861
+ let arr1 = Immutable.fromList([1, 2, 3])
2862
+ let arr2 = Immutable.fromList([4, 5, 6])
2863
+ assert Immutable.zip(arr1, arr2) == Immutable.fromList([(1, 4), (2, 5), (3, 6)])
2864
+ ```
2865
+
2866
+ #### Array.Immutable.**zipWith**
2867
+
2868
+ <details>
2869
+ <summary>Added in <code>0.6.0</code></summary>
2870
+ <table>
2871
+ <thead>
2872
+ <tr><th>version</th><th>changes</th></tr>
2873
+ </thead>
2874
+ <tbody>
2875
+ <tr><td><code>0.5.4</code></td><td>Originally in `"immutablearray"` module</td></tr>
2876
+ </tbody>
2877
+ </table>
2878
+ </details>
2879
+
2880
+ ```grain
2881
+ zipWith :
2882
+ (fn: ((a, b) => c), array1: ImmutableArray<a>, array2: ImmutableArray<b>) =>
2883
+ ImmutableArray<c>
2884
+ ```
2885
+
2886
+ Produces a new array filled with elements defined by applying a function on
2887
+ pairs from both given arrays. The first element will contain the result of
2888
+ applying the function to the first elements of each array, the second element
2889
+ will contain the result of applying the function to the second elements of
2890
+ each array, and so on.
2891
+
2892
+ Calling this function with arrays of different sizes will cause the returned
2893
+ array to have the length of the smaller array.
2894
+
2895
+ Parameters:
2896
+
2897
+ |param|type|description|
2898
+ |-----|----|-----------|
2899
+ |`fn`|`(a, b) => c`|The function to apply to pairs of elements|
2900
+ |`array1`|`ImmutableArray<a>`|The array whose elements will each be passed to the function as the first argument|
2901
+ |`array2`|`ImmutableArray<b>`|The array whose elements will each be passed to the function as the second argument|
2902
+
2903
+ Returns:
2904
+
2905
+ |type|description|
2906
+ |----|-----------|
2907
+ |`ImmutableArray<c>`|The new array containing elements derived from applying the function to pairs of input array elements|
2908
+
2909
+ Examples:
2910
+
2911
+ ```grain
2912
+ use Array.{ module Immutable }
2913
+ let arr1 = Immutable.fromList([1, 2, 3])
2914
+ let arr2 = Immutable.fromList([4, 5, 6])
2915
+ assert Immutable.zipWith((a, b) => a + b, arr1, arr2) == Immutable.fromList([5, 7, 9])
2916
+ ```
2917
+
2918
+ ```grain
2919
+ use Array.{ module Immutable }
2920
+ let arr1 = Immutable.fromList([1, 2, 3])
2921
+ let arr2 = Immutable.fromList([4, 5, 6])
2922
+ assert Immutable.zipWith((a, b) => a * b, arr1, arr2) == Immutable.fromList([4, 10, 18])
2923
+ ```
2924
+
2925
+ #### Array.Immutable.**unzip**
2926
+
2927
+ <details>
2928
+ <summary>Added in <code>0.6.0</code></summary>
2929
+ <table>
2930
+ <thead>
2931
+ <tr><th>version</th><th>changes</th></tr>
2932
+ </thead>
2933
+ <tbody>
2934
+ <tr><td><code>0.5.4</code></td><td>Originally in `"immutablearray"` module</td></tr>
2935
+ </tbody>
2936
+ </table>
2937
+ </details>
2938
+
2939
+ ```grain
2940
+ unzip :
2941
+ (array: ImmutableArray<(a, b)>) => (ImmutableArray<a>, ImmutableArray<b>)
2942
+ ```
2943
+
2944
+ Produces two arrays by splitting apart an array of tuples.
2945
+
2946
+ Parameters:
2947
+
2948
+ |param|type|description|
2949
+ |-----|----|-----------|
2950
+ |`array`|`ImmutableArray<(a, b)>`|The array of tuples to split|
2951
+
2952
+ Returns:
2953
+
2954
+ |type|description|
2955
+ |----|-----------|
2956
+ |`(ImmutableArray<a>, ImmutableArray<b>)`|An array containing all elements from the first tuple element and an array containing all elements from the second tuple element|
2957
+
2958
+ Examples:
2959
+
2960
+ ```grain
2961
+ use Array.{ module Immutable }
2962
+ let arr1 = Immutable.fromList([(1, 2), (3, 4), (5, 6)])
2963
+ let arr2 = Immutable.fromList([1, 3, 5])
2964
+ let arr3 = Immutable.fromList([2, 4, 6])
2965
+ assert Immutable.unzip(arr1) == (arr2, arr3)
2966
+ ```
2967
+
2968
+ #### Array.Immutable.**join**
2969
+
2970
+ <details>
2971
+ <summary>Added in <code>0.6.0</code></summary>
2972
+ <table>
2973
+ <thead>
2974
+ <tr><th>version</th><th>changes</th></tr>
2975
+ </thead>
2976
+ <tbody>
2977
+ <tr><td><code>0.5.4</code></td><td>Originally in `"immutablearray"` module</td></tr>
2978
+ </tbody>
2979
+ </table>
2980
+ </details>
2981
+
2982
+ ```grain
2983
+ join : (separator: String, array: ImmutableArray<String>) => String
2984
+ ```
2985
+
2986
+ Concatenates an array of strings into a single string, separated by a separator string.
2987
+
2988
+ Parameters:
2989
+
2990
+ |param|type|description|
2991
+ |-----|----|-----------|
2992
+ |`separator`|`String`|The separator to insert between items in the string|
2993
+ |`array`|`ImmutableArray<String>`|The input strings|
2994
+
2995
+ Returns:
2996
+
2997
+ |type|description|
2998
+ |----|-----------|
2999
+ |`String`|The concatenated string|
3000
+
3001
+ Examples:
3002
+
3003
+ ```grain
3004
+ use Array.{ module Immutable }
3005
+ let arr = Immutable.fromList(["a", "b", "c"])
3006
+ assert Immutable.join(", ", arr) == "a, b, c"
3007
+ ```
3008
+
3009
+ #### Array.Immutable.**slice**
3010
+
3011
+ <details>
3012
+ <summary>Added in <code>0.6.0</code></summary>
3013
+ <table>
3014
+ <thead>
3015
+ <tr><th>version</th><th>changes</th></tr>
3016
+ </thead>
3017
+ <tbody>
3018
+ <tr><td><code>0.5.4</code></td><td>Originally in `"immutablearray"` module</td></tr>
3019
+ <tr><td><code>0.6.0</code></td><td>Default `end` to the Array length</td></tr>
3020
+ </tbody>
3021
+ </table>
3022
+ </details>
3023
+
3024
+ ```grain
3025
+ slice :
3026
+ (start: Number, ?end: Number, array: ImmutableArray<a>) =>
3027
+ ImmutableArray<a>
3028
+ ```
3029
+
3030
+ Slices an array given zero-based start and end indexes. The value
3031
+ at the end index will not be included in the result.
3032
+
3033
+ If either index is a negative number, it will be treated as a reverse index from
3034
+ the end of the array.
3035
+
3036
+ Parameters:
3037
+
3038
+ |param|type|description|
3039
+ |-----|----|-----------|
3040
+ |`start`|`Number`|The index of the array where the slice will begin (inclusive)|
3041
+ |`?end`|`Number`|The index of the array where the slice will end (exclusive)|
3042
+ |`array`|`ImmutableArray<a>`|The array to be sliced|
3043
+
3044
+ Returns:
3045
+
3046
+ |type|description|
3047
+ |----|-----------|
3048
+ |`ImmutableArray<a>`|The subset of the array that was sliced|
3049
+
3050
+ Examples:
3051
+
3052
+ ```grain
3053
+ use Array.{ module Immutable }
3054
+ let arr = Immutable.fromList(['a', 'b', 'c'])
3055
+ assert Immutable.slice(0, end=2, arr) == Immutable.fromList(['a', 'b'])
3056
+ ```
3057
+
3058
+ ```grain
3059
+ use Array.{ module Immutable }
3060
+ let arr = Immutable.fromList(['a', 'b', 'c'])
3061
+ assert Immutable.slice(1, end=-1, arr) == Immutable.fromList(['b'])
3062
+ ```
3063
+
3064
+ #### Array.Immutable.**sort**
3065
+
3066
+ <details>
3067
+ <summary>Added in <code>0.6.0</code></summary>
3068
+ <table>
3069
+ <thead>
3070
+ <tr><th>version</th><th>changes</th></tr>
3071
+ </thead>
3072
+ <tbody>
3073
+ <tr><td><code>0.5.4</code></td><td>Originally in `"immutablearray"` module, with `compare` being a required argument</td></tr>
3074
+ </tbody>
3075
+ </table>
3076
+ </details>
3077
+
3078
+ ```grain
3079
+ sort :
3080
+ (?compare: ((num1: a, num2: a) => Number), array: ImmutableArray<a>) =>
3081
+ ImmutableArray<a>
3082
+ ```
3083
+
3084
+ Sorts the given array based on a given comparator function.
3085
+
3086
+ Ordering is calculated using a comparator function which takes two array elements and must return 0 if both are equal, a positive number if the first is greater, and a negative number if the first is smaller.
3087
+
3088
+ Parameters:
3089
+
3090
+ |param|type|description|
3091
+ |-----|----|-----------|
3092
+ |`?compare`|`(num1: a, num2: a) => Number`|The comparator function used to indicate sort order|
3093
+ |`array`|`ImmutableArray<a>`|The array to be sorted|
3094
+
3095
+ Returns:
3096
+
3097
+ |type|description|
3098
+ |----|-----------|
3099
+ |`ImmutableArray<a>`|The sorted array|
3100
+
3101
+ Examples:
3102
+
3103
+ ```grain
3104
+ use Array.{ module Immutable }
3105
+ let arr = Immutable.fromList([2, 3, 1, 4])
3106
+ assert Immutable.sort(compare=(a, b) => a - b, arr) == Immutable.fromList([1, 2, 3, 4])
3107
+ ```
3108
+
3109
+ #### Array.Immutable.**rotate**
3110
+
3111
+ <details>
3112
+ <summary>Added in <code>0.6.0</code></summary>
3113
+ <table>
3114
+ <thead>
3115
+ <tr><th>version</th><th>changes</th></tr>
3116
+ </thead>
3117
+ <tbody>
3118
+ <tr><td><code>0.5.4</code></td><td>Originally in `"immutablearray"` module</td></tr>
3119
+ </tbody>
3120
+ </table>
3121
+ </details>
3122
+
3123
+ ```grain
3124
+ rotate : (n: Number, array: ImmutableArray<a>) => ImmutableArray<a>
3125
+ ```
3126
+
3127
+ Rotates array elements by the specified amount to the left, such that the
3128
+ `n`th element is the first in the new array.
3129
+
3130
+ If value is negative, array elements will be rotated by the
3131
+ specified amount to the right. See examples.
3132
+
3133
+ Parameters:
3134
+
3135
+ |param|type|description|
3136
+ |-----|----|-----------|
3137
+ |`n`|`Number`|The number of elements to rotate by|
3138
+ |`array`|`ImmutableArray<a>`|The array to be rotated|
3139
+
3140
+ Examples:
3141
+
3142
+ ```grain
3143
+ use Array.{ module Immutable }
3144
+ let arr = Immutable.fromList([1, 2, 3, 4, 5])
3145
+ assert Immutable.rotate(2, arr) == Immutable.fromList([3, 4, 5, 1, 2])
1210
3146
  ```
1211
3147
 
1212
3148
  ```grain
1213
- let array = [> 1, 2, 3, 4, 5]; rotate(-1, arr); arr == [> 2, 3, 4, 5, 1]
3149
+ use Array.{ module Immutable }
3150
+ let arr = Immutable.fromList([1, 2, 3, 4, 5])
3151
+ assert Immutable.rotate(-1, arr) == Immutable.fromList([5, 1, 2, 3, 4])
1214
3152
  ```
1215
3153