effect 3.2.3 → 3.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/Array.ts CHANGED
@@ -46,6 +46,12 @@ export type NonEmptyArray<A> = [A, ...Array<A>]
46
46
  /**
47
47
  * Builds a `NonEmptyArray` from an non-empty collection of elements.
48
48
  *
49
+ * @example
50
+ * import { Array } from "effect"
51
+ *
52
+ * const result = Array.make(1, 2, 3)
53
+ * assert.deepStrictEqual(result, [1, 2, 3])
54
+ *
49
55
  * @category constructors
50
56
  * @since 2.0.0
51
57
  */
@@ -56,6 +62,12 @@ export const make = <Elements extends NonEmptyArray<any>>(
56
62
  /**
57
63
  * Creates a new `Array` of the specified length.
58
64
  *
65
+ * @example
66
+ * import { Array } from "effect"
67
+ *
68
+ * const result = Array.allocate<number>(3)
69
+ * assert.deepStrictEqual(result.length, 3)
70
+ *
59
71
  * @category constructors
60
72
  * @since 2.0.0
61
73
  */
@@ -103,9 +115,9 @@ export const range = (start: number, end: number): NonEmptyArray<number> =>
103
115
  * **Note**. `n` is normalized to an integer >= 1.
104
116
  *
105
117
  * @example
106
- * import { replicate } from "effect/Array"
118
+ * import { Array } from "effect"
107
119
  *
108
- * assert.deepStrictEqual(replicate("a", 3), ["a", "a", "a"])
120
+ * assert.deepStrictEqual(Array.replicate("a", 3), ["a", "a", "a"])
109
121
  *
110
122
  * @category constructors
111
123
  * @since 2.0.0
@@ -117,6 +129,15 @@ export const replicate: {
117
129
 
118
130
  /**
119
131
  * Creates a new `Array` from an iterable collection of values.
132
+ * If the input is already an array, it returns the input as-is.
133
+ * Otherwise, it converts the iterable collection to an array.
134
+ *
135
+ * @example
136
+ * import { Array } from "effect"
137
+ *
138
+ * const set = new Set([1, 2, 3])
139
+ * const result = Array.fromIterable(set)
140
+ * assert.deepStrictEqual(result, [1, 2, 3])
120
141
  *
121
142
  * @category constructors
122
143
  * @since 2.0.0
@@ -130,10 +151,10 @@ export const fromIterable = <A>(collection: Iterable<A>): Array<A> =>
130
151
  * @param self - The record to transform.
131
152
  *
132
153
  * @example
133
- * import { fromRecord } from "effect/Array"
154
+ * import { Array } from "effect"
134
155
  *
135
156
  * const x = { a: 1, b: 2, c: 3 }
136
- * assert.deepStrictEqual(fromRecord(x), [["a", 1], ["b", 2], ["c", 3]])
157
+ * assert.deepStrictEqual(Array.fromRecord(x), [["a", 1], ["b", 2], ["c", 3]])
137
158
  *
138
159
  * @category conversions
139
160
  * @since 2.0.0
@@ -141,12 +162,32 @@ export const fromIterable = <A>(collection: Iterable<A>): Array<A> =>
141
162
  export const fromRecord: <K extends string, A>(self: Readonly<Record<K, A>>) => Array<[K, A]> = Record.toEntries
142
163
 
143
164
  /**
165
+ * Converts an `Option` to an array.
166
+ *
167
+ * @example
168
+ * import { Array, Option } from "effect"
169
+ *
170
+ * assert.deepStrictEqual(Array.fromOption(Option.some(1)), [1])
171
+ * assert.deepStrictEqual(Array.fromOption(Option.none()), [])
172
+ *
144
173
  * @category conversions
145
174
  * @since 2.0.0
146
175
  */
147
176
  export const fromOption: <A>(self: Option<A>) => Array<A> = O.toArray
148
177
 
149
178
  /**
179
+ * Matches the elements of an array, applying functions to cases of empty and non-empty arrays.
180
+ *
181
+ * @example
182
+ * import { Array } from "effect"
183
+ *
184
+ * const match = Array.match({
185
+ * onEmpty: () => "empty",
186
+ * onNonEmpty: ([head, ...tail]) => `head: ${head}, tail: ${tail.length}`
187
+ * })
188
+ * assert.deepStrictEqual(match([]), "empty")
189
+ * assert.deepStrictEqual(match([1, 2, 3]), "head: 1, tail: 2")
190
+ *
150
191
  * @category pattern matching
151
192
  * @since 2.0.0
152
193
  */
@@ -173,6 +214,18 @@ export const match: {
173
214
  ): B | C => isNonEmptyReadonlyArray(self) ? onNonEmpty(self) : onEmpty())
174
215
 
175
216
  /**
217
+ * Matches the elements of an array from the left, applying functions to cases of empty and non-empty arrays.
218
+ *
219
+ * @example
220
+ * import { Array } from "effect"
221
+ *
222
+ * const matchLeft = Array.matchLeft({
223
+ * onEmpty: () => "empty",
224
+ * onNonEmpty: (head, tail) => `head: ${head}, tail: ${tail.length}`
225
+ * })
226
+ * assert.deepStrictEqual(matchLeft([]), "empty")
227
+ * assert.deepStrictEqual(matchLeft([1, 2, 3]), "head: 1, tail: 2")
228
+ *
176
229
  * @category pattern matching
177
230
  * @since 2.0.0
178
231
  */
@@ -199,6 +252,18 @@ export const matchLeft: {
199
252
  ): B | C => isNonEmptyReadonlyArray(self) ? onNonEmpty(headNonEmpty(self), tailNonEmpty(self)) : onEmpty())
200
253
 
201
254
  /**
255
+ * Matches the elements of an array from the right, applying functions to cases of empty and non-empty arrays.
256
+ *
257
+ * @example
258
+ * import { Array } from "effect"
259
+ *
260
+ * const matchRight = Array.matchRight({
261
+ * onEmpty: () => "empty",
262
+ * onNonEmpty: (init, last) => `init: ${init.length}, last: ${last}`
263
+ * })
264
+ * assert.deepStrictEqual(matchRight([]), "empty")
265
+ * assert.deepStrictEqual(matchRight([1, 2, 3]), "init: 2, last: 3")
266
+ *
202
267
  * @category pattern matching
203
268
  * @since 2.0.0
204
269
  */
