effect 3.2.3 → 3.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/esm/Array.js CHANGED
@@ -18,6 +18,12 @@ import * as Tuple from "./Tuple.js";
18
18
  /**
19
19
  * Builds a `NonEmptyArray` from an non-empty collection of elements.
20
20
  *
21
+ * @example
22
+ * import { Array } from "effect"
23
+ *
24
+ * const result = Array.make(1, 2, 3)
25
+ * assert.deepStrictEqual(result, [1, 2, 3])
26
+ *
21
27
  * @category constructors
22
28
  * @since 2.0.0
23
29
  */
@@ -25,6 +31,12 @@ export const make = (...elements) => elements;
25
31
  /**
26
32
  * Creates a new `Array` of the specified length.
27
33
  *
34
+ * @example
35
+ * import { Array } from "effect"
36
+ *
37
+ * const result = Array.allocate<number>(3)
38
+ * assert.deepStrictEqual(result.length, 3)
39
+ *
28
40
  * @category constructors
29
41
  * @since 2.0.0
30
42
  */
@@ -68,9 +80,9 @@ export const range = (start, end) => start <= end ? makeBy(end - start + 1, i =>
68
80
  * **Note**. `n` is normalized to an integer >= 1.
69
81
  *
70
82
  * @example
71
- * import { replicate } from "effect/Array"
83
+ * import { Array } from "effect"
72
84
  *
73
- * assert.deepStrictEqual(replicate("a", 3), ["a", "a", "a"])
85
+ * assert.deepStrictEqual(Array.replicate("a", 3), ["a", "a", "a"])
74
86
  *
75
87
  * @category constructors
76
88
  * @since 2.0.0
@@ -78,6 +90,15 @@ export const range = (start, end) => start <= end ? makeBy(end - start + 1, i =>
78
90
  export const replicate = /*#__PURE__*/dual(2, (a, n) => makeBy(n, () => a));
79
91
  /**
80
92
  * Creates a new `Array` from an iterable collection of values.
93
+ * If the input is already an array, it returns the input as-is.
94
+ * Otherwise, it converts the iterable collection to an array.
95
+ *
96
+ * @example
97
+ * import { Array } from "effect"
98
+ *
99
+ * const set = new Set([1, 2, 3])
100
+ * const result = Array.fromIterable(set)
101
+ * assert.deepStrictEqual(result, [1, 2, 3])
81
102
  *
82
103
  * @category constructors
83
104
  * @since 2.0.0
@@ -89,21 +110,41 @@ export const fromIterable = collection => Array.isArray(collection) ? collection
89
110
  * @param self - The record to transform.
90
111
  *
91
112
  * @example
92
- * import { fromRecord } from "effect/Array"
113
+ * import { Array } from "effect"
93
114
  *
94
115
  * const x = { a: 1, b: 2, c: 3 }
95
- * assert.deepStrictEqual(fromRecord(x), [["a", 1], ["b", 2], ["c", 3]])
116
+ * assert.deepStrictEqual(Array.fromRecord(x), [["a", 1], ["b", 2], ["c", 3]])
96
117
  *
97
118
  * @category conversions
98
119
  * @since 2.0.0
99
120
  */
100
121
  export const fromRecord = Record.toEntries;
101
122
  /**
123
+ * Converts an `Option` to an array.
124
+ *
125
+ * @example
126
+ * import { Array, Option } from "effect"
127
+ *
128
+ * assert.deepStrictEqual(Array.fromOption(Option.some(1)), [1])
129
+ * assert.deepStrictEqual(Array.fromOption(Option.none()), [])
130
+ *
102
131
  * @category conversions
103
132
  * @since 2.0.0
104
133
  */
105
134
  export const fromOption = O.toArray;
106
135
  /**
136
+ * Matches the elements of an array, applying functions to cases of empty and non-empty arrays.
137
+ *
138
+ * @example
139
+ * import { Array } from "effect"
140
+ *
141
+ * const match = Array.match({
142
+ * onEmpty: () => "empty",
143
+ * onNonEmpty: ([head, ...tail]) => `head: ${head}, tail: ${tail.length}`
144
+ * })
145
+ * assert.deepStrictEqual(match([]), "empty")
146
+ * assert.deepStrictEqual(match([1, 2, 3]), "head: 1, tail: 2")
147
+ *
107
148
  * @category pattern matching
108
149
  * @since 2.0.0
109
150
  */
@@ -112,6 +153,18 @@ export const match = /*#__PURE__*/dual(2, (self, {
112
153
  onNonEmpty
113
154
  }) => isNonEmptyReadonlyArray(self) ? onNonEmpty(self) : onEmpty());
114
155
  /**
156
+ * Matches the elements of an array from the left, applying functions to cases of empty and non-empty arrays.
157
+ *
158
+ * @example
159
+ * import { Array } from "effect"
160
+ *
161
+ * const matchLeft = Array.matchLeft({
162
+ * onEmpty: () => "empty",
163
+ * onNonEmpty: (head, tail) => `head: ${head}, tail: ${tail.length}`
164
+ * })
165
+ * assert.deepStrictEqual(matchLeft([]), "empty")
166
+ * assert.deepStrictEqual(matchLeft([1, 2, 3]), "head: 1, tail: 2")
167
+ *
115
168
  * @category pattern matching
116
169
  * @since 2.0.0
117
170
  */
@@ -120,6 +173,18 @@ export const matchLeft = /*#__PURE__*/dual(2, (self, {
120
173
  onNonEmpty
121
174
  }) => isNonEmptyReadonlyArray(self) ? onNonEmpty(headNonEmpty(self), tailNonEmpty(self)) : onEmpty());
122
175
  /**
176
+ * Matches the elements of an array from the right, applying functions to cases of empty and non-empty arrays.
177
+ *
178
+ * @example
179
+ * import { Array } from "effect"
180
+ *
181
+ * const matchRight = Array.matchRight({
182
+ * onEmpty: () => "empty",
183
+ * onNonEmpty: (init, last) => `init: ${init.length}, last: ${last}`
184
+ * })
185
+ * assert.deepStrictEqual(matchRight([]), "empty")
186
+ * assert.deepStrictEqual(matchRight([1, 2, 3]), "init: 2, last: 3")
187
+ *
123
188
  * @category pattern matching
124
189
  * @since 2.0.0
125
190
  */
@@ -130,6 +195,13 @@ export const matchRight = /*#__PURE__*/dual(2, (self, {
130
195
  /**
131
196
  * Prepend an element to the front of an `Iterable`, creating a new `NonEmptyArray`.
132
197
  *
198
+ * @example
199
+ * import { Array } from "effect"
200
+ *
201
+ * const original = [2, 3, 4];
202
+ * const result = Array.prepend(original, 1);
203
+ * assert.deepStrictEqual(result, [1, 2, 3, 4]);
204
+ *
133
205
  * @category concatenating
134
206
  * @since 2.0.0
135
207
  */
@@ -141,10 +213,10 @@ export const prepend = /*#__PURE__*/dual(2, (self, head) => [head, ...self]);
141
213
  * @example
142
214
  * import { Array } from "effect"
143
215
  *
144
- * assert.deepStrictEqual(
145
- * Array.prependAll([1, 2], ["a", "b"]),
146
- * ["a", "b", 1, 2]
147
- * )
216
+ * const prefix = [0, 1];
217
+ * const array = [2, 3];
218
+ * const result = Array.prependAll(array, prefix);
219
+ * assert.deepStrictEqual(result, [0, 1, 2, 3]);
148
220
  *
149
221
  * @category concatenating
150
222
  * @since 2.0.0
@@ -153,6 +225,13 @@ export const prependAll = /*#__PURE__*/dual(2, (self, that) => fromIterable(that
153
225
  /**
154
226
  * Append an element to the end of an `Iterable`, creating a new `NonEmptyArray`.
155
227
  *
228
+ * @example
229
+ * import { Array } from "effect"
230
+ *
231
+ * const original = [1, 2, 3];
232
+ * const result = Array.append(original, 4);
233
+ * assert.deepStrictEqual(result, [1, 2, 3, 4]);
234
+ *
156
235
  * @category concatenating
157
236
  * @since 2.0.0
158
237
  */
@@ -166,7 +245,22 @@ export const append = /*#__PURE__*/dual(2, (self, last) => [...self, last]);
166
245
  */
167
246
  export const appendAll = /*#__PURE__*/dual(2, (self, that) => fromIterable(self).concat(fromIterable(that)));
168
247
  /**
169
- * Reduce an `Iterable` from the left, keeping all intermediate results instead of only the final result.
248
+ * Accumulates values from an `Iterable` starting from the left, storing
249
+ * each intermediate result in an array. Useful for tracking the progression of
250
+ * a value through a series of transformations.
251
+ *
252
+ * @example
253
+ * import { Array } from "effect";
254
+ *
255
+ * const numbers = [1, 2, 3, 4]
256
+ * const result = Array.scan(numbers, 0, (acc, value) => acc + value)
257
+ * assert.deepStrictEqual(result, [0, 1, 3, 6, 10])
258
+ *
259
+ * // Explanation:
260
+ * // This function starts with the initial value (0 in this case)
261
+ * // and adds each element of the array to this accumulator one by one,
262
+ * // keeping track of the cumulative sum after each addition.
263
+ * // Each of these sums is captured in the resulting array.
170
264
  *
171
265
  * @category folding
172
266
  * @since 2.0.0
@@ -181,7 +275,16 @@ export const scan = /*#__PURE__*/dual(3, (self, b, f) => {
181
275
  return out;
182
276
  });
183
277
  /**
184
- * Reduce an `Iterable` from the right, keeping all intermediate results instead of only the final result.
278
+ * Accumulates values from an `Iterable` starting from the right, storing
279
+ * each intermediate result in an array. Useful for tracking the progression of
280
+ * a value through a series of transformations.
281
+ *
282
+ * @example
283
+ * import { Array } from "effect";
284
+ *
285
+ * const numbers = [1, 2, 3, 4]
286
+ * const result = Array.scanRight(numbers, 0, (acc, value) => acc + value)
287
+ * assert.deepStrictEqual(result, [10, 9, 7, 4, 0])
185
288
  *
186
289
  * @category folding
187
290
  * @since 2.0.0
@@ -309,6 +412,12 @@ export const unsafeGet = /*#__PURE__*/dual(2, (self, index) => {
309
412
  /**
310
413
  * Return a tuple containing the first element, and a new `Array` of the remaining elements, if any.
311
414
  *
415
+ * @example
416
+ * import { Array } from "effect";
417
+ *
418
+ * const result = Array.unprepend([1, 2, 3, 4])
419
+ * assert.deepStrictEqual(result, [1, [2, 3, 4]])
420
+ *
312
421
  * @category splitting
313
422
  * @since 2.0.0
314
423
  */
@@ -316,6 +425,12 @@ export const unprepend = self => [headNonEmpty(self), tailNonEmpty(self)];
316
425
  /**
317
426
  * Return a tuple containing a copy of the `NonEmptyReadonlyArray` without its last element, and that last element.
318
427
  *
428
+ * @example
429
+ * import { Array } from "effect";
430
+ *
431
+ * const result = Array.unappend([1, 2, 3, 4])
432
+ * assert.deepStrictEqual(result, [[1, 2, 3], 4])
433
+ *
319
434
  * @category splitting
320
435
  * @since 2.0.0
321
436
  */
@@ -328,6 +443,14 @@ export const unappend = self => [initNonEmpty(self), lastNonEmpty(self)];
328
443
  */
329
444
  export const head = /*#__PURE__*/get(0);
330
445
  /**
446
+ * Get the first element of a non empty array.
447
+ *
448
+ * @example
449
+ * import { Array } from "effect"
450
+ *
451
+ * const result = Array.headNonEmpty([1, 2, 3, 4])
452
+ * assert.deepStrictEqual(result, 1)
453
+ *
331
454
  * @category getters
332
455
  * @since 2.0.0
333
456
  */
@@ -340,6 +463,14 @@ export const headNonEmpty = /*#__PURE__*/unsafeGet(0);
340
463
  */
341
464
  export const last = self => isNonEmptyReadonlyArray(self) ? O.some(lastNonEmpty(self)) : O.none();
342
465
  /**
466
+ * Get the last element of a non empty array.
467
+ *
468
+ * @example
469
+ * import { Array } from "effect"
470
+ *
471
+ * const result = Array.lastNonEmpty([1, 2, 3, 4])
472
+ * assert.deepStrictEqual(result, 4)
473
+ *
343
474
  * @category getters
344
475
  * @since 2.0.0
345
476
  */
@@ -355,6 +486,14 @@ export const tail = self => {
355
486
  return isNonEmptyReadonlyArray(input) ? O.some(tailNonEmpty(input)) : O.none();
356
487
  };
357
488
  /**
489
+ * Get all but the first element of a `NonEmptyReadonlyArray`.
490
+ *
491
+ * @example
492
+ * import { Array } from "effect"
493
+ *
494
+ * const result = Array.tailNonEmpty([1, 2, 3, 4])
495
+ * assert.deepStrictEqual(result, [2, 3, 4])
496
+ *
358
497
  * @category getters
359
498
  * @since 2.0.0
360
499
  */
@@ -372,6 +511,12 @@ export const init = self => {
372
511
  /**
373
512
  * Get all but the last element of a non empty array, creating a new array.
374
513
  *
514
+ * @example
515
+ * import { Array } from "effect"
516
+ *
517
+ * const result = Array.initNonEmpty([1, 2, 3, 4])
518
+ * assert.deepStrictEqual(result, [1, 2, 3])
519
+ *
375
520
  * @category getters
376
521
  * @since 2.0.0
377
522
  */
@@ -381,6 +526,13 @@ export const initNonEmpty = self => self.slice(0, -1);
381
526
  *
382
527
  * **Note**. `n` is normalized to a non negative integer.
383
528
  *
529
+ * @example
530
+ * import { Array } from "effect"
531
+ *
532
+ * const numbers = [1, 2, 3, 4, 5]
533
+ * const result = Array.take(numbers, 3)
534
+ * assert.deepStrictEqual(result, [1, 2, 3])
535
+ *
384
536
  * @category getters
385
537
  * @since 2.0.0
386
538
  */
@@ -393,6 +545,13 @@ export const take = /*#__PURE__*/dual(2, (self, n) => {
393
545
  *
394
546
  * **Note**. `n` is normalized to a non negative integer.
395
547
  *
548
+ * @example
549
+ * import { Array } from "effect"
550
+ *
551
+ * const numbers = [1, 2, 3, 4, 5]
552
+ * const result = Array.takeRight(numbers, 3)
553
+ * assert.deepStrictEqual(result, [3, 4, 5])
554
+ *
396
555
  * @category getters
397
556
  * @since 2.0.0
398
557
  */
@@ -404,6 +563,19 @@ export const takeRight = /*#__PURE__*/dual(2, (self, n) => {
404
563
  /**
405
564
  * Calculate the longest initial subarray for which all element satisfy the specified predicate, creating a new `Array`.
406
565
  *
566
+ * @example
567
+ * import { Array } from "effect"
568
+ *
569
+ * const numbers = [1, 3, 2, 4, 1, 2]
570
+ * const result = Array.takeWhile(numbers, x => x < 4)
571
+ * assert.deepStrictEqual(result, [1, 3, 2])
572
+ *
573
+ * // Explanation:
574
+ * // - The function starts with the first element (`1`), which is less than `4`, so it adds `1` to the result.
575
+ * // - The next element (`3`) is also less than `4`, so it adds `3`.
576
+ * // - The next element (`2`) is again less than `4`, so it adds `2`.
577
+ * // - The function then encounters `4`, which is not less than `4`. At this point, it stops checking further elements and finalizes the result.
578
+ *
407
579
  * @category getters
408
580
  * @since 2.0.0
409
581
  */
@@ -444,6 +616,13 @@ export const span = /*#__PURE__*/dual(2, (self, predicate) => splitAt(self, span
444
616
  *
445
617
  * **Note**. `n` is normalized to a non negative integer.
446
618
  *
619
+ * @example
620
+ * import { Array } from "effect"
621
+ *
622
+ * const numbers = [1, 2, 3, 4, 5]
623
+ * const result = Array.drop(numbers, 2)
624
+ * assert.deepStrictEqual(result, [3, 4, 5])
625
+ *
447
626
  * @category getters
448
627
  * @since 2.0.0
449
628
  */
@@ -456,6 +635,13 @@ export const drop = /*#__PURE__*/dual(2, (self, n) => {
456
635
  *
457
636
  * **Note**. `n` is normalized to a non negative integer.
458
637
  *
638
+ * @example
639
+ * import { Array } from "effect"
640
+ *
641
+ * const numbers = [1, 2, 3, 4, 5]
642
+ * const result = Array.dropRight(numbers, 2)
643
+ * assert.deepStrictEqual(result, [1, 2, 3])
644
+ *
459
645
  * @category getters
460
646
  * @since 2.0.0
461
647
  */
@@ -466,6 +652,13 @@ export const dropRight = /*#__PURE__*/dual(2, (self, n) => {
466
652
  /**
467
653
  * Remove the longest initial subarray for which all element satisfy the specified predicate, creating a new `Array`.
468
654
  *
655
+ * @example
656
+ * import { Array } from "effect"
657
+ *
658
+ * const numbers = [1, 2, 3, 4, 5]
659
+ * const result = Array.dropWhile(numbers, x => x < 4)
660
+ * assert.deepStrictEqual(result, [4, 5])
661
+ *
469
662
  * @category getters
470
663
  * @since 2.0.0
471
664
  */
@@ -473,6 +666,13 @@ export const dropWhile = /*#__PURE__*/dual(2, (self, predicate) => fromIterable(
473
666
  /**
474
667
  * Return the first index for which a predicate holds.
475
668
  *
669
+ * @example
670
+ * import { Array, Option } from "effect"
671
+ *
672
+ * const numbers = [5, 3, 8, 9]
673
+ * const result = Array.findFirstIndex(numbers, x => x > 5)
674
+ * assert.deepStrictEqual(result, Option.some(2))
675
+ *
476
676
  * @category elements
477
677
  * @since 2.0.0
478
678
  */
@@ -489,6 +689,13 @@ export const findFirstIndex = /*#__PURE__*/dual(2, (self, predicate) => {
489
689
  /**
490
690
  * Return the last index for which a predicate holds.
491
691
  *
692
+ * @example
693
+ * import { Array, Option } from "effect"
694
+ *
695
+ * const numbers = [1, 3, 8, 9]
696
+ * const result = Array.findLastIndex(numbers, x => x < 5)
697
+ * assert.deepStrictEqual(result, Option.some(1))
698
+ *
492
699
  * @category elements
493
700
  * @since 2.0.0
494
701
  */
@@ -505,12 +712,27 @@ export const findLastIndex = /*#__PURE__*/dual(2, (self, predicate) => {
505
712
  * Returns the first element that satisfies the specified
506
713
  * predicate, or `None` if no such element exists.
507
714
  *
715
+ * @example
716
+ * import { Array, Option } from "effect"
717
+ *
718
+ * const numbers = [1, 2, 3, 4, 5]
719
+ * const result = Array.findFirst(numbers, x => x > 3)
720
+ * assert.deepStrictEqual(result, Option.some(4))
721
+ *
508
722
  * @category elements
509
723
  * @since 2.0.0
510
724
  */
511
725
  export const findFirst = EffectIterable.findFirst;
512
726
  /**
513
- * Find the last element for which a predicate holds.
727
+ * Finds the last element in an iterable collection that satisfies the given predicate or refinement.
728
+ * Returns an `Option` containing the found element, or `Option.none` if no element matches.
729
+ *
730
+ * @example
731
+ * import { Array, Option } from "effect"
732
+ *
733
+ * const numbers = [1, 2, 3, 4, 5]
734
+ * const result = Array.findLast(numbers, n => n % 2 === 0)
735
+ * assert.deepStrictEqual(result, Option.some(4))
514
736
  *
515
737
  * @category elements
516
738
  * @since 2.0.0
@@ -536,6 +758,13 @@ export const findLast = /*#__PURE__*/dual(2, (self, f) => {
536
758
  * Insert an element at the specified index, creating a new `NonEmptyArray`,
537
759
  * or return `None` if the index is out of bounds.
538
760
  *
761
+ * @example
762
+ * import { Array, Option } from "effect"
763
+ *
764
+ * const letters = ['a', 'b', 'c', 'e']
765
+ * const result = Array.insertAt(letters, 3, 'd')
766
+ * assert.deepStrictEqual(result, Option.some(['a', 'b', 'c', 'd', 'e']))
767
+ *
539
768
  * @since 2.0.0
540
769
  */
541
770
  export const insertAt = /*#__PURE__*/dual(3, (self, i, b) => {
@@ -551,10 +780,26 @@ export const insertAt = /*#__PURE__*/dual(3, (self, i, b) => {
551
780
  * Change the element at the specified index, creating a new `Array`,
552
781
  * or return a copy of the input if the index is out of bounds.
553
782
  *
783
+ * @example
784
+ * import { Array } from "effect"
785
+ *
786
+ * const letters = ['a', 'b', 'c', 'd']
787
+ * const result = Array.replace(1, 'z')(letters)
788
+ * assert.deepStrictEqual(result, ['a', 'z', 'c', 'd'])
789
+ *
554
790
  * @since 2.0.0
555
791
  */
556
792
  export const replace = /*#__PURE__*/dual(3, (self, i, b) => modify(self, i, () => b));
557
793
  /**
794
+ * Replaces an element in an array with the given value, returning an option of the updated array.
795
+ *
796
+ * @example
797
+ * import { Array, Option } from "effect"
798
+ *
799
+ * const numbers = [1, 2, 3]
800
+ * const result = Array.replaceOption(numbers, 1, 4)
801
+ * assert.deepStrictEqual(result, Option.some([1, 4, 3]))
802
+ *
558
803
  * @since 2.0.0
559
804
  */
560
805
  export const replaceOption = /*#__PURE__*/dual(3, (self, i, b) => modifyOption(self, i, () => b));
@@ -562,6 +807,13 @@ export const replaceOption = /*#__PURE__*/dual(3, (self, i, b) => modifyOption(s
562
807
  * Apply a function to the element at the specified index, creating a new `Array`,
563
808
  * or return a copy of the input if the index is out of bounds.
564
809
  *
810
+ * @example
811
+ * import { Array } from "effect"
812
+ *
813
+ * const numbers = [1, 2, 3, 4]
814
+ * const result = Array.modify(numbers, 2, (n) => n * 2)
815
+ * assert.deepStrictEqual(result, [1, 2, 6, 4])
816
+ *
565
817
  * @since 2.0.0
566
818
  */
567
819
  export const modify = /*#__PURE__*/dual(3, (self, i, f) => O.getOrElse(modifyOption(self, i, f), () => Array.from(self)));
@@ -569,6 +821,16 @@ export const modify = /*#__PURE__*/dual(3, (self, i, f) => O.getOrElse(modifyOpt
569
821
  * Apply a function to the element at the specified index, creating a new `Array`,
570
822
  * or return `None` if the index is out of bounds.
571
823
  *
824
+ * @example
825
+ * import { Array, Option } from "effect"
826
+ *
827
+ * const numbers = [1, 2, 3, 4]
828
+ * const result = Array.modifyOption(numbers, 2, (n) => n * 2)
829
+ * assert.deepStrictEqual(result, Option.some([1, 2, 6, 4]))
830
+ *
831
+ * const outOfBoundsResult = Array.modifyOption(numbers, 5, (n) => n * 2)
832
+ * assert.deepStrictEqual(outOfBoundsResult, Option.none())
833
+ *
572
834
  * @since 2.0.0
573
835
  */
574
836
  export const modifyOption = /*#__PURE__*/dual(3, (self, i, f) => {
@@ -585,6 +847,16 @@ export const modifyOption = /*#__PURE__*/dual(3, (self, i, f) => {
585
847
  * Delete the element at the specified index, creating a new `Array`,
586
848
  * or return a copy of the input if the index is out of bounds.
587
849
  *
850
+ * @example
851
+ * import { Array } from "effect"
852
+ *
853
+ * const numbers = [1, 2, 3, 4]
854
+ * const result = Array.remove(numbers, 2)
855
+ * assert.deepStrictEqual(result, [1, 2, 4])
856
+ *
857
+ * const outOfBoundsResult = Array.remove(numbers, 5)
858
+ * assert.deepStrictEqual(outOfBoundsResult, [1, 2, 3, 4])
859
+ *
588
860
  * @since 2.0.0
589
861
  */
590
862
  export const remove = /*#__PURE__*/dual(2, (self, i) => {
@@ -598,6 +870,13 @@ export const remove = /*#__PURE__*/dual(2, (self, i) => {
598
870
  /**
599
871
  * Reverse an `Iterable`, creating a new `Array`.
600
872
  *
873
+ * @example
874
+ * import { Array } from "effect"
875
+ *
876
+ * const numbers = [1, 2, 3, 4]
877
+ * const result = Array.reverse(numbers)
878
+ * assert.deepStrictEqual(result, [4, 3, 2, 1])
879
+ *
601
880
  * @category elements
602
881
  * @since 2.0.0
603
882
  */
@@ -615,13 +894,54 @@ export const sort = /*#__PURE__*/dual(2, (self, O) => {
615
894
  return out;
616
895
  });
617
896
  /**
897
+ * Sorts an array based on a provided mapping function and order. The mapping
898
+ * function transforms the elements into a value that can be compared, and the
899
+ * order defines how those values should be sorted.
900
+ *
901
+ * @example
902
+ * import { Array, Order } from "effect"
903
+ *
904
+ * const strings = ["aaa", "b", "cc"]
905
+ * const result = Array.sortWith(strings, (s) => s.length, Order.number)
906
+ * assert.deepStrictEqual(result, ["b", "cc", "aaa"])
907
+ *
908
+ * // Explanation:
909
+ * // The array of strings is sorted based on their lengths. The mapping function `(s) => s.length`
910
+ * // converts each string into its length, and the `Order.number` specifies that the lengths should
911
+ * // be sorted in ascending order.
912
+ *
618
913
  * @since 2.0.0
619
914
  * @category elements
620
915
  */
621
916
  export const sortWith = /*#__PURE__*/dual(3, (self, f, order) => sort(self, Order.mapInput(order, f)));
622
917
  /**
623
- * Sort the elements of an `Iterable` in increasing order, where elements are compared
624
- * using first `orders[0]`, then `orders[1]`, etc...
918
+ * Sorts the elements of an `Iterable` in increasing order based on the provided
919
+ * orders. The elements are compared using the first order in `orders`, then the
920
+ * second order if the first comparison is equal, and so on.
921
+ *
922
+ * @example
923
+ * import { Array, Order } from "effect"
924
+ *
925
+ * const users = [
926
+ * { name: "Alice", age: 30 },
927
+ * { name: "Bob", age: 25 },
928
+ * { name: "Charlie", age: 30 }
929
+ * ]
930
+ *
931
+ * const result = Array.sortBy(
932
+ * Order.mapInput(Order.number, (user: (typeof users)[number]) => user.age),
933
+ * Order.mapInput(Order.string, (user: (typeof users)[number]) => user.name)
934
+ * )(users)
935
+ *
936
+ * assert.deepStrictEqual(result, [
937
+ * { name: "Bob", age: 25 },
938
+ * { name: "Alice", age: 30 },
939
+ * { name: "Charlie", age: 30 }
940
+ * ])
941
+ *
942
+ * // Explanation:
943
+ * // The array of users is sorted first by age in ascending order. When ages are equal,
944
+ * // the users are further sorted by name in ascending order.
625
945
  *
626
946
  * @category sorting
627
947
  * @since 2.0.0
@@ -641,6 +961,14 @@ export const sortBy = (...orders) => {
641
961
  * If one input `Iterable` is short, excess elements of the
642
962
  * longer `Iterable` are discarded.
643
963
  *
964
+ * @example
965
+ * import { Array } from "effect"
966
+ *
967
+ * const array1 = [1, 2, 3]
968
+ * const array2 = ['a', 'b']
969
+ * const result = Array.zip(array1, array2)
970
+ * assert.deepStrictEqual(result, [[1, 'a'], [2, 'b']])
971
+ *
644
972
  * @category zipping
645
973
  * @since 2.0.0
646
974
  */
@@ -649,6 +977,14 @@ export const zip = /*#__PURE__*/dual(2, (self, that) => zipWith(self, that, Tupl
649
977
  * Apply a function to pairs of elements at the same index in two `Iterable`s, collecting the results in a new `Array`. If one
650
978
  * input `Iterable` is short, excess elements of the longer `Iterable` are discarded.
651
979
  *
980
+ * @example
981
+ * import { Array } from "effect"
982
+ *
983
+ * const array1 = [1, 2, 3]
984
+ * const array2 = [4, 5, 6]
985
+ * const result = Array.zipWith(array1, array2, (a, b) => a + b)
986
+ * assert.deepStrictEqual(result, [5, 7, 9])
987
+ *
652
988
  * @category zipping
653
989
  * @since 2.0.0
654
990
  */
@@ -668,6 +1004,12 @@ export const zipWith = /*#__PURE__*/dual(3, (self, that, f) => {
668
1004
  /**
669
1005
  * This function is the inverse of `zip`. Takes an `Iterable` of pairs and return two corresponding `Array`s.
670
1006
  *
1007
+ * @example
1008
+ * import { Array } from "effect"
1009
+ *
1010
+ * const result = Array.unzip([[1, "a"], [2, "b"], [3, "c"]])
1011
+ * assert.deepStrictEqual(result, [[1, 2, 3], ['a', 'b', 'c']])
1012
+ *
671
1013
  * @since 2.0.0
672
1014
  */
673
1015
  export const unzip = self => {
@@ -687,6 +1029,13 @@ export const unzip = self => {
687
1029
  * Places an element in between members of an `Iterable`.
688
1030
  * If the input is a non-empty array, the result is also a non-empty array.
689
1031
  *
1032
+ * @example
1033
+ * import { Array } from "effect"
1034
+ *
1035
+ * const numbers = [1, 2, 3]
1036
+ * const result = Array.intersperse(numbers, 0)
1037
+ * assert.deepStrictEqual(result, [1, 0, 2, 0, 3])
1038
+ *
690
1039
  * @since 2.0.0
691
1040
  */
692
1041
  export const intersperse = /*#__PURE__*/dual(2, (self, middle) => {
@@ -707,24 +1056,48 @@ export const intersperse = /*#__PURE__*/dual(2, (self, middle) => {
707
1056
  /**
708
1057
  * Apply a function to the head, creating a new `NonEmptyReadonlyArray`.
709
1058
  *
1059
+ * @example
1060
+ * import { Array } from "effect"
1061
+ *
1062
+ * const result = Array.modifyNonEmptyHead([1, 2, 3], n => n * 10)
1063
+ * assert.deepStrictEqual(result, [10, 2, 3])
1064
+ *
710
1065
  * @since 2.0.0
711
1066
  */
712
1067
  export const modifyNonEmptyHead = /*#__PURE__*/dual(2, (self, f) => [f(headNonEmpty(self)), ...tailNonEmpty(self)]);
713
1068
  /**
714
1069
  * Change the head, creating a new `NonEmptyReadonlyArray`.
715
1070
  *
1071
+ * @example
1072
+ * import { Array } from "effect"
1073
+ *
1074
+ * const result = Array.setNonEmptyHead([1, 2, 3], 10)
1075
+ * assert.deepStrictEqual(result, [10, 2, 3])
1076
+ *
716
1077
  * @since 2.0.0
717
1078
  */
718
1079
  export const setNonEmptyHead = /*#__PURE__*/dual(2, (self, b) => modifyNonEmptyHead(self, () => b));
719
1080
  /**
720
1081
  * Apply a function to the last element, creating a new `NonEmptyReadonlyArray`.
721
1082
  *
1083
+ * @example
1084
+ * import { Array } from "effect"
1085
+ *
1086
+ * const result = Array.modifyNonEmptyLast([1, 2, 3], n => n * 2)
1087
+ * assert.deepStrictEqual(result, [1, 2, 6])
1088
+ *
722
1089
  * @since 2.0.0
723
1090
  */
724
1091
  export const modifyNonEmptyLast = /*#__PURE__*/dual(2, (self, f) => append(initNonEmpty(self), f(lastNonEmpty(self))));
725
1092
  /**
726
1093
  * Change the last element, creating a new `NonEmptyReadonlyArray`.
727
1094
  *
1095
+ * @example
1096
+ * import { Array } from "effect"
1097
+ *
1098
+ * const result = Array.setNonEmptyLast([1, 2, 3], 4)
1099
+ * assert.deepStrictEqual(result, [1, 2, 4])
1100
+ *
728
1101
  * @since 2.0.0
729
1102
  */
730
1103
  export const setNonEmptyLast = /*#__PURE__*/dual(2, (self, b) => modifyNonEmptyLast(self, () => b));
@@ -732,6 +1105,13 @@ export const setNonEmptyLast = /*#__PURE__*/dual(2, (self, b) => modifyNonEmptyL
732
1105
  * Rotate an `Iterable` by `n` steps.
733
1106
  * If the input is a non-empty array, the result is also a non-empty array.
734
1107
  *
1108
+ * @example
1109
+ * import { Array } from "effect"
1110
+ *
1111
+ * const letters = ['a', 'b', 'c', 'd']
1112
+ * const result = Array.rotate(letters, 2)
1113
+ * assert.deepStrictEqual(result, ['c', 'd', 'a', 'b'])
1114
+ *
735
1115
  * @since 2.0.0
736
1116
  */
737
1117
  export const rotate = /*#__PURE__*/dual(2, (self, n) => {
@@ -754,6 +1134,15 @@ export const rotate = /*#__PURE__*/dual(2, (self, n) => {
754
1134
  /**
755
1135
  * Returns a function that checks if a `ReadonlyArray` contains a given value using a provided `isEquivalent` function.
756
1136
  *
1137
+ * @example
1138
+ * import { Array } from "effect"
1139
+ *
1140
+ * const numbers = [1, 2, 3, 4]
1141
+ * const isEquivalent = (a: number, b: number) => a === b
1142
+ * const containsNumber = Array.containsWith(isEquivalent)
1143
+ * const result = containsNumber(3)(numbers)
1144
+ * assert.deepStrictEqual(result, true)
1145
+ *
757
1146
  * @category elements
758
1147
  * @since 2.0.0
759
1148
  */
@@ -769,6 +1158,13 @@ const _equivalence = /*#__PURE__*/Equal.equivalence();
769
1158
  /**
770
1159
  * Returns a function that checks if a `ReadonlyArray` contains a given value using the default `Equivalence`.
771
1160
  *
1161
+ * @example
1162
+ * import { Array } from "effect"
1163
+ *
1164
+ * const letters = ['a', 'b', 'c', 'd']
1165
+ * const result = Array.contains('c')(letters)
1166
+ * assert.deepStrictEqual(result, true)
1167
+ *
772
1168
  * @category elements
773
1169
  * @since 2.0.0
774
1170
  */
@@ -778,6 +1174,18 @@ export const contains = /*#__PURE__*/containsWith(_equivalence);
778
1174
  * `Iterable`. Typically chop is called with some function that will consume an initial prefix of the `Iterable` and produce a
779
1175
  * value and the rest of the `Array`.
780
1176
  *
1177
+ * @example
1178
+ * import { Array } from "effect"
1179
+ *
1180
+ * const numbers = [1, 2, 3, 4, 5]
1181
+ * const result = Array.chop(numbers, (as): [number, Array<number>] => [as[0] * 2, as.slice(1)])
1182
+ * assert.deepStrictEqual(result, [2, 4, 6, 8, 10])
1183
+ *
1184
+ * // Explanation:
1185
+ * // The `chopFunction` takes the first element of the array, doubles it, and then returns it along with the rest of the array.
1186
+ * // The `chop` function applies this `chopFunction` recursively to the input array `[1, 2, 3, 4, 5]`,
1187
+ * // resulting in a new array `[2, 4, 6, 8, 10]`.
1188
+ *
781
1189
  * @since 2.0.0
782
1190
  */
783
1191
  export const chop = /*#__PURE__*/dual(2, (self, f) => {
@@ -799,6 +1207,13 @@ export const chop = /*#__PURE__*/dual(2, (self, f) => {
799
1207
  * Splits an `Iterable` into two segments, with the first segment containing a maximum of `n` elements.
800
1208
  * The value of `n` can be `0`.
801
1209
  *
1210
+ * @example
1211
+ * import { Array } from "effect"
1212
+ *
1213
+ * const numbers = [1, 2, 3, 4, 5]
1214
+ * const result = Array.splitAt(numbers, 3)
1215
+ * assert.deepStrictEqual(result, [[1, 2, 3], [4, 5]])
1216
+ *
802
1217
  * @category splitting
803
1218
  * @since 2.0.0
804
1219
  */
@@ -817,6 +1232,12 @@ export const splitAt = /*#__PURE__*/dual(2, (self, n) => {
817
1232
  * Splits a `NonEmptyReadonlyArray` into two segments, with the first segment containing a maximum of `n` elements.
818
1233
  * The value of `n` must be `>= 1`.
819
1234
  *
1235
+ * @example
1236
+ * import { Array } from "effect"
1237
+ *
1238
+ * const result = Array.splitNonEmptyAt(["a", "b", "c", "d", "e"], 3)
1239
+ * assert.deepStrictEqual(result, [["a", "b", "c"], ["d", "e"]])
1240
+ *
820
1241
  * @category splitting
821
1242
  * @since 2.0.0
822
1243
  */
@@ -827,6 +1248,13 @@ export const splitNonEmptyAt = /*#__PURE__*/dual(2, (self, n) => {
827
1248
  /**
828
1249
  * Splits this iterable into `n` equally sized arrays.
829
1250
  *
1251
+ * @example
1252
+ * import { Array } from "effect"
1253
+ *
1254
+ * const numbers = [1, 2, 3, 4, 5, 6, 7, 8]
1255
+ * const result = Array.split(numbers, 3)
1256
+ * assert.deepStrictEqual(result, [[1, 2, 3], [4, 5, 6], [7, 8]])
1257
+ *
830
1258
  * @since 2.0.0
831
1259
  * @category splitting
832
1260
  */
@@ -838,11 +1266,27 @@ export const split = /*#__PURE__*/dual(2, (self, n) => {
838
1266
  * Splits this iterable on the first element that matches this predicate.
839
1267
  * Returns a tuple containing two arrays: the first one is before the match, and the second one is from the match onward.
840
1268
  *
1269
+ * @example
1270
+ * import { Array } from "effect"
1271
+ *
1272
+ * const numbers = [1, 2, 3, 4, 5]
1273
+ * const result = Array.splitWhere(numbers, n => n > 3)
1274
+ * assert.deepStrictEqual(result, [[1, 2, 3], [4, 5]])
1275
+ *
841
1276
  * @category splitting
842
1277
  * @since 2.0.0
843
1278
  */
844
1279
  export const splitWhere = /*#__PURE__*/dual(2, (self, predicate) => span(self, (a, i) => !predicate(a, i)));
845
1280
  /**
1281
+ * Copies an array.
1282
+ *
1283
+ * @example
1284
+ * import { Array } from "effect"
1285
+ *
1286
+ * const numbers = [1, 2, 3]
1287
+ * const copy = Array.copy(numbers)
1288
+ * assert.deepStrictEqual(copy, [1, 2, 3])
1289
+ *
846
1290
  * @since 2.0.0
847
1291
  */
848
1292
  export const copy = self => self.slice();
@@ -857,6 +1301,19 @@ export const copy = self => self.slice();
857
1301
  *
858
1302
  * whenever `n` evenly divides the length of `self`.
859
1303
  *
1304
+ * @example
1305
+ * import { Array } from "effect"
1306
+ *
1307
+ * const numbers = [1, 2, 3, 4, 5]
1308
+ * const result = Array.chunksOf(numbers, 2)
1309
+ * assert.deepStrictEqual(result, [[1, 2], [3, 4], [5]])
1310
+ *
1311
+ * // Explanation:
1312
+ * // The `chunksOf` function takes an array of numbers `[1, 2, 3, 4, 5]` and a number `2`.
1313
+ * // It splits the array into chunks of length 2. Since the array length is not evenly divisible by 2,
1314
+ * // the last chunk contains the remaining elements.
1315
+ * // The result is `[[1, 2], [3, 4], [5]]`.
1316
+ *
860
1317
  * @category splitting
861
1318
  * @since 2.0.0
862
1319
  */
@@ -870,6 +1327,12 @@ export const chunksOf = /*#__PURE__*/dual(2, (self, n) => {
870
1327
  /**
871
1328
  * Group equal, consecutive elements of a `NonEmptyReadonlyArray` into `NonEmptyArray`s using the provided `isEquivalent` function.
872
1329
  *
1330
+ * @example
1331
+ * import { Array } from "effect"
1332
+ *
1333
+ * const result = Array.groupWith(["a", "a", "b", "b", "b", "c", "a"], (x, y) => x === y)
1334
+ * assert.deepStrictEqual(result, [["a", "a"], ["b", "b", "b"], ["c"], ["a"]])
1335
+ *
873
1336
  * @category grouping
874
1337
  * @since 2.0.0
875
1338
  */
@@ -890,6 +1353,12 @@ export const groupWith = /*#__PURE__*/dual(2, (self, isEquivalent) => chop(self,
890
1353
  /**
891
1354
  * Group equal, consecutive elements of a `NonEmptyReadonlyArray` into `NonEmptyArray`s.
892
1355
  *
1356
+ * @example
1357
+ * import { Array } from "effect"
1358
+ *
1359
+ * const result = Array.group([1, 1, 2, 2, 2, 3, 1])
1360
+ * assert.deepStrictEqual(result, [[1, 1], [2, 2, 2], [3], [1]])
1361
+ *
893
1362
  * @category grouping
894
1363
  * @since 2.0.0
895
1364
  */
@@ -898,6 +1367,20 @@ export const group = /*#__PURE__*/groupWith( /*#__PURE__*/Equal.equivalence());
898
1367
  * Splits an `Iterable` into sub-non-empty-arrays stored in an object, based on the result of calling a `string`-returning
899
1368
  * function on each element, and grouping the results according to values returned
900
1369
  *
1370
+ * @example
1371
+ * import { Array } from "effect"
1372
+ *
1373
+ * const people = [
1374
+ * { name: "Alice", group: "A" },
1375
+ * { name: "Bob", group: "B" },
1376
+ * { name: "Charlie", group: "A" }
1377
+ * ]
1378
+ * const result = Array.groupBy(people, person => person.group)
1379
+ * assert.deepStrictEqual(result, {
1380
+ * A: [{ name: "Alice", group: "A" }, { name: "Charlie", group: "A" }],
1381
+ * B: [{ name: "Bob", group: "B" }]
1382
+ * })
1383
+ *
901
1384
  * @category grouping
902
1385
  * @since 2.0.0
903
1386
  */
@@ -914,6 +1397,16 @@ export const groupBy = /*#__PURE__*/dual(2, (self, f) => {
914
1397
  return out;
915
1398
  });
916
1399
  /**
1400
+ * Calculates the union of two arrays using the provided equivalence relation.
1401
+ *
1402
+ * @example
1403
+ * import { Array } from "effect"
1404
+ *
1405
+ * const array1 = [1, 2]
1406
+ * const array2 = [2, 3]
1407
+ * const union = Array.unionWith(array1, array2, (a, b) => a === b)
1408
+ * assert.deepStrictEqual(union, [1, 2, 3])
1409
+ *
917
1410
  * @since 2.0.0
918
1411
  */
919
1412
  export const unionWith = /*#__PURE__*/dual(3, (self, that, isEquivalent) => {
@@ -929,6 +1422,16 @@ export const unionWith = /*#__PURE__*/dual(3, (self, that, isEquivalent) => {
929
1422
  return b;
930
1423
  });
931
1424
  /**
1425
+ * Creates a union of two arrays, removing duplicates.
1426
+ *
1427
+ * @example
1428
+ * import { Array } from "effect"
1429
+ *
1430
+ * const array1 = [1, 2]
1431
+ * const array2 = [2, 3]
1432
+ * const result = Array.union(array1, array2)
1433
+ * assert.deepStrictEqual(result, [1, 2, 3])
1434
+ *
932
1435
  * @since 2.0.0
933
1436
  */
934
1437
  export const union = /*#__PURE__*/dual(2, (self, that) => unionWith(self, that, _equivalence));
@@ -936,6 +1439,15 @@ export const union = /*#__PURE__*/dual(2, (self, that) => unionWith(self, that,
936
1439
  * Creates an `Array` of unique values that are included in all given `Iterable`s using the provided `isEquivalent` function.
937
1440
  * The order and references of result values are determined by the first `Iterable`.
938
1441
  *
1442
+ * @example
1443
+ * import { Array } from "effect"
1444
+ *
1445
+ * const array1 = [{ id: 1 }, { id: 2 }, { id: 3 }]
1446
+ * const array2 = [{ id: 3 }, { id: 4 }, { id: 1 }]
1447
+ * const isEquivalent = (a: { id: number }, b: { id: number }) => a.id === b.id
1448
+ * const result = Array.intersectionWith(isEquivalent)(array2)(array1)
1449
+ * assert.deepStrictEqual(result, [{ id: 1 }, { id: 3 }])
1450
+ *
939
1451
  * @since 2.0.0
940
1452
  */
941
1453
  export const intersectionWith = isEquivalent => {
@@ -946,6 +1458,14 @@ export const intersectionWith = isEquivalent => {
946
1458
  * Creates an `Array` of unique values that are included in all given `Iterable`s.
947
1459
  * The order and references of result values are determined by the first `Iterable`.
948
1460
  *
1461
+ * @example
1462
+ * import { Array } from "effect"
1463
+ *
1464
+ * const array1 = [1, 2, 3]
1465
+ * const array2 = [3, 4, 1]
1466
+ * const result = Array.intersection(array1, array2)
1467
+ * assert.deepStrictEqual(result, [1, 3])
1468
+ *
949
1469
  * @since 2.0.0
950
1470
  */
951
1471
  export const intersection = /*#__PURE__*/intersectionWith(_equivalence);
@@ -953,6 +1473,14 @@ export const intersection = /*#__PURE__*/intersectionWith(_equivalence);
953
1473
  * Creates a `Array` of values not included in the other given `Iterable` using the provided `isEquivalent` function.
954
1474
  * The order and references of result values are determined by the first `Iterable`.
955
1475
  *
1476
+ * @example
1477
+ * import { Array } from "effect"
1478
+ *
1479
+ * const array1 = [1, 2, 3]
1480
+ * const array2 = [2, 3, 4]
1481
+ * const difference = Array.differenceWith<number>((a, b) => a === b)(array1, array2)
1482
+ * assert.deepStrictEqual(difference, [1])
1483
+ *
956
1484
  * @since 2.0.0
957
1485
  */
958
1486
  export const differenceWith = isEquivalent => {
@@ -963,6 +1491,14 @@ export const differenceWith = isEquivalent => {
963
1491
  * Creates a `Array` of values not included in the other given `Iterable`.
964
1492
  * The order and references of result values are determined by the first `Iterable`.
965
1493
  *
1494
+ * @example
1495
+ * import { Array } from "effect"
1496
+ *
1497
+ * const array1 = [1, 2, 3]
1498
+ * const array2 = [2, 3, 4]
1499
+ * const difference = Array.difference(array1, array2)
1500
+ * assert.deepStrictEqual(difference, [1])
1501
+ *
966
1502
  * @since 2.0.0
967
1503
  */
968
1504
  export const difference = /*#__PURE__*/differenceWith(_equivalence);
@@ -1003,13 +1539,35 @@ export const flatMap = /*#__PURE__*/dual(2, (self, f) => {
1003
1539
  return out;
1004
1540
  });
1005
1541
  /**
1006
- * Flattens an array of arrays into a single array by concatenating all arrays.
1542
+ * Combines multiple arrays into a single array by concatenating all elements
1543
+ * from each nested array. This function ensures that the structure of nested
1544
+ * arrays is collapsed into a single, flat array.
1545
+ *
1546
+ * @example
1547
+ * import { Array } from "effect";
1548
+ *
1549
+ * const nestedArrays = [[1, 2], [], [3, 4], [], [5, 6]]
1550
+ * const result = Array.flatten(nestedArrays)
1551
+ *
1552
+ * assert.deepStrictEqual(result, [1, 2, 3, 4, 5, 6]);
1007
1553
  *
1008
1554
  * @category sequencing
1009
1555
  * @since 2.0.0
1010
1556
  */
1011
1557
  export const flatten = /*#__PURE__*/flatMap(identity);
1012
1558
  /**
1559
+ * Applies a function to each element of the `Iterable` and filters based on the result, keeping the transformed values where the function returns `Some`.
1560
+ * This method combines filtering and mapping functionalities, allowing transformations and filtering of elements based on a single function pass.
1561
+ *
1562
+ * @example
1563
+ * import { Array, Option } from "effect";
1564
+ *
1565
+ * const data = [1, 2, 3, 4, 5];
1566
+ * const evenSquares = (x: number) => x % 2 === 0 ? Option.some(x * x) : Option.none();
1567
+ * const result = Array.filterMap(data, evenSquares);
1568
+ *
1569
+ * assert.deepStrictEqual(result, [4, 16]);
1570
+ *
1013
1571
  * @category filtering
1014
1572
  * @since 2.0.0
1015
1573
  */
@@ -1025,7 +1583,18 @@ export const filterMap = /*#__PURE__*/dual(2, (self, f) => {
1025
1583
  return out;
1026
1584
  });
1027
1585
  /**
1028
- * Transforms all elements of the `readonlyArray` for as long as the specified function returns some value
1586
+ * Applies a function to each element of the array and filters based on the result, stopping when a condition is not met.
1587
+ * This method combines filtering and mapping in a single pass, and short-circuits, i.e., stops processing, as soon as the function returns `None`.
1588
+ * This is useful when you need to transform an array but only up to the point where a certain condition holds true.
1589
+ *
1590
+ * @example
1591
+ * import { Array, Option } from "effect";
1592
+ *
1593
+ * const data = [2, 4, 5];
1594
+ * const toSquareTillOdd = (x: number) => x % 2 === 0 ? Option.some(x * x) : Option.none();
1595
+ * const result = Array.filterMapWhile(data, toSquareTillOdd);
1596
+ *
1597
+ * assert.deepStrictEqual(result, [4, 16]);
1029
1598
  *
1030
1599
  * @category filtering
1031
1600
  * @since 2.0.0
@@ -1045,6 +1614,25 @@ export const filterMapWhile = /*#__PURE__*/dual(2, (self, f) => {
1045
1614
  return out;
1046
1615
  });
1047
1616
  /**
1617
+ * Applies a function to each element of the `Iterable`, categorizing the results into two separate arrays.
1618
+ * This function is particularly useful for operations where each element can result in two possible types,
1619
+ * and you want to separate these types into different collections. For instance, separating validation results
1620
+ * into successes and failures.
1621
+ *
1622
+ * @example
1623
+ * import { Array, Either } from "effect";
1624
+ *
1625
+ * const data = [1, 2, 3, 4, 5]
1626
+ * const isEven = (x: number) => x % 2 === 0
1627
+ * const partitioned = Array.partitionMap(data, x =>
1628
+ * isEven(x) ? Either.right(x) : Either.left(x)
1629
+ * )
1630
+ *
1631
+ * assert.deepStrictEqual(partitioned, [
1632
+ * [1, 3, 5],
1633
+ * [2, 4]
1634
+ * ])
1635
+ *
1048
1636
  * @category filtering
1049
1637
  * @since 2.0.0
1050
1638
  */
@@ -1157,21 +1745,58 @@ export const partition = /*#__PURE__*/dual(2, (self, predicate) => {
1157
1745
  return [left, right];
1158
1746
  });
1159
1747
  /**
1748
+ * Separates an `Iterable` into two arrays based on a predicate.
1749
+ *
1750
+ * @example
1751
+ * import { Array } from "effect"
1752
+ *
1753
+ * const numbers = [1, 2, 3, 4]
1754
+ * const result = Array.partition(numbers, n => n % 2 === 0)
1755
+ * assert.deepStrictEqual(result, [[1, 3], [2, 4]])
1756
+ *
1160
1757
  * @category filtering
1161
1758
  * @since 2.0.0
1162
1759
  */
1163
1760
  export const separate = /*#__PURE__*/partitionMap(identity);
1164
1761
  /**
1762
+ * Reduces an array from the left.
1763
+ *
1764
+ * @example
1765
+ * import { Array } from "effect"
1766
+ *
1767
+ * const numbers = [1, 2, 3]
1768
+ * const result = Array.reduce(numbers, 0, (acc, n) => acc + n)
1769
+ * assert.deepStrictEqual(result, 6)
1770
+ *
1165
1771
  * @category folding
1166
1772
  * @since 2.0.0
1167
1773
  */
1168
1774
  export const reduce = /*#__PURE__*/dual(3, (self, b, f) => fromIterable(self).reduce((b, a, i) => f(b, a, i), b));
1169
1775
  /**
1776
+ * Reduces an array from the right.
1777
+ *
1778
+ * @example
1779
+ * import { Array } from "effect"
1780
+ *
1781
+ * const numbers = [1, 2, 3]
1782
+ * const result = Array.reduceRight(numbers, 0, (acc, n) => acc + n)
1783
+ * assert.deepStrictEqual(result, 6)
1784
+ *
1170
1785
  * @category folding
1171
1786
  * @since 2.0.0
1172
1787
  */
1173
1788
  export const reduceRight = /*#__PURE__*/dual(3, (self, b, f) => fromIterable(self).reduceRight((b, a, i) => f(b, a, i), b));
1174
1789
  /**
1790
+ * Lifts a predicate into an array.
1791
+ *
1792
+ * @example
1793
+ * import { Array } from "effect"
1794
+ *
1795
+ * const isEven = (n: number) => n % 2 === 0
1796
+ * const to = Array.liftPredicate(isEven)
1797
+ * assert.deepStrictEqual(to(1), [])
1798
+ * assert.deepStrictEqual(to(2), [2])
1799
+ *
1175
1800
  * @category lifting
1176
1801
  * @since 2.0.0
1177
1802
  */
@@ -1192,11 +1817,48 @@ export const fromNullable = a => a == null ? empty() : [a];
1192
1817
  */
1193
1818
  export const liftNullable = f => (...a) => fromNullable(f(...a));
1194
1819
  /**
1820
+ * Maps over an array and flattens the result, removing null and undefined values.
1821
+ *
1822
+ * @example
1823
+ * import { Array } from "effect"
1824
+ *
1825
+ * const numbers = [1, 2, 3]
1826
+ * const result = Array.flatMapNullable(numbers, n => (n % 2 === 0 ? null : n))
1827
+ * assert.deepStrictEqual(result, [1, 3])
1828
+ *
1829
+ * // Explanation:
1830
+ * // The array of numbers [1, 2, 3] is mapped with a function that returns null for even numbers
1831
+ * // and the number itself for odd numbers. The resulting array [1, null, 3] is then flattened
1832
+ * // to remove null values, resulting in [1, 3].
1833
+ *
1195
1834
  * @category sequencing
1196
1835
  * @since 2.0.0
1197
1836
  */
1198
- export const flatMapNullable = /*#__PURE__*/dual(2, (self, f) => isNonEmptyReadonlyArray(self) ? fromNullable(f(headNonEmpty(self))) : empty());
1837
+ export const flatMapNullable = /*#__PURE__*/dual(2, (self, f) => flatMap(self, a => fromNullable(f(a))));
1199
1838
  /**
1839
+ * Lifts a function that returns an `Either` into a function that returns an array.
1840
+ * If the `Either` is a left, it returns an empty array.
1841
+ * If the `Either` is a right, it returns an array with the right value.
1842
+ *
1843
+ * @example
1844
+ * import { Array, Either } from "effect"
1845
+ *
1846
+ * const parseNumber = (s: string): Either.Either<number, Error> =>
1847
+ * isNaN(Number(s)) ? Either.left(new Error("Not a number")) : Either.right(Number(s))
1848
+ *
1849
+ * const liftedParseNumber = Array.liftEither(parseNumber)
1850
+ *
1851
+ * const result1 = liftedParseNumber("42")
1852
+ * assert.deepStrictEqual(result1, [42])
1853
+ *
1854
+ * const result2 = liftedParseNumber("not a number")
1855
+ * assert.deepStrictEqual(result2, [])
1856
+ *
1857
+ * // Explanation:
1858
+ * // The function parseNumber is lifted to return an array.
1859
+ * // When parsing "42", it returns an Either.left with the number 42, resulting in [42].
1860
+ * // When parsing "not a number", it returns an Either.right with an error, resulting in an empty array [].
1861
+ *
1200
1862
  * @category lifting
1201
1863
  * @since 2.0.0
1202
1864
  */
@@ -1219,14 +1881,45 @@ export const every = /*#__PURE__*/dual(2, (self, refinement) => self.every(refin
1219
1881
  */
1220
1882
  export const some = /*#__PURE__*/dual(2, (self, predicate) => self.some(predicate));
1221
1883
  /**
1884
+ * Extends an array with a function that maps each subarray to a value.
1885
+ *
1886
+ * @example
1887
+ * import { Array } from "effect"
1888
+ *
1889
+ * const numbers = [1, 2, 3]
1890
+ * const result = Array.extend(numbers, as => as.length)
1891
+ * assert.deepStrictEqual(result, [3, 2, 1])
1892
+ *
1893
+ * // Explanation:
1894
+ * // The function maps each subarray starting from each element to its length.
1895
+ * // The subarrays are: [1, 2, 3], [2, 3], [3].
1896
+ * // The lengths are: 3, 2, 1.
1897
+ * // Therefore, the result is [3, 2, 1].
1898
+ *
1222
1899
  * @since 2.0.0
1223
1900
  */
1224
1901
  export const extend = /*#__PURE__*/dual(2, (self, f) => self.map((_, i, as) => f(as.slice(i))));
1225
1902
  /**
1903
+ * Finds the minimum element in an array based on a comparator.
1904
+ *
1905
+ * @example
1906
+ * import { Array, Order } from "effect"
1907
+ *
1908
+ * const min = Array.min([3, 1, 2], Order.number)
1909
+ * assert.deepStrictEqual(min, 1)
1910
+ *
1226
1911
  * @since 2.0.0
1227
1912
  */
1228
1913
  export const min = /*#__PURE__*/dual(2, (self, O) => self.reduce(Order.min(O)));
1229
1914
  /**
1915
+ * Finds the maximum element in an array based on a comparator.
1916
+ *
1917
+ * @example
1918
+ * import { Array, Order } from "effect"
1919
+ *
1920
+ * const max = Array.max([3, 1, 2], Order.number)
1921
+ * assert.deepStrictEqual(max, 3)
1922
+ *
1230
1923
  * @since 2.0.0
1231
1924
  */
1232
1925
  export const max = /*#__PURE__*/dual(2, (self, O) => self.reduce(Order.max(O)));
@@ -1256,12 +1949,28 @@ export const unfold = (b, f) => {
1256
1949
  */
1257
1950
  export const getOrder = Order.array;
1258
1951
  /**
1952
+ * Creates an equivalence relation for arrays.
1953
+ *
1954
+ * @example
1955
+ * import { Array } from "effect"
1956
+ *
1957
+ * const numbers1 = [1, 2, 3]
1958
+ * const numbers2 = [1, 2, 3]
1959
+ * const eq = Array.getEquivalence<number>((a, b) => a === b)
1960
+ * assert.deepStrictEqual(eq(numbers1, numbers2), true)
1961
+ *
1259
1962
  * @category instances
1260
1963
  * @since 2.0.0
1261
1964
  */
1262
1965
  export const getEquivalence = Equivalence.array;
1263
1966
  /**
1264
- * Iterate over the `Iterable` applying `f`.
1967
+ * Performs a side-effect for each element of the `Iterable`.
1968
+ *
1969
+ * @example
1970
+ * import { Array } from "effect"
1971
+ *
1972
+ * const numbers = [1, 2, 3]
1973
+ * Array.forEach(numbers, n => console.log(n)) // 1, 2, 3
1265
1974
  *
1266
1975
  * @since 2.0.0
1267
1976
  */
@@ -1270,6 +1979,13 @@ export const forEach = /*#__PURE__*/dual(2, (self, f) => fromIterable(self).forE
1270
1979
  * Remove duplicates from an `Iterable` using the provided `isEquivalent` function,
1271
1980
  * preserving the order of the first occurrence of each element.
1272
1981
  *
1982
+ * @example
1983
+ * import { Array } from "effect"
1984
+ *
1985
+ * const numbers = [1, 2, 2, 3, 3, 3]
1986
+ * const unique = Array.dedupeWith(numbers, (a, b) => a === b)
1987
+ * assert.deepStrictEqual(unique, [1, 2, 3])
1988
+ *
1273
1989
  * @since 2.0.0
1274
1990
  */
1275
1991
  export const dedupeWith = /*#__PURE__*/dual(2, (self, isEquivalent) => {
@@ -1296,6 +2012,13 @@ export const dedupe = self => dedupeWith(self, Equal.equivalence());
1296
2012
  /**
1297
2013
  * Deduplicates adjacent elements that are identical using the provided `isEquivalent` function.
1298
2014
  *
2015
+ * @example
2016
+ * import { Array } from "effect"
2017
+ *
2018
+ * const numbers = [1, 1, 2, 2, 3, 3]
2019
+ * const unique = Array.dedupeAdjacentWith(numbers, (a, b) => a === b)
2020
+ * assert.deepStrictEqual(unique, [1, 2, 3])
2021
+ *
1299
2022
  * @since 2.0.0
1300
2023
  */
1301
2024
  export const dedupeAdjacentWith = /*#__PURE__*/dual(2, (self, isEquivalent) => {
@@ -1312,12 +2035,26 @@ export const dedupeAdjacentWith = /*#__PURE__*/dual(2, (self, isEquivalent) => {
1312
2035
  /**
1313
2036
  * Deduplicates adjacent elements that are identical.
1314
2037
  *
2038
+ * @example
2039
+ * import { Array } from "effect"
2040
+ *
2041
+ * const numbers = [1, 1, 2, 2, 3, 3]
2042
+ * const unique = Array.dedupeAdjacent(numbers)
2043
+ * assert.deepStrictEqual(unique, [1, 2, 3])
2044
+ *
1315
2045
  * @since 2.0.0
1316
2046
  */
1317
2047
  export const dedupeAdjacent = /*#__PURE__*/dedupeAdjacentWith( /*#__PURE__*/Equal.equivalence());
1318
2048
  /**
1319
2049
  * Joins the elements together with "sep" in the middle.
1320
2050
  *
2051
+ * @example
2052
+ * import { Array } from "effect"
2053
+ *
2054
+ * const strings = ["a", "b", "c"]
2055
+ * const joined = Array.join(strings, "-")
2056
+ * assert.deepStrictEqual(joined, "a-b-c")
2057
+ *
1321
2058
  * @since 2.0.0
1322
2059
  * @category folding
1323
2060
  */
@@ -1325,6 +2062,13 @@ export const join = /*#__PURE__*/dual(2, (self, sep) => fromIterable(self).join(
1325
2062
  /**
1326
2063
  * Statefully maps over the chunk, producing new elements of type `B`.
1327
2064
  *
2065
+ * @example
2066
+ * import { Array } from "effect"
2067
+ *
2068
+ * const numbers = [1, 2, 3]
2069
+ * const result = Array.mapAccum(numbers, 0, (acc, n) => [acc + n, acc + n])
2070
+ * assert.deepStrictEqual(result, [6, [1, 3, 6]])
2071
+ *
1328
2072
  * @since 2.0.0
1329
2073
  * @category folding
1330
2074
  */
@@ -1343,6 +2087,14 @@ export const mapAccum = /*#__PURE__*/dual(3, (self, s, f) => {
1343
2087
  /**
1344
2088
  * Zips this chunk crosswise with the specified chunk using the specified combiner.
1345
2089
  *
2090
+ * @example
2091
+ * import { Array } from "effect"
2092
+ *
2093
+ * const array1 = [1, 2]
2094
+ * const array2 = ["a", "b"]
2095
+ * const product = Array.cartesianWith(array1, array2, (a, b) => `${a}-${b}`)
2096
+ * assert.deepStrictEqual(product, ["1-a", "1-b", "2-a", "2-b"])
2097
+ *
1346
2098
  * @since 2.0.0
1347
2099
  * @category elements
1348
2100
  */
@@ -1350,6 +2102,14 @@ export const cartesianWith = /*#__PURE__*/dual(3, (self, that, f) => flatMap(sel
1350
2102
  /**
1351
2103
  * Zips this chunk crosswise with the specified chunk.
1352
2104
  *
2105
+ * @example
2106
+ * import { Array } from "effect"
2107
+ *
2108
+ * const array1 = [1, 2]
2109
+ * const array2 = ["a", "b"]
2110
+ * const product = Array.cartesian(array1, array2)
2111
+ * assert.deepStrictEqual(product, [[1, "a"], [1, "b"], [2, "a"], [2, "b"]])
2112
+ *
1353
2113
  * @since 2.0.0
1354
2114
  * @category elements
1355
2115
  */