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/cjs/Array.js +777 -17
- package/dist/cjs/Array.js.map +1 -1
- package/dist/cjs/internal/pool.js +2 -2
- package/dist/cjs/internal/pool.js.map +1 -1
- package/dist/cjs/internal/stream.js +9 -3
- package/dist/cjs/internal/stream.js.map +1 -1
- package/dist/cjs/internal/version.js +1 -1
- package/dist/dts/Array.d.ts +793 -33
- package/dist/dts/Array.d.ts.map +1 -1
- package/dist/dts/internal/stream.d.ts.map +1 -1
- package/dist/esm/Array.js +777 -17
- package/dist/esm/Array.js.map +1 -1
- package/dist/esm/internal/pool.js +2 -2
- package/dist/esm/internal/pool.js.map +1 -1
- package/dist/esm/internal/stream.js +9 -3
- package/dist/esm/internal/stream.js.map +1 -1
- package/dist/esm/internal/version.js +1 -1
- package/package.json +1 -1
- package/src/Array.ts +806 -40
- package/src/internal/pool.ts +9 -6
- package/src/internal/stream.ts +7 -3
- package/src/internal/version.ts +1 -1
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 {
|
|
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 {
|
|
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,16 +317,16 @@ export const prepend: {
|
|
|
245
317
|
* @example
|
|
246
318
|
* import { Array } from "effect"
|
|
247
319
|
*
|
|
248
|
-
*
|
|
249
|
-
*
|
|
250
|
-
*
|
|
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
|
|
255
327
|
*/
|
|
256
328
|
export const prependAll: {
|
|
257
|
-
<S extends
|
|
329
|
+
<S extends Iterable<any>, T extends Iterable<any>>(
|
|
258
330
|
that: T
|
|
259
331
|
): (self: S) => ReadonlyArray.OrNonEmpty<S, T, ReadonlyArray.Infer<S> | ReadonlyArray.Infer<T>>
|
|
260
332
|
<A, B>(self: Iterable<A>, that: NonEmptyReadonlyArray<B>): NonEmptyArray<A | B>
|
|
@@ -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
|
*/
|
|
@@ -284,7 +363,7 @@ export const append: {
|
|
|
284
363
|
* @since 2.0.0
|
|
285
364
|
*/
|
|
286
365
|
export const appendAll: {
|
|
287
|
-
<S extends
|
|
366
|
+
<S extends Iterable<any>, T extends Iterable<any>>(
|
|
288
367
|
that: T
|
|
289
368
|
): (self: S) => ReadonlyArray.OrNonEmpty<S, T, ReadonlyArray.Infer<S> | ReadonlyArray.Infer<T>>
|
|
290
369
|
<A, B>(self: Iterable<A>, that: NonEmptyReadonlyArray<B>): NonEmptyArray<A | B>
|
|
@@ -296,7 +375,22 @@ export const appendAll: {
|
|
|
296
375
|
)
|
|
297
376
|
|
|
298
377
|
/**
|
|
299
|
-
*
|
|
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
|
-
*
|
|
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
|
-
*
|
|
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
|
-
*
|
|
923
|
-
* using first `orders
|
|
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,12 +1353,19 @@ 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: {
|
|
1017
1366
|
<B>(
|
|
1018
1367
|
middle: B
|
|
1019
|
-
): <S extends
|
|
1368
|
+
): <S extends Iterable<any>>(self: S) => ReadonlyArray.With<S, ReadonlyArray.Infer<S> | B>
|
|
1020
1369
|
<A, B>(self: NonEmptyReadonlyArray<A>, middle: B): NonEmptyArray<A | B>
|
|
1021
1370
|
<A, B>(self: Iterable<A>, middle: B): Array<A | B>
|
|
1022
1371
|
} = dual(2, <A, B>(self: Iterable<A>, middle: B): Array<A | B> => {
|
|
@@ -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,10 +1468,17 @@ 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: {
|
|
1101
|
-
(n: number): <S extends
|
|
1481
|
+
(n: number): <S extends Iterable<any>>(self: S) => ReadonlyArray.With<S, ReadonlyArray.Infer<S>>
|
|
1102
1482
|
<A>(self: NonEmptyReadonlyArray<A>, n: number): NonEmptyArray<A>
|
|
1103
1483
|
<A>(self: Iterable<A>, n: number): Array<A>
|
|
1104
1484
|
} = dual(2, <A>(self: Iterable<A>, n: number): Array<A> => {
|
|
@@ -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,10 +1552,22 @@ 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: {
|
|
1162
|
-
<S extends
|
|
1570
|
+
<S extends Iterable<any>, B>(
|
|
1163
1571
|
f: (as: NonEmptyReadonlyArray<ReadonlyArray.Infer<S>>) => readonly [B, ReadonlyArray<ReadonlyArray.Infer<S>>]
|
|
1164
1572
|
): (self: S) => ReadonlyArray.With<S, ReadonlyArray.Infer<S>>
|
|
1165
1573
|
<A, B>(
|
|
@@ -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,13 +1723,26 @@ 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
|
*/
|
|
1285
1742
|
export const chunksOf: {
|
|
1286
1743
|
(
|
|
1287
1744
|
n: number
|
|
1288
|
-
): <S extends
|
|
1745
|
+
): <S extends Iterable<any>>(
|
|
1289
1746
|
self: S
|
|
1290
1747
|
) => ReadonlyArray.With<S, NonEmptyArray<ReadonlyArray.Infer<S>>>
|
|
1291
1748
|
<A>(self: NonEmptyReadonlyArray<A>, n: number): NonEmptyArray<NonEmptyArray<A>>
|
|
@@ -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,10 +1851,20 @@ 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: {
|
|
1374
|
-
<S extends
|
|
1867
|
+
<S extends Iterable<any>, T extends Iterable<any>>(
|
|
1375
1868
|
that: T,
|
|
1376
1869
|
isEquivalent: (self: ReadonlyArray.Infer<S>, that: ReadonlyArray.Infer<T>) => boolean
|
|
1377
1870
|
): (self: S) => ReadonlyArray.OrNonEmpty<S, T, ReadonlyArray.Infer<S> | ReadonlyArray.Infer<T>>
|
|
@@ -1400,12 +1893,22 @@ 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: {
|
|
1406
|
-
<T extends
|
|
1909
|
+
<T extends Iterable<any>>(
|
|
1407
1910
|
that: T
|
|
1408
|
-
): <S extends
|
|
1911
|
+
): <S extends Iterable<any>>(
|
|
1409
1912
|
self: S
|
|
1410
1913
|
) => ReadonlyArray.OrNonEmpty<S, T, ReadonlyArray.Infer<S> | ReadonlyArray.Infer<T>>
|
|
1411
1914
|
<A, B>(self: NonEmptyReadonlyArray<A>, that: ReadonlyArray<B>): NonEmptyArray<A | B>
|
|
@@ -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: {
|
|
@@ -1490,23 +2026,22 @@ export declare namespace ReadonlyArray {
|
|
|
1490
2026
|
/**
|
|
1491
2027
|
* @since 2.0.0
|
|
1492
2028
|
*/
|
|
1493
|
-
export type Infer<S extends
|
|
2029
|
+
export type Infer<S extends Iterable<any>> = S extends ReadonlyArray<infer A> ? A
|
|
1494
2030
|
: S extends Iterable<infer A> ? A
|
|
1495
2031
|
: never
|
|
1496
2032
|
|
|
1497
2033
|
/**
|
|
1498
2034
|
* @since 2.0.0
|
|
1499
2035
|
*/
|
|
1500
|
-
export type With<S extends
|
|
1501
|
-
NonEmptyArray<A>
|
|
2036
|
+
export type With<S extends Iterable<any>, A> = S extends NonEmptyReadonlyArray<any> ? NonEmptyArray<A>
|
|
1502
2037
|
: Array<A>
|
|
1503
2038
|
|
|
1504
2039
|
/**
|
|
1505
2040
|
* @since 2.0.0
|
|
1506
2041
|
*/
|
|
1507
2042
|
export type OrNonEmpty<
|
|
1508
|
-
S extends
|
|
1509
|
-
T extends
|
|
2043
|
+
S extends Iterable<any>,
|
|
2044
|
+
T extends Iterable<any>,
|
|
1510
2045
|
A
|
|
1511
2046
|
> = S extends NonEmptyReadonlyArray<any> ? NonEmptyArray<A>
|
|
1512
2047
|
: T extends NonEmptyReadonlyArray<any> ? NonEmptyArray<A>
|
|
@@ -1516,8 +2051,8 @@ export declare namespace ReadonlyArray {
|
|
|
1516
2051
|
* @since 2.0.0
|
|
1517
2052
|
*/
|
|
1518
2053
|
export type AndNonEmpty<
|
|
1519
|
-
S extends
|
|
1520
|
-
T extends
|
|
2054
|
+
S extends Iterable<any>,
|
|
2055
|
+
T extends Iterable<any>,
|
|
1521
2056
|
A
|
|
1522
2057
|
> = S extends NonEmptyReadonlyArray<any> ? T extends NonEmptyReadonlyArray<any> ? NonEmptyArray<A>
|
|
1523
2058
|
: Array<A>
|
|
@@ -1573,7 +2108,17 @@ export const flatMap: {
|
|
|
1573
2108
|
)
|
|
1574
2109
|
|
|
1575
2110
|
/**
|
|
1576
|
-
*
|
|
2111
|
+
* Combines multiple arrays into a single array by concatenating all elements
|
|
2112
|
+
* from each nested array. This function ensures that the structure of nested
|
|
2113
|
+
* arrays is collapsed into a single, flat array.
|
|
2114
|
+
*
|
|
2115
|
+
* @example
|
|
2116
|
+
* import { Array } from "effect";
|
|
2117
|
+
*
|
|
2118
|
+
* const nestedArrays = [[1, 2], [], [3, 4], [], [5, 6]]
|
|
2119
|
+
* const result = Array.flatten(nestedArrays)
|
|
2120
|
+
*
|
|
2121
|
+
* assert.deepStrictEqual(result, [1, 2, 3, 4, 5, 6]);
|
|
1577
2122
|
*
|
|
1578
2123
|
* @category sequencing
|
|
1579
2124
|
* @since 2.0.0
|
|
@@ -1583,6 +2128,18 @@ export const flatten: <S extends ReadonlyArray<ReadonlyArray<any>>>(self: S) =>
|
|
|
1583
2128
|
) as any
|
|
1584
2129
|
|
|
1585
2130
|
/**
|
|
2131
|
+
* Applies a function to each element of the `Iterable` and filters based on the result, keeping the transformed values where the function returns `Some`.
|
|
2132
|
+
* This method combines filtering and mapping functionalities, allowing transformations and filtering of elements based on a single function pass.
|
|
2133
|
+
*
|
|
2134
|
+
* @example
|
|
2135
|
+
* import { Array, Option } from "effect";
|
|
2136
|
+
*
|
|
2137
|
+
* const data = [1, 2, 3, 4, 5];
|
|
2138
|
+
* const evenSquares = (x: number) => x % 2 === 0 ? Option.some(x * x) : Option.none();
|
|
2139
|
+
* const result = Array.filterMap(data, evenSquares);
|
|
2140
|
+
*
|
|
2141
|
+
* assert.deepStrictEqual(result, [4, 16]);
|
|
2142
|
+
*
|
|
1586
2143
|
* @category filtering
|
|
1587
2144
|
* @since 2.0.0
|
|
1588
2145
|
*/
|
|
@@ -1605,7 +2162,18 @@ export const filterMap: {
|
|
|
1605
2162
|
)
|
|
1606
2163
|
|
|
1607
2164
|
/**
|
|
1608
|
-
*
|
|
2165
|
+
* Applies a function to each element of the array and filters based on the result, stopping when a condition is not met.
|
|
2166
|
+
* This method combines filtering and mapping in a single pass, and short-circuits, i.e., stops processing, as soon as the function returns `None`.
|
|
2167
|
+
* This is useful when you need to transform an array but only up to the point where a certain condition holds true.
|
|
2168
|
+
*
|
|
2169
|
+
* @example
|
|
2170
|
+
* import { Array, Option } from "effect";
|
|
2171
|
+
*
|
|
2172
|
+
* const data = [2, 4, 5];
|
|
2173
|
+
* const toSquareTillOdd = (x: number) => x % 2 === 0 ? Option.some(x * x) : Option.none();
|
|
2174
|
+
* const result = Array.filterMapWhile(data, toSquareTillOdd);
|
|
2175
|
+
*
|
|
2176
|
+
* assert.deepStrictEqual(result, [4, 16]);
|
|
1609
2177
|
*
|
|
1610
2178
|
* @category filtering
|
|
1611
2179
|
* @since 2.0.0
|
|
@@ -1629,6 +2197,25 @@ export const filterMapWhile: {
|
|
|
1629
2197
|
})
|
|
1630
2198
|
|
|
1631
2199
|
/**
|
|
2200
|
+
* Applies a function to each element of the `Iterable`, categorizing the results into two separate arrays.
|
|
2201
|
+
* This function is particularly useful for operations where each element can result in two possible types,
|
|
2202
|
+
* and you want to separate these types into different collections. For instance, separating validation results
|
|
2203
|
+
* into successes and failures.
|
|
2204
|
+
*
|
|
2205
|
+
* @example
|
|
2206
|
+
* import { Array, Either } from "effect";
|
|
2207
|
+
*
|
|
2208
|
+
* const data = [1, 2, 3, 4, 5]
|
|
2209
|
+
* const isEven = (x: number) => x % 2 === 0
|
|
2210
|
+
* const partitioned = Array.partitionMap(data, x =>
|
|
2211
|
+
* isEven(x) ? Either.right(x) : Either.left(x)
|
|
2212
|
+
* )
|
|
2213
|
+
*
|
|
2214
|
+
* assert.deepStrictEqual(partitioned, [
|
|
2215
|
+
* [1, 3, 5],
|
|
2216
|
+
* [2, 4]
|
|
2217
|
+
* ])
|
|
2218
|
+
*
|
|
1632
2219
|
* @category filtering
|
|
1633
2220
|
* @since 2.0.0
|
|
1634
2221
|
*/
|
|
@@ -1667,7 +2254,10 @@ export const partitionMap: {
|
|
|
1667
2254
|
* @category filtering
|
|
1668
2255
|
* @since 2.0.0
|
|
1669
2256
|
*/
|
|
1670
|
-
|
|
2257
|
+
|
|
2258
|
+
export const getSomes: <T extends Iterable<Option<X>>, X = any>(
|
|
2259
|
+
self: T
|
|
2260
|
+
) => Array<Option.Value<ReadonlyArray.Infer<T>>> = filterMap(identity as any)
|
|
1671
2261
|
|
|
1672
2262
|
/**
|
|
1673
2263
|
* Retrieves the `Left` values from an `Iterable` of `Either`s, collecting them into an array.
|
|
@@ -1683,8 +2273,8 @@ export const getSomes: <A>(self: Iterable<Option<A>>) => Array<A> = filterMap(id
|
|
|
1683
2273
|
* @category filtering
|
|
1684
2274
|
* @since 2.0.0
|
|
1685
2275
|
*/
|
|
1686
|
-
export const getLefts = <
|
|
1687
|
-
const out: Array<
|
|
2276
|
+
export const getLefts = <T extends Iterable<Either<any, any>>>(self: T): Array<Either.Left<ReadonlyArray.Infer<T>>> => {
|
|
2277
|
+
const out: Array<any> = []
|
|
1688
2278
|
for (const a of self) {
|
|
1689
2279
|
if (E.isLeft(a)) {
|
|
1690
2280
|
out.push(a.left)
|
|
@@ -1708,8 +2298,10 @@ export const getLefts = <R, L>(self: Iterable<Either<R, L>>): Array<L> => {
|
|
|
1708
2298
|
* @category filtering
|
|
1709
2299
|
* @since 2.0.0
|
|
1710
2300
|
*/
|
|
1711
|
-
export const getRights = <
|
|
1712
|
-
|
|
2301
|
+
export const getRights = <T extends Iterable<Either<any, any>>>(
|
|
2302
|
+
self: T
|
|
2303
|
+
): Array<Either.Right<ReadonlyArray.Infer<T>>> => {
|
|
2304
|
+
const out: Array<any> = []
|
|
1713
2305
|
for (const a of self) {
|
|
1714
2306
|
if (E.isRight(a)) {
|
|
1715
2307
|
out.push(a.right)
|
|
@@ -1778,14 +2370,34 @@ export const partition: {
|
|
|
1778
2370
|
)
|
|
1779
2371
|
|
|
1780
2372
|
/**
|
|
2373
|
+
* Separates an `Iterable` into two arrays based on a predicate.
|
|
2374
|
+
*
|
|
2375
|
+
* @example
|
|
2376
|
+
* import { Array } from "effect"
|
|
2377
|
+
*
|
|
2378
|
+
* const numbers = [1, 2, 3, 4]
|
|
2379
|
+
* const result = Array.partition(numbers, n => n % 2 === 0)
|
|
2380
|
+
* assert.deepStrictEqual(result, [[1, 3], [2, 4]])
|
|
2381
|
+
*
|
|
1781
2382
|
* @category filtering
|
|
1782
2383
|
* @since 2.0.0
|
|
1783
2384
|
*/
|
|
1784
|
-
export const separate: <
|
|
2385
|
+
export const separate: <T extends Iterable<Either<any, any>>>(
|
|
2386
|
+
self: T
|
|
2387
|
+
) => [Array<Either.Left<ReadonlyArray.Infer<T>>>, Array<Either.Right<ReadonlyArray.Infer<T>>>] = partitionMap(
|
|
1785
2388
|
identity
|
|
1786
2389
|
)
|
|
1787
2390
|
|
|
1788
2391
|
/**
|
|
2392
|
+
* Reduces an array from the left.
|
|
2393
|
+
*
|
|
2394
|
+
* @example
|
|
2395
|
+
* import { Array } from "effect"
|
|
2396
|
+
*
|
|
2397
|
+
* const numbers = [1, 2, 3]
|
|
2398
|
+
* const result = Array.reduce(numbers, 0, (acc, n) => acc + n)
|
|
2399
|
+
* assert.deepStrictEqual(result, 6)
|
|
2400
|
+
*
|
|
1789
2401
|
* @category folding
|
|
1790
2402
|
* @since 2.0.0
|
|
1791
2403
|
*/
|
|
@@ -1799,6 +2411,15 @@ export const reduce: {
|
|
|
1799
2411
|
)
|
|
1800
2412
|
|
|
1801
2413
|
/**
|
|
2414
|
+
* Reduces an array from the right.
|
|
2415
|
+
*
|
|
2416
|
+
* @example
|
|
2417
|
+
* import { Array } from "effect"
|
|
2418
|
+
*
|
|
2419
|
+
* const numbers = [1, 2, 3]
|
|
2420
|
+
* const result = Array.reduceRight(numbers, 0, (acc, n) => acc + n)
|
|
2421
|
+
* assert.deepStrictEqual(result, 6)
|
|
2422
|
+
*
|
|
1802
2423
|
* @category folding
|
|
1803
2424
|
* @since 2.0.0
|
|
1804
2425
|
*/
|
|
@@ -1812,6 +2433,16 @@ export const reduceRight: {
|
|
|
1812
2433
|
)
|
|
1813
2434
|
|
|
1814
2435
|
/**
|
|
2436
|
+
* Lifts a predicate into an array.
|
|
2437
|
+
*
|
|
2438
|
+
* @example
|
|
2439
|
+
* import { Array } from "effect"
|
|
2440
|
+
*
|
|
2441
|
+
* const isEven = (n: number) => n % 2 === 0
|
|
2442
|
+
* const to = Array.liftPredicate(isEven)
|
|
2443
|
+
* assert.deepStrictEqual(to(1), [])
|
|
2444
|
+
* assert.deepStrictEqual(to(2), [2])
|
|
2445
|
+
*
|
|
1815
2446
|
* @category lifting
|
|
1816
2447
|
* @since 2.0.0
|
|
1817
2448
|
*/
|
|
@@ -1845,6 +2476,20 @@ export const liftNullable = <A extends Array<unknown>, B>(
|
|
|
1845
2476
|
(...a) => fromNullable(f(...a))
|
|
1846
2477
|
|
|
1847
2478
|
/**
|
|
2479
|
+
* Maps over an array and flattens the result, removing null and undefined values.
|
|
2480
|
+
*
|
|
2481
|
+
* @example
|
|
2482
|
+
* import { Array } from "effect"
|
|
2483
|
+
*
|
|
2484
|
+
* const numbers = [1, 2, 3]
|
|
2485
|
+
* const result = Array.flatMapNullable(numbers, n => (n % 2 === 0 ? null : n))
|
|
2486
|
+
* assert.deepStrictEqual(result, [1, 3])
|
|
2487
|
+
*
|
|
2488
|
+
* // Explanation:
|
|
2489
|
+
* // The array of numbers [1, 2, 3] is mapped with a function that returns null for even numbers
|
|
2490
|
+
* // and the number itself for odd numbers. The resulting array [1, null, 3] is then flattened
|
|
2491
|
+
* // to remove null values, resulting in [1, 3].
|
|
2492
|
+
*
|
|
1848
2493
|
* @category sequencing
|
|
1849
2494
|
* @since 2.0.0
|
|
1850
2495
|
*/
|
|
@@ -1854,10 +2499,33 @@ export const flatMapNullable: {
|
|
|
1854
2499
|
} = dual(
|
|
1855
2500
|
2,
|
|
1856
2501
|
<A, B>(self: ReadonlyArray<A>, f: (a: A) => B | null | undefined): Array<NonNullable<B>> =>
|
|
1857
|
-
|
|
2502
|
+
flatMap(self, (a) => fromNullable(f(a)))
|
|
1858
2503
|
)
|
|
1859
2504
|
|
|
1860
2505
|
/**
|
|
2506
|
+
* Lifts a function that returns an `Either` into a function that returns an array.
|
|
2507
|
+
* If the `Either` is a left, it returns an empty array.
|
|
2508
|
+
* If the `Either` is a right, it returns an array with the right value.
|
|
2509
|
+
*
|
|
2510
|
+
* @example
|
|
2511
|
+
* import { Array, Either } from "effect"
|
|
2512
|
+
*
|
|
2513
|
+
* const parseNumber = (s: string): Either.Either<number, Error> =>
|
|
2514
|
+
* isNaN(Number(s)) ? Either.left(new Error("Not a number")) : Either.right(Number(s))
|
|
2515
|
+
*
|
|
2516
|
+
* const liftedParseNumber = Array.liftEither(parseNumber)
|
|
2517
|
+
*
|
|
2518
|
+
* const result1 = liftedParseNumber("42")
|
|
2519
|
+
* assert.deepStrictEqual(result1, [42])
|
|
2520
|
+
*
|
|
2521
|
+
* const result2 = liftedParseNumber("not a number")
|
|
2522
|
+
* assert.deepStrictEqual(result2, [])
|
|
2523
|
+
*
|
|
2524
|
+
* // Explanation:
|
|
2525
|
+
* // The function parseNumber is lifted to return an array.
|
|
2526
|
+
* // When parsing "42", it returns an Either.left with the number 42, resulting in [42].
|
|
2527
|
+
* // When parsing "not a number", it returns an Either.right with an error, resulting in an empty array [].
|
|
2528
|
+
*
|
|
1861
2529
|
* @category lifting
|
|
1862
2530
|
* @since 2.0.0
|
|
1863
2531
|
*/
|
|
@@ -1906,6 +2574,21 @@ export const some: {
|
|
|
1906
2574
|
)
|
|
1907
2575
|
|
|
1908
2576
|
/**
|
|
2577
|
+
* Extends an array with a function that maps each subarray to a value.
|
|
2578
|
+
*
|
|
2579
|
+
* @example
|
|
2580
|
+
* import { Array } from "effect"
|
|
2581
|
+
*
|
|
2582
|
+
* const numbers = [1, 2, 3]
|
|
2583
|
+
* const result = Array.extend(numbers, as => as.length)
|
|
2584
|
+
* assert.deepStrictEqual(result, [3, 2, 1])
|
|
2585
|
+
*
|
|
2586
|
+
* // Explanation:
|
|
2587
|
+
* // The function maps each subarray starting from each element to its length.
|
|
2588
|
+
* // The subarrays are: [1, 2, 3], [2, 3], [3].
|
|
2589
|
+
* // The lengths are: 3, 2, 1.
|
|
2590
|
+
* // Therefore, the result is [3, 2, 1].
|
|
2591
|
+
*
|
|
1909
2592
|
* @since 2.0.0
|
|
1910
2593
|
*/
|
|
1911
2594
|
export const extend: {
|
|
@@ -1917,6 +2600,14 @@ export const extend: {
|
|
|
1917
2600
|
)
|
|
1918
2601
|
|
|
1919
2602
|
/**
|
|
2603
|
+
* Finds the minimum element in an array based on a comparator.
|
|
2604
|
+
*
|
|
2605
|
+
* @example
|
|
2606
|
+
* import { Array, Order } from "effect"
|
|
2607
|
+
*
|
|
2608
|
+
* const min = Array.min([3, 1, 2], Order.number)
|
|
2609
|
+
* assert.deepStrictEqual(min, 1)
|
|
2610
|
+
*
|
|
1920
2611
|
* @since 2.0.0
|
|
1921
2612
|
*/
|
|
1922
2613
|
export const min: {
|
|
@@ -1925,6 +2616,14 @@ export const min: {
|
|
|
1925
2616
|
} = dual(2, <A>(self: NonEmptyReadonlyArray<A>, O: Order.Order<A>): A => self.reduce(Order.min(O)))
|
|
1926
2617
|
|
|
1927
2618
|
/**
|
|
2619
|
+
* Finds the maximum element in an array based on a comparator.
|
|
2620
|
+
*
|
|
2621
|
+
* @example
|
|
2622
|
+
* import { Array, Order } from "effect"
|
|
2623
|
+
*
|
|
2624
|
+
* const max = Array.max([3, 1, 2], Order.number)
|
|
2625
|
+
* assert.deepStrictEqual(max, 3)
|
|
2626
|
+
*
|
|
1928
2627
|
* @since 2.0.0
|
|
1929
2628
|
*/
|
|
1930
2629
|
export const max: {
|
|
@@ -1960,6 +2659,16 @@ export const unfold = <B, A>(b: B, f: (b: B) => Option<readonly [A, B]>): Array<
|
|
|
1960
2659
|
export const getOrder: <A>(O: Order.Order<A>) => Order.Order<ReadonlyArray<A>> = Order.array
|
|
1961
2660
|
|
|
1962
2661
|
/**
|
|
2662
|
+
* Creates an equivalence relation for arrays.
|
|
2663
|
+
*
|
|
2664
|
+
* @example
|
|
2665
|
+
* import { Array } from "effect"
|
|
2666
|
+
*
|
|
2667
|
+
* const numbers1 = [1, 2, 3]
|
|
2668
|
+
* const numbers2 = [1, 2, 3]
|
|
2669
|
+
* const eq = Array.getEquivalence<number>((a, b) => a === b)
|
|
2670
|
+
* assert.deepStrictEqual(eq(numbers1, numbers2), true)
|
|
2671
|
+
*
|
|
1963
2672
|
* @category instances
|
|
1964
2673
|
* @since 2.0.0
|
|
1965
2674
|
*/
|
|
@@ -1968,7 +2677,13 @@ export const getEquivalence: <A>(
|
|
|
1968
2677
|
) => Equivalence.Equivalence<ReadonlyArray<A>> = Equivalence.array
|
|
1969
2678
|
|
|
1970
2679
|
/**
|
|
1971
|
-
*
|
|
2680
|
+
* Performs a side-effect for each element of the `Iterable`.
|
|
2681
|
+
*
|
|
2682
|
+
* @example
|
|
2683
|
+
* import { Array } from "effect"
|
|
2684
|
+
*
|
|
2685
|
+
* const numbers = [1, 2, 3]
|
|
2686
|
+
* Array.forEach(numbers, n => console.log(n)) // 1, 2, 3
|
|
1972
2687
|
*
|
|
1973
2688
|
* @since 2.0.0
|
|
1974
2689
|
*/
|
|
@@ -1981,10 +2696,17 @@ export const forEach: {
|
|
|
1981
2696
|
* Remove duplicates from an `Iterable` using the provided `isEquivalent` function,
|
|
1982
2697
|
* preserving the order of the first occurrence of each element.
|
|
1983
2698
|
*
|
|
2699
|
+
* @example
|
|
2700
|
+
* import { Array } from "effect"
|
|
2701
|
+
*
|
|
2702
|
+
* const numbers = [1, 2, 2, 3, 3, 3]
|
|
2703
|
+
* const unique = Array.dedupeWith(numbers, (a, b) => a === b)
|
|
2704
|
+
* assert.deepStrictEqual(unique, [1, 2, 3])
|
|
2705
|
+
*
|
|
1984
2706
|
* @since 2.0.0
|
|
1985
2707
|
*/
|
|
1986
2708
|
export const dedupeWith: {
|
|
1987
|
-
<S extends
|
|
2709
|
+
<S extends Iterable<any>>(
|
|
1988
2710
|
isEquivalent: (self: ReadonlyArray.Infer<S>, that: ReadonlyArray.Infer<S>) => boolean
|
|
1989
2711
|
): (self: S) => ReadonlyArray.With<S, ReadonlyArray.Infer<S>>
|
|
1990
2712
|
<A>(self: NonEmptyReadonlyArray<A>, isEquivalent: (self: A, that: A) => boolean): NonEmptyArray<A>
|
|
@@ -2021,6 +2743,13 @@ export const dedupe = <S extends Iterable<any> | NonEmptyReadonlyArray<any>>(
|
|
|
2021
2743
|
/**
|
|
2022
2744
|
* Deduplicates adjacent elements that are identical using the provided `isEquivalent` function.
|
|
2023
2745
|
*
|
|
2746
|
+
* @example
|
|
2747
|
+
* import { Array } from "effect"
|
|
2748
|
+
*
|
|
2749
|
+
* const numbers = [1, 1, 2, 2, 3, 3]
|
|
2750
|
+
* const unique = Array.dedupeAdjacentWith(numbers, (a, b) => a === b)
|
|
2751
|
+
* assert.deepStrictEqual(unique, [1, 2, 3])
|
|
2752
|
+
*
|
|
2024
2753
|
* @since 2.0.0
|
|
2025
2754
|
*/
|
|
2026
2755
|
export const dedupeAdjacentWith: {
|
|
@@ -2041,6 +2770,13 @@ export const dedupeAdjacentWith: {
|
|
|
2041
2770
|
/**
|
|
2042
2771
|
* Deduplicates adjacent elements that are identical.
|
|
2043
2772
|
*
|
|
2773
|
+
* @example
|
|
2774
|
+
* import { Array } from "effect"
|
|
2775
|
+
*
|
|
2776
|
+
* const numbers = [1, 1, 2, 2, 3, 3]
|
|
2777
|
+
* const unique = Array.dedupeAdjacent(numbers)
|
|
2778
|
+
* assert.deepStrictEqual(unique, [1, 2, 3])
|
|
2779
|
+
*
|
|
2044
2780
|
* @since 2.0.0
|
|
2045
2781
|
*/
|
|
2046
2782
|
export const dedupeAdjacent: <A>(self: Iterable<A>) => Array<A> = dedupeAdjacentWith(Equal.equivalence())
|
|
@@ -2048,6 +2784,13 @@ export const dedupeAdjacent: <A>(self: Iterable<A>) => Array<A> = dedupeAdjacent
|
|
|
2048
2784
|
/**
|
|
2049
2785
|
* Joins the elements together with "sep" in the middle.
|
|
2050
2786
|
*
|
|
2787
|
+
* @example
|
|
2788
|
+
* import { Array } from "effect"
|
|
2789
|
+
*
|
|
2790
|
+
* const strings = ["a", "b", "c"]
|
|
2791
|
+
* const joined = Array.join(strings, "-")
|
|
2792
|
+
* assert.deepStrictEqual(joined, "a-b-c")
|
|
2793
|
+
*
|
|
2051
2794
|
* @since 2.0.0
|
|
2052
2795
|
* @category folding
|
|
2053
2796
|
*/
|
|
@@ -2059,6 +2802,13 @@ export const join: {
|
|
|
2059
2802
|
/**
|
|
2060
2803
|
* Statefully maps over the chunk, producing new elements of type `B`.
|
|
2061
2804
|
*
|
|
2805
|
+
* @example
|
|
2806
|
+
* import { Array } from "effect"
|
|
2807
|
+
*
|
|
2808
|
+
* const numbers = [1, 2, 3]
|
|
2809
|
+
* const result = Array.mapAccum(numbers, 0, (acc, n) => [acc + n, acc + n])
|
|
2810
|
+
* assert.deepStrictEqual(result, [6, [1, 3, 6]])
|
|
2811
|
+
*
|
|
2062
2812
|
* @since 2.0.0
|
|
2063
2813
|
* @category folding
|
|
2064
2814
|
*/
|
|
@@ -2087,6 +2837,14 @@ export const mapAccum: {
|
|
|
2087
2837
|
/**
|
|
2088
2838
|
* Zips this chunk crosswise with the specified chunk using the specified combiner.
|
|
2089
2839
|
*
|
|
2840
|
+
* @example
|
|
2841
|
+
* import { Array } from "effect"
|
|
2842
|
+
*
|
|
2843
|
+
* const array1 = [1, 2]
|
|
2844
|
+
* const array2 = ["a", "b"]
|
|
2845
|
+
* const product = Array.cartesianWith(array1, array2, (a, b) => `${a}-${b}`)
|
|
2846
|
+
* assert.deepStrictEqual(product, ["1-a", "1-b", "2-a", "2-b"])
|
|
2847
|
+
*
|
|
2090
2848
|
* @since 2.0.0
|
|
2091
2849
|
* @category elements
|
|
2092
2850
|
*/
|
|
@@ -2102,6 +2860,14 @@ export const cartesianWith: {
|
|
|
2102
2860
|
/**
|
|
2103
2861
|
* Zips this chunk crosswise with the specified chunk.
|
|
2104
2862
|
*
|
|
2863
|
+
* @example
|
|
2864
|
+
* import { Array } from "effect"
|
|
2865
|
+
*
|
|
2866
|
+
* const array1 = [1, 2]
|
|
2867
|
+
* const array2 = ["a", "b"]
|
|
2868
|
+
* const product = Array.cartesian(array1, array2)
|
|
2869
|
+
* assert.deepStrictEqual(product, [[1, "a"], [1, "b"], [2, "a"], [2, "b"]])
|
|
2870
|
+
*
|
|
2105
2871
|
* @since 2.0.0
|
|
2106
2872
|
* @category elements
|
|
2107
2873
|
*/
|