@@ -230,6 +295,13 @@ export const matchRight: {
230
295
  /**
231
296
  * Prepend an element to the front of an `Iterable`, creating a new `NonEmptyArray`.
232
297
  *
298
+ * @example
299
+ * import { Array } from "effect"
300
+ *
301
+ * const original = [2, 3, 4];
302
+ * const result = Array.prepend(original, 1);
303
+ * assert.deepStrictEqual(result, [1, 2, 3, 4]);
304
+ *
233
305
  * @category concatenating
234
306
  * @since 2.0.0
235
307
  */
@@ -245,10 +317,10 @@ export const prepend: {
245
317
  * @example
246
318
  * import { Array } from "effect"
247
319
  *
248
- * assert.deepStrictEqual(
249
- * Array.prependAll([1, 2], ["a", "b"]),
250
- * ["a", "b", 1, 2]
251
- * )
320
+ * const prefix = [0, 1];
321
+ * const array = [2, 3];
322
+ * const result = Array.prependAll(array, prefix);
323
+ * assert.deepStrictEqual(result, [0, 1, 2, 3]);
252
324
  *
253
325
  * @category concatenating
254
326
  * @since 2.0.0
@@ -268,6 +340,13 @@ export const prependAll: {
268
340
  /**
269
341
  * Append an element to the end of an `Iterable`, creating a new `NonEmptyArray`.
270
342
  *
343
+ * @example
344
+ * import { Array } from "effect"
345
+ *
346
+ * const original = [1, 2, 3];
347
+ * const result = Array.append(original, 4);
348
+ * assert.deepStrictEqual(result, [1, 2, 3, 4]);
349
+ *
271
350
  * @category concatenating
272
351
  * @since 2.0.0
273
352
  */
@@ -296,7 +375,22 @@ export const appendAll: {
296
375
  )
297
376
 
298
377
  /**
299
- * Reduce an `Iterable` from the left, keeping all intermediate results instead of only the final result.
378
+ * Accumulates values from an `Iterable` starting from the left, storing
379
+ * each intermediate result in an array. Useful for tracking the progression of
380
+ * a value through a series of transformations.
381
+ *
382
+ * @example
383
+ * import { Array } from "effect";
384
+ *
385
+ * const numbers = [1, 2, 3, 4]
386
+ * const result = Array.scan(numbers, 0, (acc, value) => acc + value)
387
+ * assert.deepStrictEqual(result, [0, 1, 3, 6, 10])
388
+ *
389
+ * // Explanation:
390
+ * // This function starts with the initial value (0 in this case)
391
+ * // and adds each element of the array to this accumulator one by one,
392
+ * // keeping track of the cumulative sum after each addition.
393
+ * // Each of these sums is captured in the resulting array.
300
394
  *
301
395
  * @category folding
302
396
  * @since 2.0.0
@@ -315,7 +409,16 @@ export const scan: {
315
409
  })
316
410
 
317
411
  /**
318
- * Reduce an `Iterable` from the right, keeping all intermediate results instead of only the final result.
412
+ * Accumulates values from an `Iterable` starting from the right, storing
413
+ * each intermediate result in an array. Useful for tracking the progression of
414
+ * a value through a series of transformations.
415
+ *
416
+ * @example
417
+ * import { Array } from "effect";
418
+ *
419
+ * const numbers = [1, 2, 3, 4]
420
+ * const result = Array.scanRight(numbers, 0, (acc, value) => acc + value)
421
+ * assert.deepStrictEqual(result, [10, 9, 7, 4, 0])
319
422
  *
320
423
  * @category folding
321
424
  * @since 2.0.0
@@ -467,6 +570,12 @@ export const unsafeGet: {
467
570
  /**
468
571
  * Return a tuple containing the first element, and a new `Array` of the remaining elements, if any.
469
572
  *
573
+ * @example
574
+ * import { Array } from "effect";
575
+ *
576
+ * const result = Array.unprepend([1, 2, 3, 4])
577
+ * assert.deepStrictEqual(result, [1, [2, 3, 4]])
578
+ *
470
579
  * @category splitting
471
580
  * @since 2.0.0
472
581
  */
@@ -477,6 +586,12 @@ export const unprepend = <A>(
477
586
  /**
478
587
  * Return a tuple containing a copy of the `NonEmptyReadonlyArray` without its last element, and that last element.
479
588
  *
589
+ * @example
590
+ * import { Array } from "effect";
591
+ *
592
+ * const result = Array.unappend([1, 2, 3, 4])
593
+ * assert.deepStrictEqual(result, [[1, 2, 3], 4])
594
+ *
480
595
  * @category splitting
481
596
  * @since 2.0.0
482
597
  */
@@ -493,6 +608,14 @@ export const unappend = <A>(
493
608
  export const head: <A>(self: ReadonlyArray<A>) => Option<A> = get(0)
494
609
 
495
610
  /**
611
+ * Get the first element of a non empty array.
612
+ *
613
+ * @example
614
+ * import { Array } from "effect"
615
+ *
616
+ * const result = Array.headNonEmpty([1, 2, 3, 4])
617
+ * assert.deepStrictEqual(result, 1)
618
+ *
496
619
  * @category getters
497
620
  * @since 2.0.0
498
621
  */
@@ -508,6 +631,14 @@ export const last = <A>(self: ReadonlyArray<A>): Option<A> =>
508
631
  isNonEmptyReadonlyArray(self) ? O.some(lastNonEmpty(self)) : O.none()
509
632
 
510
633
  /**
634
+ * Get the last element of a non empty array.
635
+ *
636
+ * @example
637
+ * import { Array } from "effect"
638
+ *
639
+ * const result = Array.lastNonEmpty([1, 2, 3, 4])
640
+ * assert.deepStrictEqual(result, 4)
641
+ *
511
642
  * @category getters
512
643
  * @since 2.0.0
513
644
  */
@@ -525,6 +656,14 @@ export const tail = <A>(self: Iterable<A>): Option<Array<A>> => {
525
656
  }
526
657
 
527
658
  /**
659
+ * Get all but the first element of a `NonEmptyReadonlyArray`.
660
+ *
661
+ * @example
662
+ * import { Array } from "effect"
663
+ *
664
+ * const result = Array.tailNonEmpty([1, 2, 3, 4])
665
+ * assert.deepStrictEqual(result, [2, 3, 4])
666
+ *
528
667
  * @category getters
529
668
  * @since 2.0.0
530
669
  */
@@ -544,6 +683,12 @@ export const init = <A>(self: Iterable<A>): Option<Array<A>> => {
544
683
  /**
545
684
  * Get all but the last element of a non empty array, creating a new array.
546
685
  *
686
+ * @example
687
+ * import { Array } from "effect"
688
+ *
689
+ * const result = Array.initNonEmpty([1, 2, 3, 4])
690
+ * assert.deepStrictEqual(result, [1, 2, 3])
691
+ *
547
692
  * @category getters
548
693
  * @since 2.0.0
549
694
  */
@@ -554,6 +699,13 @@ export const initNonEmpty = <A>(self: NonEmptyReadonlyArray<A>): Array<A> => sel
554
699
  *
555
700
  * **Note**. `n` is normalized to a non negative integer.
556
701
  *
702
+ * @example
703
+ * import { Array } from "effect"
704
+ *
705
+ * const numbers = [1, 2, 3, 4, 5]
706
+ * const result = Array.take(numbers, 3)
707
+ * assert.deepStrictEqual(result, [1, 2, 3])
708
+ *
557
709
  * @category getters
558
710
  * @since 2.0.0
559
711
  */
@@ -570,6 +722,13 @@ export const take: {
570
722
  *
571
723
  * **Note**. `n` is normalized to a non negative integer.
572
724
  *
725
+ * @example
726
+ * import { Array } from "effect"
727
+ *
728
+ * const numbers = [1, 2, 3, 4, 5]
729
+ * const result = Array.takeRight(numbers, 3)
730
+ * assert.deepStrictEqual(result, [3, 4, 5])
731
+ *
573
732
  * @category getters
574
733
  * @since 2.0.0
575
734
  */
@@ -585,6 +744,19 @@ export const takeRight: {
585
744
  /**
586
745
  * Calculate the longest initial subarray for which all element satisfy the specified predicate, creating a new `Array`.
587
746
  *
747
+ * @example
748
+ * import { Array } from "effect"
749
+ *
750
+ * const numbers = [1, 3, 2, 4, 1, 2]
751
+ * const result = Array.takeWhile(numbers, x => x < 4)
752
+ * assert.deepStrictEqual(result, [1, 3, 2])
753
+ *
754
+ * // Explanation:
755
+ * // - The function starts with the first element (`1`), which is less than `4`, so it adds `1` to the result.
756
+ * // - The next element (`3`) is also less than `4`, so it adds `3`.
757
+ * // - The next element (`2`) is again less than `4`, so it adds `2`.
758
+ * // - The function then encounters `4`, which is not less than `4`. At this point, it stops checking further elements and finalizes the result.
759
+ *
588
760
  * @category getters
589
761
  * @since 2.0.0
590
762
  */
@@ -647,6 +819,13 @@ export const span: {
647
819
  *
648
820
  * **Note**. `n` is normalized to a non negative integer.
649
821
  *
822
+ * @example
823
+ * import { Array } from "effect"
824
+ *
825
+ * const numbers = [1, 2, 3, 4, 5]
826
+ * const result = Array.drop(numbers, 2)
827
+ * assert.deepStrictEqual(result, [3, 4, 5])
828
+ *
650
829
  * @category getters
651
830
  * @since 2.0.0
652
831
  */
@@ -663,6 +842,13 @@ export const drop: {
663
842
  *
664
843
  * **Note**. `n` is normalized to a non negative integer.
665
844
  *
845
+ * @example
846
+ * import { Array } from "effect"
847
+ *
848
+ * const numbers = [1, 2, 3, 4, 5]
849
+ * const result = Array.dropRight(numbers, 2)
850
+ * assert.deepStrictEqual(result, [1, 2, 3])
851
+ *
666
852
  * @category getters
667
853
  * @since 2.0.0
668
854
  */
@@ -677,6 +863,13 @@ export const dropRight: {
677
863
  /**
678
864
  * Remove the longest initial subarray for which all element satisfy the specified predicate, creating a new `Array`.
679
865
  *
866
+ * @example
867
+ * import { Array } from "effect"
868
+ *
869
+ * const numbers = [1, 2, 3, 4, 5]
870
+ * const result = Array.dropWhile(numbers, x => x < 4)
871
+ * assert.deepStrictEqual(result, [4, 5])
872
+ *
680
873
  * @category getters
681
874
  * @since 2.0.0
682
875
  */
@@ -692,6 +885,13 @@ export const dropWhile: {
692
885
  /**
693
886
  * Return the first index for which a predicate holds.
694
887
  *
888
+ * @example
889
+ * import { Array, Option } from "effect"
890
+ *
891
+ * const numbers = [5, 3, 8, 9]
892
+ * const result = Array.findFirstIndex(numbers, x => x > 5)
893
+ * assert.deepStrictEqual(result, Option.some(2))
894
+ *
695
895
  * @category elements
696
896
  * @since 2.0.0
697
897
  */
@@ -712,6 +912,13 @@ export const findFirstIndex: {
712
912
  /**
713
913
  * Return the last index for which a predicate holds.
714
914
  *
915
+ * @example
916
+ * import { Array, Option } from "effect"
917
+ *
918
+ * const numbers = [1, 3, 8, 9]
919
+ * const result = Array.findLastIndex(numbers, x => x < 5)
920
+ * assert.deepStrictEqual(result, Option.some(1))
921
+ *
715
922
  * @category elements
716
923
  * @since 2.0.0
717
924
  */
@@ -732,6 +939,13 @@ export const findLastIndex: {
732
939
  * Returns the first element that satisfies the specified
733
940
  * predicate, or `None` if no such element exists.
734
941
  *
942
+ * @example
943
+ * import { Array, Option } from "effect"
944
+ *
945
+ * const numbers = [1, 2, 3, 4, 5]
946
+ * const result = Array.findFirst(numbers, x => x > 3)
947
+ * assert.deepStrictEqual(result, Option.some(4))
948
+ *
735
949
  * @category elements
736
950
  * @since 2.0.0
737
951
  */
@@ -745,7 +959,15 @@ export const findFirst: {
745
959
  } = EffectIterable.findFirst
746
960
 
747
961
  /**
748
- * Find the last element for which a predicate holds.
962
+ * Finds the last element in an iterable collection that satisfies the given predicate or refinement.
963
+ * Returns an `Option` containing the found element, or `Option.none` if no element matches.
964
+ *
965
+ * @example
966
+ * import { Array, Option } from "effect"
967
+ *
968
+ * const numbers = [1, 2, 3, 4, 5]
969
+ * const result = Array.findLast(numbers, n => n % 2 === 0)
970
+ * assert.deepStrictEqual(result, Option.some(4))
749
971
  *
750
972
  * @category elements
751
973
  * @since 2.0.0
@@ -782,6 +1004,13 @@ export const findLast: {
782
1004
  * Insert an element at the specified index, creating a new `NonEmptyArray`,
783
1005
  * or return `None` if the index is out of bounds.
784
1006
  *
1007
+ * @example
1008
+ * import { Array, Option } from "effect"
1009
+ *
1010
+ * const letters = ['a', 'b', 'c', 'e']
1011
+ * const result = Array.insertAt(letters, 3, 'd')
1012
+ * assert.deepStrictEqual(result, Option.some(['a', 'b', 'c', 'd', 'e']))
1013
+ *
785
1014
  * @since 2.0.0
786
1015
  */
787
1016
  export const insertAt: {
@@ -801,6 +1030,13 @@ export const insertAt: {
801
1030
  * Change the element at the specified index, creating a new `Array`,
802
1031
  * or return a copy of the input if the index is out of bounds.
803
1032
  *
1033
+ * @example
1034
+ * import { Array } from "effect"
1035
+ *
1036
+ * const letters = ['a', 'b', 'c', 'd']
1037
+ * const result = Array.replace(1, 'z')(letters)
1038
+ * assert.deepStrictEqual(result, ['a', 'z', 'c', 'd'])
1039
+ *
804
1040
  * @since 2.0.0
805
1041
  */
806
1042
  export const replace: {
@@ -809,6 +1045,15 @@ export const replace: {
809
1045
  } = dual(3, <A, B>(self: Iterable<A>, i: number, b: B): Array<A | B> => modify(self, i, () => b))
810
1046
 
811
1047
  /**
1048
+ * Replaces an element in an array with the given value, returning an option of the updated array.
1049
+ *
1050
+ * @example
1051
+ * import { Array, Option } from "effect"
1052
+ *
1053
+ * const numbers = [1, 2, 3]
1054
+ * const result = Array.replaceOption(numbers, 1, 4)
1055
+ * assert.deepStrictEqual(result, Option.some([1, 4, 3]))
1056
+ *
812
1057
  * @since 2.0.0
813
1058
  */
814
1059
  export const replaceOption: {
@@ -823,6 +1068,13 @@ export const replaceOption: {
823
1068
  * Apply a function to the element at the specified index, creating a new `Array`,
824
1069
  * or return a copy of the input if the index is out of bounds.
825
1070
  *
1071
+ * @example
1072
+ * import { Array } from "effect"
1073
+ *
1074
+ * const numbers = [1, 2, 3, 4]
1075
+ * const result = Array.modify(numbers, 2, (n) => n * 2)
1076
+ * assert.deepStrictEqual(result, [1, 2, 6, 4])
1077
+ *
826
1078
  * @since 2.0.0
827
1079
  */
828
1080
  export const modify: {
@@ -838,6 +1090,16 @@ export const modify: {
838
1090
  * Apply a function to the element at the specified index, creating a new `Array`,
839
1091
  * or return `None` if the index is out of bounds.
840
1092
  *
1093
+ * @example
1094
+ * import { Array, Option } from "effect"
1095
+ *
1096
+ * const numbers = [1, 2, 3, 4]
1097
+ * const result = Array.modifyOption(numbers, 2, (n) => n * 2)
1098
+ * assert.deepStrictEqual(result, Option.some([1, 2, 6, 4]))
1099
+ *
1100
+ * const outOfBoundsResult = Array.modifyOption(numbers, 5, (n) => n * 2)
1101
+ * assert.deepStrictEqual(outOfBoundsResult, Option.none())
1102
+ *
841
1103
  * @since 2.0.0
842
1104
  */
843
1105
  export const modifyOption: {
@@ -858,6 +1120,16 @@ export const modifyOption: {
858
1120
  * Delete the element at the specified index, creating a new `Array`,
859
1121
  * or return a copy of the input if the index is out of bounds.
860
1122
  *
1123
+ * @example
1124
+ * import { Array } from "effect"
1125
+ *
1126
+ * const numbers = [1, 2, 3, 4]
1127
+ * const result = Array.remove(numbers, 2)
1128
+ * assert.deepStrictEqual(result, [1, 2, 4])
1129
+ *
1130
+ * const outOfBoundsResult = Array.remove(numbers, 5)
1131
+ * assert.deepStrictEqual(outOfBoundsResult, [1, 2, 3, 4])
1132
+ *
861
1133
  * @since 2.0.0
862
1134
  */
863
1135
  export const remove: {
@@ -875,6 +1147,13 @@ export const remove: {
875
1147
  /**
876
1148
  * Reverse an `Iterable`, creating a new `Array`.
877
1149
  *
1150
+ * @example
1151
+ * import { Array } from "effect"
1152
+ *
1153
+ * const numbers = [1, 2, 3, 4]
1154
+ * const result = Array.reverse(numbers)
1155
+ * assert.deepStrictEqual(result, [4, 3, 2, 1])
1156
+ *
878
1157
  * @category elements
879
1158
  * @since 2.0.0
880
1159
  */
@@ -903,6 +1182,22 @@ export const sort: {
903
1182
  })
904
1183
 
905
1184
  /**
1185
+ * Sorts an array based on a provided mapping function and order. The mapping
1186
+ * function transforms the elements into a value that can be compared, and the
1187
+ * order defines how those values should be sorted.
1188
+ *
1189
+ * @example
1190
+ * import { Array, Order } from "effect"
1191
+ *
1192
+ * const strings = ["aaa", "b", "cc"]
1193
+ * const result = Array.sortWith(strings, (s) => s.length, Order.number)
1194
+ * assert.deepStrictEqual(result, ["b", "cc", "aaa"])
1195
+ *
1196
+ * // Explanation:
1197
+ * // The array of strings is sorted based on their lengths. The mapping function `(s) => s.length`
1198
+ * // converts each string into its length, and the `Order.number` specifies that the lengths should
1199
+ * // be sorted in ascending order.
1200
+ *
906
1201
  * @since 2.0.0
907
1202
  * @category elements
908
1203
  */
@@ -919,8 +1214,33 @@ export const sortWith: {
919
1214
  )
920
1215
 
921
1216
  /**
922
- * Sort the elements of an `Iterable` in increasing order, where elements are compared
923
- * using first `orders[0]`, then `orders[1]`, etc...
1217
+ * Sorts the elements of an `Iterable` in increasing order based on the provided
1218
+ * orders. The elements are compared using the first order in `orders`, then the
1219
+ * second order if the first comparison is equal, and so on.
1220
+ *
1221
+ * @example
1222
+ * import { Array, Order } from "effect"
1223
+ *
1224
+ * const users = [
1225
+ * { name: "Alice", age: 30 },
1226
+ * { name: "Bob", age: 25 },
1227
+ * { name: "Charlie", age: 30 }
1228
+ * ]
1229
+ *
1230
+ * const result = Array.sortBy(
1231
+ * Order.mapInput(Order.number, (user: (typeof users)[number]) => user.age),
1232
+ * Order.mapInput(Order.string, (user: (typeof users)[number]) => user.name)
1233
+ * )(users)
1234
+ *
1235
+ * assert.deepStrictEqual(result, [
1236
+ * { name: "Bob", age: 25 },
1237
+ * { name: "Alice", age: 30 },
1238
+ * { name: "Charlie", age: 30 }
1239
+ * ])
1240
+ *
1241
+ * // Explanation:
1242
+ * // The array of users is sorted first by age in ascending order. When ages are equal,
1243
+ * // the users are further sorted by name in ascending order.
924
1244
  *
925
1245
  * @category sorting
926
1246
  * @since 2.0.0
@@ -945,6 +1265,14 @@ export const sortBy = <S extends Iterable<any> | NonEmptyReadonlyArray<any>>(
945
1265
  * If one input `Iterable` is short, excess elements of the
946
1266
  * longer `Iterable` are discarded.
947
1267
  *
1268
+ * @example
1269
+ * import { Array } from "effect"
1270
+ *
1271
+ * const array1 = [1, 2, 3]
1272
+ * const array2 = ['a', 'b']
1273
+ * const result = Array.zip(array1, array2)
1274
+ * assert.deepStrictEqual(result, [[1, 'a'], [2, 'b']])
1275
+ *
948
1276
  * @category zipping
949
1277
  * @since 2.0.0
950
1278
  */
@@ -962,6 +1290,14 @@ export const zip: {
962
1290
  * Apply a function to pairs of elements at the same index in two `Iterable`s, collecting the results in a new `Array`. If one
963
1291
  * input `Iterable` is short, excess elements of the longer `Iterable` are discarded.
964
1292
  *
1293
+ * @example
1294
+ * import { Array } from "effect"
1295
+ *
1296
+ * const array1 = [1, 2, 3]
1297
+ * const array2 = [4, 5, 6]
1298
+ * const result = Array.zipWith(array1, array2, (a, b) => a + b)
1299
+ * assert.deepStrictEqual(result, [5, 7, 9])
1300
+ *
965
1301
  * @category zipping
966
1302
  * @since 2.0.0
967
1303
  */
@@ -987,6 +1323,12 @@ export const zipWith: {
987
1323
  /**
988
1324
  * This function is the inverse of `zip`. Takes an `Iterable` of pairs and return two corresponding `Array`s.
989
1325
  *
1326
+ * @example
1327
+ * import { Array } from "effect"
1328
+ *
1329
+ * const result = Array.unzip([[1, "a"], [2, "b"], [3, "c"]])
1330
+ * assert.deepStrictEqual(result, [[1, 2, 3], ['a', 'b', 'c']])
1331
+ *
990
1332
  * @since 2.0.0
991
1333
  */
992
1334
  export const unzip: <S extends Iterable<readonly [any, any]> | NonEmptyReadonlyArray<readonly [any, any]>>(
@@ -1011,6 +1353,13 @@ export const unzip: <S extends Iterable<readonly [any, any]> | NonEmptyReadonlyA
1011
1353
  * Places an element in between members of an `Iterable`.
1012
1354
  * If the input is a non-empty array, the result is also a non-empty array.
1013
1355
  *
1356
+ * @example
1357
+ * import { Array } from "effect"
1358
+ *
1359
+ * const numbers = [1, 2, 3]
1360
+ * const result = Array.intersperse(numbers, 0)
1361
+ * assert.deepStrictEqual(result, [1, 0, 2, 0, 3])
1362
+ *
1014
1363
  * @since 2.0.0
1015
1364
  */
1016
1365
  export const intersperse: {
@@ -1038,6 +1387,12 @@ export const intersperse: {
1038
1387
  /**
1039
1388
  * Apply a function to the head, creating a new `NonEmptyReadonlyArray`.
1040
1389
  *
1390
+ * @example
1391
+ * import { Array } from "effect"
1392
+ *
1393
+ * const result = Array.modifyNonEmptyHead([1, 2, 3], n => n * 10)
1394
+ * assert.deepStrictEqual(result, [10, 2, 3])
1395
+ *
1041
1396
  * @since 2.0.0
1042
1397
  */
1043
1398
  export const modifyNonEmptyHead: {
@@ -1054,6 +1409,12 @@ export const modifyNonEmptyHead: {
1054
1409
  /**
1055
1410
  * Change the head, creating a new `NonEmptyReadonlyArray`.
1056
1411
  *
1412
+ * @example
1413
+ * import { Array } from "effect"
1414
+ *
1415
+ * const result = Array.setNonEmptyHead([1, 2, 3], 10)
1416
+ * assert.deepStrictEqual(result, [10, 2, 3])
1417
+ *
1057
1418
  * @since 2.0.0
1058
1419
  */
1059
1420
  export const setNonEmptyHead: {
@@ -1067,6 +1428,12 @@ export const setNonEmptyHead: {
1067
1428
  /**
1068
1429
  * Apply a function to the last element, creating a new `NonEmptyReadonlyArray`.
1069
1430
  *
1431
+ * @example
1432
+ * import { Array } from "effect"
1433
+ *
1434
+ * const result = Array.modifyNonEmptyLast([1, 2, 3], n => n * 2)
1435
+ * assert.deepStrictEqual(result, [1, 2, 6])
1436
+ *
1070
1437
  * @since 2.0.0
1071
1438
  */
1072
1439
  export const modifyNonEmptyLast: {
@@ -1081,6 +1448,12 @@ export const modifyNonEmptyLast: {
1081
1448
  /**
1082
1449
  * Change the last element, creating a new `NonEmptyReadonlyArray`.
1083
1450
  *
1451
+ * @example
1452
+ * import { Array } from "effect"
1453
+ *
1454
+ * const result = Array.setNonEmptyLast([1, 2, 3], 4)
1455
+ * assert.deepStrictEqual(result, [1, 2, 4])
1456
+ *
1084
1457
  * @since 2.0.0
1085
1458
  */
1086
1459
  export const setNonEmptyLast: {
@@ -1095,6 +1468,13 @@ export const setNonEmptyLast: {
1095
1468
  * Rotate an `Iterable` by `n` steps.
1096
1469
  * If the input is a non-empty array, the result is also a non-empty array.
1097
1470
  *
1471
+ * @example
1472
+ * import { Array } from "effect"
1473
+ *
1474
+ * const letters = ['a', 'b', 'c', 'd']
1475
+ * const result = Array.rotate(letters, 2)
1476
+ * assert.deepStrictEqual(result, ['c', 'd', 'a', 'b'])
1477
+ *
1098
1478
  * @since 2.0.0
1099
1479
  */
1100
1480
  export const rotate: {
@@ -1122,6 +1502,15 @@ export const rotate: {
1122
1502
  /**
1123
1503
  * Returns a function that checks if a `ReadonlyArray` contains a given value using a provided `isEquivalent` function.
1124
1504
  *
1505
+ * @example
1506
+ * import { Array } from "effect"
1507
+ *
1508
+ * const numbers = [1, 2, 3, 4]
1509
+ * const isEquivalent = (a: number, b: number) => a === b
1510
+ * const containsNumber = Array.containsWith(isEquivalent)
1511
+ * const result = containsNumber(3)(numbers)
1512
+ * assert.deepStrictEqual(result, true)
1513
+ *
1125
1514
  * @category elements
1126
1515
  * @since 2.0.0
1127
1516
  */
@@ -1143,6 +1532,13 @@ const _equivalence = Equal.equivalence()
1143
1532
  /**
1144
1533
  * Returns a function that checks if a `ReadonlyArray` contains a given value using the default `Equivalence`.
1145
1534
  *
1535
+ * @example
1536
+ * import { Array } from "effect"
1537
+ *
1538
+ * const letters = ['a', 'b', 'c', 'd']
1539
+ * const result = Array.contains('c')(letters)
1540
+ * assert.deepStrictEqual(result, true)
1541
+ *
1146
1542
  * @category elements
1147
1543
  * @since 2.0.0
1148
1544
  */
@@ -1156,6 +1552,18 @@ export const contains: {
1156
1552
  * `Iterable`. Typically chop is called with some function that will consume an initial prefix of the `Iterable` and produce a
1157
1553
  * value and the rest of the `Array`.
1158
1554
  *
1555
+ * @example
1556
+ * import { Array } from "effect"
1557
+ *
1558
+ * const numbers = [1, 2, 3, 4, 5]
1559
+ * const result = Array.chop(numbers, (as): [number, Array<number>] => [as[0] * 2, as.slice(1)])
1560
+ * assert.deepStrictEqual(result, [2, 4, 6, 8, 10])
1561
+ *
1562
+ * // Explanation:
1563
+ * // The `chopFunction` takes the first element of the array, doubles it, and then returns it along with the rest of the array.
1564
+ * // The `chop` function applies this `chopFunction` recursively to the input array `[1, 2, 3, 4, 5]`,
1565
+ * // resulting in a new array `[2, 4, 6, 8, 10]`.
1566
+ *
1159
1567
  * @since 2.0.0
1160
1568
  */
1161
1569
  export const chop: {
@@ -1193,6 +1601,13 @@ export const chop: {
1193
1601
  * Splits an `Iterable` into two segments, with the first segment containing a maximum of `n` elements.
1194
1602
  * The value of `n` can be `0`.
1195
1603
  *
1604
+ * @example
1605
+ * import { Array } from "effect"
1606
+ *
1607
+ * const numbers = [1, 2, 3, 4, 5]
1608
+ * const result = Array.splitAt(numbers, 3)
1609
+ * assert.deepStrictEqual(result, [[1, 2, 3], [4, 5]])
1610
+ *
1196
1611
  * @category splitting
1197
1612
  * @since 2.0.0
1198
1613
  */
@@ -1215,6 +1630,12 @@ export const splitAt: {
1215
1630
  * Splits a `NonEmptyReadonlyArray` into two segments, with the first segment containing a maximum of `n` elements.
1216
1631
  * The value of `n` must be `>= 1`.
1217
1632
  *
1633
+ * @example
1634
+ * import { Array } from "effect"
1635
+ *
1636
+ * const result = Array.splitNonEmptyAt(["a", "b", "c", "d", "e"], 3)
1637
+ * assert.deepStrictEqual(result, [["a", "b", "c"], ["d", "e"]])
1638
+ *
1218
1639
  * @category splitting
1219
1640
  * @since 2.0.0
1220
1641
  */
@@ -1231,6 +1652,13 @@ export const splitNonEmptyAt: {
1231
1652
  /**
1232
1653
  * Splits this iterable into `n` equally sized arrays.
1233
1654
  *
1655
+ * @example
1656
+ * import { Array } from "effect"
1657
+ *
1658
+ * const numbers = [1, 2, 3, 4, 5, 6, 7, 8]
1659
+ * const result = Array.split(numbers, 3)
1660
+ * assert.deepStrictEqual(result, [[1, 2, 3], [4, 5, 6], [7, 8]])
1661
+ *
1234
1662
  * @since 2.0.0
1235
1663
  * @category splitting
1236
1664
  */
@@ -1246,6 +1674,13 @@ export const split: {
1246
1674
  * Splits this iterable on the first element that matches this predicate.
1247
1675
  * Returns a tuple containing two arrays: the first one is before the match, and the second one is from the match onward.
1248
1676
  *
1677
+ * @example
1678
+ * import { Array } from "effect"
1679
+ *
1680
+ * const numbers = [1, 2, 3, 4, 5]
1681
+ * const result = Array.splitWhere(numbers, n => n > 3)
1682
+ * assert.deepStrictEqual(result, [[1, 2, 3], [4, 5]])
1683
+ *
1249
1684
  * @category splitting
1250
1685
  * @since 2.0.0
1251
1686
  */
@@ -1261,6 +1696,15 @@ export const splitWhere: {
1261
1696
  )
1262
1697
 
1263
1698
  /**
1699
+ * Copies an array.
1700
+ *
1701
+ * @example
1702
+ * import { Array } from "effect"
1703
+ *
1704
+ * const numbers = [1, 2, 3]
1705
+ * const copy = Array.copy(numbers)
1706
+ * assert.deepStrictEqual(copy, [1, 2, 3])
1707
+ *
1264
1708
  * @since 2.0.0
1265
1709
  */
1266
1710
  export const copy: {
@@ -1279,6 +1723,19 @@ export const copy: {
1279
1723
  *
1280
1724
  * whenever `n` evenly divides the length of `self`.
1281
1725
  *
1726
+ * @example
1727
+ * import { Array } from "effect"
1728
+ *
1729
+ * const numbers = [1, 2, 3, 4, 5]
1730
+ * const result = Array.chunksOf(numbers, 2)
1731
+ * assert.deepStrictEqual(result, [[1, 2], [3, 4], [5]])
1732
+ *
1733
+ * // Explanation:
1734
+ * // The `chunksOf` function takes an array of numbers `[1, 2, 3, 4, 5]` and a number `2`.
1735
+ * // It splits the array into chunks of length 2. Since the array length is not evenly divisible by 2,
1736
+ * // the last chunk contains the remaining elements.
1737
+ * // The result is `[[1, 2], [3, 4], [5]]`.
1738
+ *
1282
1739
  * @category splitting
1283
1740
  * @since 2.0.0
1284
1741
  */
@@ -1301,6 +1758,12 @@ export const chunksOf: {
1301
1758
  /**
1302
1759
  * Group equal, consecutive elements of a `NonEmptyReadonlyArray` into `NonEmptyArray`s using the provided `isEquivalent` function.
1303
1760
  *
1761
+ * @example
1762
+ * import { Array } from "effect"
1763
+ *
1764
+ * const result = Array.groupWith(["a", "a", "b", "b", "b", "c", "a"], (x, y) => x === y)
1765
+ * assert.deepStrictEqual(result, [["a", "a"], ["b", "b", "b"], ["c"], ["a"]])
1766
+ *
1304
1767
  * @category grouping
1305
1768
  * @since 2.0.0
1306
1769
  */
@@ -1329,6 +1792,12 @@ export const groupWith: {
1329
1792
  /**
1330
1793
  * Group equal, consecutive elements of a `NonEmptyReadonlyArray` into `NonEmptyArray`s.
1331
1794
  *
1795
+ * @example
1796
+ * import { Array } from "effect"
1797
+ *
1798
+ * const result = Array.group([1, 1, 2, 2, 2, 3, 1])
1799
+ * assert.deepStrictEqual(result, [[1, 1], [2, 2, 2], [3], [1]])
1800
+ *
1332
1801
  * @category grouping
1333
1802
  * @since 2.0.0
1334
1803
  */
@@ -1340,6 +1809,20 @@ export const group: <A>(self: NonEmptyReadonlyArray<A>) => NonEmptyArray<NonEmpt
1340
1809
  * Splits an `Iterable` into sub-non-empty-arrays stored in an object, based on the result of calling a `string`-returning
1341
1810
  * function on each element, and grouping the results according to values returned
1342
1811
  *
1812
+ * @example
1813
+ * import { Array } from "effect"
1814
+ *
1815
+ * const people = [
1816
+ * { name: "Alice", group: "A" },
1817
+ * { name: "Bob", group: "B" },
1818
+ * { name: "Charlie", group: "A" }
1819
+ * ]
1820
+ * const result = Array.groupBy(people, person => person.group)
1821
+ * assert.deepStrictEqual(result, {
1822
+ * A: [{ name: "Alice", group: "A" }, { name: "Charlie", group: "A" }],
1823
+ * B: [{ name: "Bob", group: "B" }]
1824
+ * })
1825
+ *
1343
1826
  * @category grouping
1344
1827
  * @since 2.0.0
1345
1828
  */
@@ -1368,6 +1851,16 @@ export const groupBy: {
1368
1851
  })
1369
1852
 
1370
1853
  /**
1854
+ * Calculates the union of two arrays using the provided equivalence relation.
1855
+ *
1856
+ * @example
1857
+ * import { Array } from "effect"
1858
+ *
1859
+ * const array1 = [1, 2]
1860
+ * const array2 = [2, 3]
1861
+ * const union = Array.unionWith(array1, array2, (a, b) => a === b)
1862
+ * assert.deepStrictEqual(union, [1, 2, 3])
1863
+ *
1371
1864
  * @since 2.0.0
1372
1865
  */
1373
1866
  export const unionWith: {
@@ -1400,6 +1893,16 @@ export const unionWith: {
1400
1893
  })
1401
1894
 
1402
1895
  /**
1896
+ * Creates a union of two arrays, removing duplicates.
1897
+ *
1898
+ * @example
1899
+ * import { Array } from "effect"
1900
+ *
1901
+ * const array1 = [1, 2]
1902
+ * const array2 = [2, 3]
1903
+ * const result = Array.union(array1, array2)
1904
+ * assert.deepStrictEqual(result, [1, 2, 3])
1905
+ *
1403
1906
  * @since 2.0.0
1404
1907
  */
1405
1908
  export const union: {
@@ -1417,6 +1920,15 @@ export const union: {
1417
1920
  * Creates an `Array` of unique values that are included in all given `Iterable`s using the provided `isEquivalent` function.
1418
1921
  * The order and references of result values are determined by the first `Iterable`.
1419
1922
  *
1923
+ * @example
1924
+ * import { Array } from "effect"
1925
+ *
1926
+ * const array1 = [{ id: 1 }, { id: 2 }, { id: 3 }]
1927
+ * const array2 = [{ id: 3 }, { id: 4 }, { id: 1 }]
1928
+ * const isEquivalent = (a: { id: number }, b: { id: number }) => a.id === b.id
1929
+ * const result = Array.intersectionWith(isEquivalent)(array2)(array1)
1930
+ * assert.deepStrictEqual(result, [{ id: 1 }, { id: 3 }])
1931
+ *
1420
1932
  * @since 2.0.0
1421
1933
  */
1422
1934
  export const intersectionWith = <A>(isEquivalent: (self: A, that: A) => boolean): {
@@ -1434,6 +1946,14 @@ export const intersectionWith = <A>(isEquivalent: (self: A, that: A) => boolean)
1434
1946
  * Creates an `Array` of unique values that are included in all given `Iterable`s.
1435
1947
  * The order and references of result values are determined by the first `Iterable`.
1436
1948
  *
1949
+ * @example
1950
+ * import { Array } from "effect"
1951
+ *
1952
+ * const array1 = [1, 2, 3]
1953
+ * const array2 = [3, 4, 1]
1954
+ * const result = Array.intersection(array1, array2)
1955
+ * assert.deepStrictEqual(result, [1, 3])
1956
+ *
1437
1957
  * @since 2.0.0
1438
1958
  */
1439
1959
  export const intersection: {
@@ -1445,6 +1965,14 @@ export const intersection: {
1445
1965
  * Creates a `Array` of values not included in the other given `Iterable` using the provided `isEquivalent` function.
1446
1966
  * The order and references of result values are determined by the first `Iterable`.
1447
1967
  *
1968
+ * @example
1969
+ * import { Array } from "effect"
1970
+ *
1971
+ * const array1 = [1, 2, 3]
1972
+ * const array2 = [2, 3, 4]
1973
+ * const difference = Array.differenceWith<number>((a, b) => a === b)(array1, array2)
1974
+ * assert.deepStrictEqual(difference, [1])
1975
+ *
1448
1976
  * @since 2.0.0
1449
1977
  */
1450
1978
  export const differenceWith = <A>(isEquivalent: (self: A, that: A) => boolean): {
@@ -1462,6 +1990,14 @@ export const differenceWith = <A>(isEquivalent: (self: A, that: A) => boolean):
1462
1990
  * Creates a `Array` of values not included in the other given `Iterable`.
1463
1991
  * The order and references of result values are determined by the first `Iterable`.
1464
1992
  *
1993
+ * @example
1994
+ * import { Array } from "effect"
1995
+ *
1996
+ * const array1 = [1, 2, 3]
1997
+ * const array2 = [2, 3, 4]
1998
+ * const difference = Array.difference(array1, array2)
1999
+ * assert.deepStrictEqual(difference, [1])
2000
+ *
1465
2001
  * @since 2.0.0
1466
2002
  */
1467
2003
  export const difference: {
@@ -1573,7 +2109,17 @@ export const flatMap: {
1573
2109
  )
1574
2110
 
1575
2111
  /**
1576
- * Flattens an array of arrays into a single array by concatenating all arrays.
2112
+ * Combines multiple arrays into a single array by concatenating all elements
2113
+ * from each nested array. This function ensures that the structure of nested
2114
+ * arrays is collapsed into a single, flat array.
2115
+ *
2116
+ * @example
2117
+ * import { Array } from "effect";
2118
+ *
2119
+ * const nestedArrays = [[1, 2], [], [3, 4], [], [5, 6]]
2120
+ * const result = Array.flatten(nestedArrays)
2121
+ *
2122
+ * assert.deepStrictEqual(result, [1, 2, 3, 4, 5, 6]);
1577
2123
  *
1578
2124
  * @category sequencing
1579
2125
  * @since 2.0.0
@@ -1583,6 +2129,18 @@ export const flatten: <S extends ReadonlyArray<ReadonlyArray<any>>>(self: S) =>
1583
2129
  ) as any
1584
2130
 
1585
2131
  /**
2132
+ * Applies a function to each element of the `Iterable` and filters based on the result, keeping the transformed values where the function returns `Some`.
2133
+ * This method combines filtering and mapping functionalities, allowing transformations and filtering of elements based on a single function pass.
2134
+ *
2135
+ * @example
2136
+ * import { Array, Option } from "effect";
2137
+ *
2138
+ * const data = [1, 2, 3, 4, 5];
2139
+ * const evenSquares = (x: number) => x % 2 === 0 ? Option.some(x * x) : Option.none();
2140
+ * const result = Array.filterMap(data, evenSquares);
2141
+ *
2142
+ * assert.deepStrictEqual(result, [4, 16]);
2143
+ *
1586
2144
  * @category filtering
1587
2145
  * @since 2.0.0
1588
2146
  */
@@ -1605,7 +2163,18 @@ export const filterMap: {
1605
2163
  )
1606
2164
 
1607
2165
  /**
1608
- * Transforms all elements of the `readonlyArray` for as long as the specified function returns some value
2166
+ * Applies a function to each element of the array and filters based on the result, stopping when a condition is not met.
2167
+ * This method combines filtering and mapping in a single pass, and short-circuits, i.e., stops processing, as soon as the function returns `None`.
2168
+ * This is useful when you need to transform an array but only up to the point where a certain condition holds true.
2169
+ *
2170
+ * @example
2171
+ * import { Array, Option } from "effect";
2172
+ *
2173
+ * const data = [2, 4, 5];
2174
+ * const toSquareTillOdd = (x: number) => x % 2 === 0 ? Option.some(x * x) : Option.none();
2175
+ * const result = Array.filterMapWhile(data, toSquareTillOdd);
2176
+ *
2177
+ * assert.deepStrictEqual(result, [4, 16]);
1609
2178
  *
1610
2179
  * @category filtering
1611
2180
  * @since 2.0.0
@@ -1629,6 +2198,25 @@ export const filterMapWhile: {
1629
2198
  })
1630
2199
 
1631
2200
  /**
2201
+ * Applies a function to each element of the `Iterable`, categorizing the results into two separate arrays.
2202
+ * This function is particularly useful for operations where each element can result in two possible types,
2203
+ * and you want to separate these types into different collections. For instance, separating validation results
2204
+ * into successes and failures.
2205
+ *
2206
+ * @example
2207
+ * import { Array, Either } from "effect";
2208
+ *
2209
+ * const data = [1, 2, 3, 4, 5]
2210
+ * const isEven = (x: number) => x % 2 === 0
2211
+ * const partitioned = Array.partitionMap(data, x =>
2212
+ * isEven(x) ? Either.right(x) : Either.left(x)
2213
+ * )
2214
+ *
2215
+ * assert.deepStrictEqual(partitioned, [
2216
+ * [1, 3, 5],
2217
+ * [2, 4]
2218
+ * ])
2219
+ *
1632
2220
  * @category filtering
1633
2221
  * @since 2.0.0
1634
2222
  */
@@ -1778,6 +2366,15 @@ export const partition: {
1778
2366
  )
1779
2367
 
1780
2368
  /**
2369
+ * Separates an `Iterable` into two arrays based on a predicate.
2370
+ *
2371
+ * @example
2372
+ * import { Array } from "effect"
2373
+ *
2374
+ * const numbers = [1, 2, 3, 4]
2375
+ * const result = Array.partition(numbers, n => n % 2 === 0)
2376
+ * assert.deepStrictEqual(result, [[1, 3], [2, 4]])
2377
+ *
1781
2378
  * @category filtering
1782
2379
  * @since 2.0.0
1783
2380
  */
@@ -1786,6 +2383,15 @@ export const separate: <R, L>(self: Iterable<Either<R, L>>) => [Array<L>, Array<
1786
2383
  )
1787
2384
 
1788
2385
  /**
2386
+ * Reduces an array from the left.
2387
+ *
2388
+ * @example
2389
+ * import { Array } from "effect"
2390
+ *
2391
+ * const numbers = [1, 2, 3]
2392
+ * const result = Array.reduce(numbers, 0, (acc, n) => acc + n)
2393
+ * assert.deepStrictEqual(result, 6)
2394
+ *
1789
2395
  * @category folding
1790
2396
  * @since 2.0.0
1791
2397
  */
@@ -1799,6 +2405,15 @@ export const reduce: {
1799
2405
  )
1800
2406
 
1801
2407
  /**
2408
+ * Reduces an array from the right.
2409
+ *
2410
+ * @example
2411
+ * import { Array } from "effect"
2412
+ *
2413
+ * const numbers = [1, 2, 3]
2414
+ * const result = Array.reduceRight(numbers, 0, (acc, n) => acc + n)
2415
+ * assert.deepStrictEqual(result, 6)
2416
+ *
1802
2417
  * @category folding
1803
2418
  * @since 2.0.0
1804
2419
  */
@@ -1812,6 +2427,16 @@ export const reduceRight: {
1812
2427
  )
1813
2428
 
1814
2429
  /**
2430
+ * Lifts a predicate into an array.
2431
+ *
2432
+ * @example
2433
+ * import { Array } from "effect"
2434
+ *
2435
+ * const isEven = (n: number) => n % 2 === 0
2436
+ * const to = Array.liftPredicate(isEven)
2437
+ * assert.deepStrictEqual(to(1), [])
2438
+ * assert.deepStrictEqual(to(2), [2])
2439
+ *
1815
2440
  * @category lifting
1816
2441
  * @since 2.0.0
1817
2442
  */
@@ -1845,6 +2470,20 @@ export const liftNullable = <A extends Array<unknown>, B>(
1845
2470
  (...a) => fromNullable(f(...a))
1846
2471
 
1847
2472
  /**
2473
+ * Maps over an array and flattens the result, removing null and undefined values.
2474
+ *
2475
+ * @example
2476
+ * import { Array } from "effect"
2477
+ *
2478
+ * const numbers = [1, 2, 3]
2479
+ * const result = Array.flatMapNullable(numbers, n => (n % 2 === 0 ? null : n))
2480
+ * assert.deepStrictEqual(result, [1, 3])
2481
+ *
2482
+ * // Explanation:
2483
+ * // The array of numbers [1, 2, 3] is mapped with a function that returns null for even numbers
2484
+ * // and the number itself for odd numbers. The resulting array [1, null, 3] is then flattened
2485
+ * // to remove null values, resulting in [1, 3].
2486
+ *
1848
2487
  * @category sequencing
1849
2488
  * @since 2.0.0
1850
2489
  */
@@ -1854,10 +2493,33 @@ export const flatMapNullable: {
1854
2493
  } = dual(
1855
2494
  2,
1856
2495
  <A, B>(self: ReadonlyArray<A>, f: (a: A) => B | null | undefined): Array<NonNullable<B>> =>
1857
- isNonEmptyReadonlyArray(self) ? fromNullable(f(headNonEmpty(self))) : empty()
2496
+ flatMap(self, (a) => fromNullable(f(a)))
1858
2497
  )
1859
2498
 
1860
2499
  /**
2500
+ * Lifts a function that returns an `Either` into a function that returns an array.
2501
+ * If the `Either` is a left, it returns an empty array.
2502
+ * If the `Either` is a right, it returns an array with the right value.
2503
+ *
2504
+ * @example
2505
+ * import { Array, Either } from "effect"
2506
+ *
2507
+ * const parseNumber = (s: string): Either.Either<number, Error> =>
2508
+ * isNaN(Number(s)) ? Either.left(new Error("Not a number")) : Either.right(Number(s))
2509
+ *
2510
+ * const liftedParseNumber = Array.liftEither(parseNumber)
2511
+ *
2512
+ * const result1 = liftedParseNumber("42")
2513
+ * assert.deepStrictEqual(result1, [42])
2514
+ *
2515
+ * const result2 = liftedParseNumber("not a number")
2516
+ * assert.deepStrictEqual(result2, [])
2517
+ *
2518
+ * // Explanation:
2519
+ * // The function parseNumber is lifted to return an array.
2520
+ * // When parsing "42", it returns an Either.left with the number 42, resulting in [42].
2521
+ * // When parsing "not a number", it returns an Either.right with an error, resulting in an empty array [].
2522
+ *
1861
2523
  * @category lifting
1862
2524
  * @since 2.0.0
1863
2525
  */
@@ -1906,6 +2568,21 @@ export const some: {
1906
2568
  )
1907
2569
 
1908
2570
  /**
2571
+ * Extends an array with a function that maps each subarray to a value.
2572
+ *
2573
+ * @example
2574
+ * import { Array } from "effect"
2575
+ *
2576
+ * const numbers = [1, 2, 3]
2577
+ * const result = Array.extend(numbers, as => as.length)
2578
+ * assert.deepStrictEqual(result, [3, 2, 1])
2579
+ *
2580
+ * // Explanation:
2581
+ * // The function maps each subarray starting from each element to its length.
2582
+ * // The subarrays are: [1, 2, 3], [2, 3], [3].
2583
+ * // The lengths are: 3, 2, 1.
2584
+ * // Therefore, the result is [3, 2, 1].
2585
+ *
1909
2586
  * @since 2.0.0
1910
2587
  */
1911
2588
  export const extend: {
@@ -1917,6 +2594,14 @@ export const extend: {
1917
2594
  )
1918
2595
 
1919
2596
  /**
2597
+ * Finds the minimum element in an array based on a comparator.
2598
+ *
2599
+ * @example
2600
+ * import { Array, Order } from "effect"
2601
+ *
2602
+ * const min = Array.min([3, 1, 2], Order.number)
2603
+ * assert.deepStrictEqual(min, 1)
2604
+ *
1920
2605
  * @since 2.0.0
1921
2606
  */
1922
2607
  export const min: {
@@ -1925,6 +2610,14 @@ export const min: {
1925
2610
  } = dual(2, <A>(self: NonEmptyReadonlyArray<A>, O: Order.Order<A>): A => self.reduce(Order.min(O)))
1926
2611
 
1927
2612
  /**
2613
+ * Finds the maximum element in an array based on a comparator.
2614
+ *
2615
+ * @example
2616
+ * import { Array, Order } from "effect"
2617
+ *
2618
+ * const max = Array.max([3, 1, 2], Order.number)
2619
+ * assert.deepStrictEqual(max, 3)
2620
+ *
1928
2621
  * @since 2.0.0
1929
2622
  */
1930
2623
  export const max: {
@@ -1960,6 +2653,16 @@ export const unfold = <B, A>(b: B, f: (b: B) => Option<readonly [A, B]>): Array<
1960
2653
  export const getOrder: <A>(O: Order.Order<A>) => Order.Order<ReadonlyArray<A>> = Order.array
1961
2654
 
1962
2655
  /**
2656
+ * Creates an equivalence relation for arrays.
2657
+ *
2658
+ * @example
2659
+ * import { Array } from "effect"
2660
+ *
2661
+ * const numbers1 = [1, 2, 3]
2662
+ * const numbers2 = [1, 2, 3]
2663
+ * const eq = Array.getEquivalence<number>((a, b) => a === b)
2664
+ * assert.deepStrictEqual(eq(numbers1, numbers2), true)
2665
+ *
1963
2666
  * @category instances
1964
2667
  * @since 2.0.0
1965
2668
  */
@@ -1968,7 +2671,13 @@ export const getEquivalence: <A>(
1968
2671
  ) => Equivalence.Equivalence<ReadonlyArray<A>> = Equivalence.array
1969
2672
 
1970
2673
  /**
1971
- * Iterate over the `Iterable` applying `f`.
2674
+ * Performs a side-effect for each element of the `Iterable`.
2675
+ *
2676
+ * @example
2677
+ * import { Array } from "effect"
2678
+ *
2679
+ * const numbers = [1, 2, 3]
2680
+ * Array.forEach(numbers, n => console.log(n)) // 1, 2, 3
1972
2681
  *
1973
2682
  * @since 2.0.0
1974
2683
  */
@@ -1981,6 +2690,13 @@ export const forEach: {
1981
2690
  * Remove duplicates from an `Iterable` using the provided `isEquivalent` function,
1982
2691
  * preserving the order of the first occurrence of each element.
1983
2692
  *
2693
+ * @example
2694
+ * import { Array } from "effect"
2695
+ *
2696
+ * const numbers = [1, 2, 2, 3, 3, 3]
2697
+ * const unique = Array.dedupeWith(numbers, (a, b) => a === b)
2698
+ * assert.deepStrictEqual(unique, [1, 2, 3])
2699
+ *
1984
2700
  * @since 2.0.0
1985
2701
  */
1986
2702
  export const dedupeWith: {
@@ -2021,6 +2737,13 @@ export const dedupe = <S extends Iterable<any> | NonEmptyReadonlyArray<any>>(
2021
2737
  /**
2022
2738
  * Deduplicates adjacent elements that are identical using the provided `isEquivalent` function.
2023
2739
  *
2740
+ * @example
2741
+ * import { Array } from "effect"
2742
+ *
2743
+ * const numbers = [1, 1, 2, 2, 3, 3]
2744
+ * const unique = Array.dedupeAdjacentWith(numbers, (a, b) => a === b)
2745
+ * assert.deepStrictEqual(unique, [1, 2, 3])
2746
+ *
2024
2747
  * @since 2.0.0
2025
2748
  */
2026
2749
  export const dedupeAdjacentWith: {
@@ -2041,6 +2764,13 @@ export const dedupeAdjacentWith: {
2041
2764
  /**
2042
2765
  * Deduplicates adjacent elements that are identical.
2043
2766
  *
2767
+ * @example
2768
+ * import { Array } from "effect"
2769
+ *
2770
+ * const numbers = [1, 1, 2, 2, 3, 3]
2771
+ * const unique = Array.dedupeAdjacent(numbers)
2772
+ * assert.deepStrictEqual(unique, [1, 2, 3])
2773
+ *
2044
2774
  * @since 2.0.0
2045
2775
  */
2046
2776
  export const dedupeAdjacent: <A>(self: Iterable<A>) => Array<A> = dedupeAdjacentWith(Equal.equivalence())
@@ -2048,6 +2778,13 @@ export const dedupeAdjacent: <A>(self: Iterable<A>) => Array<A> = dedupeAdjacent
2048
2778
  /**
2049
2779
  * Joins the elements together with "sep" in the middle.
2050
2780
  *
2781
+ * @example
2782
+ * import { Array } from "effect"
2783
+ *
2784
+ * const strings = ["a", "b", "c"]
2785
+ * const joined = Array.join(strings, "-")
2786
+ * assert.deepStrictEqual(joined, "a-b-c")
2787
+ *
2051
2788
  * @since 2.0.0
2052
2789
  * @category folding
2053
2790
  */
@@ -2059,6 +2796,13 @@ export const join: {
2059
2796
  /**
2060
2797
  * Statefully maps over the chunk, producing new elements of type `B`.
2061
2798
  *
2799
+ * @example
2800
+ * import { Array } from "effect"
2801
+ *
2802
+ * const numbers = [1, 2, 3]
2803
+ * const result = Array.mapAccum(numbers, 0, (acc, n) => [acc + n, acc + n])
2804
+ * assert.deepStrictEqual(result, [6, [1, 3, 6]])
2805
+ *
2062
2806
  * @since 2.0.0
2063
2807
  * @category folding
2064
2808
  */
@@ -2087,6 +2831,14 @@ export const mapAccum: {
2087
2831
  /**
2088
2832
  * Zips this chunk crosswise with the specified chunk using the specified combiner.
2089
2833
  *
2834
+ * @example
2835
+ * import { Array } from "effect"
2836
+ *
2837
+ * const array1 = [1, 2]
2838
+ * const array2 = ["a", "b"]
2839
+ * const product = Array.cartesianWith(array1, array2, (a, b) => `${a}-${b}`)
2840
+ * assert.deepStrictEqual(product, ["1-a", "1-b", "2-a", "2-b"])
2841
+ *
2090
2842
  * @since 2.0.0
2091
2843
  * @category elements
2092
2844
  */
@@ -2102,6 +2854,14 @@ export const cartesianWith: {
2102
2854
  /**
2103
2855
  * Zips this chunk crosswise with the specified chunk.
2104
2856
  *
2857
+ * @example
2858
+ * import { Array } from "effect"
2859
+ *
2860
+ * const array1 = [1, 2]
2861
+ * const array2 = ["a", "b"]
2862
+ * const product = Array.cartesian(array1, array2)
2863
+ * assert.deepStrictEqual(product, [[1, "a"], [1, "b"], [2, "a"], [2, "b"]])
2864
+ *
2105
2865
  * @since 2.0.0
2106
2866
  * @category elements
2107
2867
  */