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/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/version.js +1 -1
- package/dist/dts/Array.d.ts +776 -16
- package/dist/dts/Array.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/version.js +1 -1
- package/package.json +1 -1
- package/src/Array.ts +777 -17
- package/src/internal/pool.ts +9 -6
- package/src/internal/version.ts +1 -1
package/dist/cjs/Array.js
CHANGED
|
@@ -51,6 +51,12 @@ function _interopRequireWildcard(e, r) {
|
|
|
51
51
|
/**
|
|
52
52
|
* Builds a `NonEmptyArray` from an non-empty collection of elements.
|
|
53
53
|
*
|
|
54
|
+
* @example
|
|
55
|
+
* import { Array } from "effect"
|
|
56
|
+
*
|
|
57
|
+
* const result = Array.make(1, 2, 3)
|
|
58
|
+
* assert.deepStrictEqual(result, [1, 2, 3])
|
|
59
|
+
*
|
|
54
60
|
* @category constructors
|
|
55
61
|
* @since 2.0.0
|
|
56
62
|
*/
|
|
@@ -58,6 +64,12 @@ const make = (...elements) => elements;
|
|
|
58
64
|
/**
|
|
59
65
|
* Creates a new `Array` of the specified length.
|
|
60
66
|
*
|
|
67
|
+
* @example
|
|
68
|
+
* import { Array } from "effect"
|
|
69
|
+
*
|
|
70
|
+
* const result = Array.allocate<number>(3)
|
|
71
|
+
* assert.deepStrictEqual(result.length, 3)
|
|
72
|
+
*
|
|
61
73
|
* @category constructors
|
|
62
74
|
* @since 2.0.0
|
|
63
75
|
*/
|
|
@@ -104,9 +116,9 @@ const range = (start, end) => start <= end ? makeBy(end - start + 1, i => start
|
|
|
104
116
|
* **Note**. `n` is normalized to an integer >= 1.
|
|
105
117
|
*
|
|
106
118
|
* @example
|
|
107
|
-
* import {
|
|
119
|
+
* import { Array } from "effect"
|
|
108
120
|
*
|
|
109
|
-
* assert.deepStrictEqual(replicate("a", 3), ["a", "a", "a"])
|
|
121
|
+
* assert.deepStrictEqual(Array.replicate("a", 3), ["a", "a", "a"])
|
|
110
122
|
*
|
|
111
123
|
* @category constructors
|
|
112
124
|
* @since 2.0.0
|
|
@@ -115,6 +127,15 @@ exports.range = range;
|
|
|
115
127
|
const replicate = exports.replicate = /*#__PURE__*/(0, _Function.dual)(2, (a, n) => makeBy(n, () => a));
|
|
116
128
|
/**
|
|
117
129
|
* Creates a new `Array` from an iterable collection of values.
|
|
130
|
+
* If the input is already an array, it returns the input as-is.
|
|
131
|
+
* Otherwise, it converts the iterable collection to an array.
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* import { Array } from "effect"
|
|
135
|
+
*
|
|
136
|
+
* const set = new Set([1, 2, 3])
|
|
137
|
+
* const result = Array.fromIterable(set)
|
|
138
|
+
* assert.deepStrictEqual(result, [1, 2, 3])
|
|
118
139
|
*
|
|
119
140
|
* @category constructors
|
|
120
141
|
* @since 2.0.0
|
|
@@ -126,10 +147,10 @@ const fromIterable = collection => Array.isArray(collection) ? collection : Arra
|
|
|
126
147
|
* @param self - The record to transform.
|
|
127
148
|
*
|
|
128
149
|
* @example
|
|
129
|
-
* import {
|
|
150
|
+
* import { Array } from "effect"
|
|
130
151
|
*
|
|
131
152
|
* const x = { a: 1, b: 2, c: 3 }
|
|
132
|
-
* assert.deepStrictEqual(fromRecord(x), [["a", 1], ["b", 2], ["c", 3]])
|
|
153
|
+
* assert.deepStrictEqual(Array.fromRecord(x), [["a", 1], ["b", 2], ["c", 3]])
|
|
133
154
|
*
|
|
134
155
|
* @category conversions
|
|
135
156
|
* @since 2.0.0
|
|
@@ -137,11 +158,31 @@ const fromIterable = collection => Array.isArray(collection) ? collection : Arra
|
|
|
137
158
|
exports.fromIterable = fromIterable;
|
|
138
159
|
const fromRecord = exports.fromRecord = Record.toEntries;
|
|
139
160
|
/**
|
|
161
|
+
* Converts an `Option` to an array.
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* import { Array, Option } from "effect"
|
|
165
|
+
*
|
|
166
|
+
* assert.deepStrictEqual(Array.fromOption(Option.some(1)), [1])
|
|
167
|
+
* assert.deepStrictEqual(Array.fromOption(Option.none()), [])
|
|
168
|
+
*
|
|
140
169
|
* @category conversions
|
|
141
170
|
* @since 2.0.0
|
|
142
171
|
*/
|
|
143
172
|
const fromOption = exports.fromOption = O.toArray;
|
|
144
173
|
/**
|
|
174
|
+
* Matches the elements of an array, applying functions to cases of empty and non-empty arrays.
|
|
175
|
+
*
|
|
176
|
+
* @example
|
|
177
|
+
* import { Array } from "effect"
|
|
178
|
+
*
|
|
179
|
+
* const match = Array.match({
|
|
180
|
+
* onEmpty: () => "empty",
|
|
181
|
+
* onNonEmpty: ([head, ...tail]) => `head: ${head}, tail: ${tail.length}`
|
|
182
|
+
* })
|
|
183
|
+
* assert.deepStrictEqual(match([]), "empty")
|
|
184
|
+
* assert.deepStrictEqual(match([1, 2, 3]), "head: 1, tail: 2")
|
|
185
|
+
*
|
|
145
186
|
* @category pattern matching
|
|
146
187
|
* @since 2.0.0
|
|
147
188
|
*/
|
|
@@ -150,6 +191,18 @@ const match = exports.match = /*#__PURE__*/(0, _Function.dual)(2, (self, {
|
|
|
150
191
|
onNonEmpty
|
|
151
192
|
}) => isNonEmptyReadonlyArray(self) ? onNonEmpty(self) : onEmpty());
|
|
152
193
|
/**
|
|
194
|
+
* Matches the elements of an array from the left, applying functions to cases of empty and non-empty arrays.
|
|
195
|
+
*
|
|
196
|
+
* @example
|
|
197
|
+
* import { Array } from "effect"
|
|
198
|
+
*
|
|
199
|
+
* const matchLeft = Array.matchLeft({
|
|
200
|
+
* onEmpty: () => "empty",
|
|
201
|
+
* onNonEmpty: (head, tail) => `head: ${head}, tail: ${tail.length}`
|
|
202
|
+
* })
|
|
203
|
+
* assert.deepStrictEqual(matchLeft([]), "empty")
|
|
204
|
+
* assert.deepStrictEqual(matchLeft([1, 2, 3]), "head: 1, tail: 2")
|
|
205
|
+
*
|
|
153
206
|
* @category pattern matching
|
|
154
207
|
* @since 2.0.0
|
|
155
208
|
*/
|
|
@@ -158,6 +211,18 @@ const matchLeft = exports.matchLeft = /*#__PURE__*/(0, _Function.dual)(2, (self,
|
|
|
158
211
|
onNonEmpty
|
|
159
212
|
}) => isNonEmptyReadonlyArray(self) ? onNonEmpty(headNonEmpty(self), tailNonEmpty(self)) : onEmpty());
|
|
160
213
|
/**
|
|
214
|
+
* Matches the elements of an array from the right, applying functions to cases of empty and non-empty arrays.
|
|
215
|
+
*
|
|
216
|
+
* @example
|
|
217
|
+
* import { Array } from "effect"
|
|
218
|
+
*
|
|
219
|
+
* const matchRight = Array.matchRight({
|
|
220
|
+
* onEmpty: () => "empty",
|
|
221
|
+
* onNonEmpty: (init, last) => `init: ${init.length}, last: ${last}`
|
|
222
|
+
* })
|
|
223
|
+
* assert.deepStrictEqual(matchRight([]), "empty")
|
|
224
|
+
* assert.deepStrictEqual(matchRight([1, 2, 3]), "init: 2, last: 3")
|
|
225
|
+
*
|
|
161
226
|
* @category pattern matching
|
|
162
227
|
* @since 2.0.0
|
|
163
228
|
*/
|
|
@@ -168,6 +233,13 @@ const matchRight = exports.matchRight = /*#__PURE__*/(0, _Function.dual)(2, (sel
|
|
|
168
233
|
/**
|
|
169
234
|
* Prepend an element to the front of an `Iterable`, creating a new `NonEmptyArray`.
|
|
170
235
|
*
|
|
236
|
+
* @example
|
|
237
|
+
* import { Array } from "effect"
|
|
238
|
+
*
|
|
239
|
+
* const original = [2, 3, 4];
|
|
240
|
+
* const result = Array.prepend(original, 1);
|
|
241
|
+
* assert.deepStrictEqual(result, [1, 2, 3, 4]);
|
|
242
|
+
*
|
|
171
243
|
* @category concatenating
|
|
172
244
|
* @since 2.0.0
|
|
173
245
|
*/
|
|
@@ -179,10 +251,10 @@ const prepend = exports.prepend = /*#__PURE__*/(0, _Function.dual)(2, (self, hea
|
|
|
179
251
|
* @example
|
|
180
252
|
* import { Array } from "effect"
|
|
181
253
|
*
|
|
182
|
-
*
|
|
183
|
-
*
|
|
184
|
-
*
|
|
185
|
-
* )
|
|
254
|
+
* const prefix = [0, 1];
|
|
255
|
+
* const array = [2, 3];
|
|
256
|
+
* const result = Array.prependAll(array, prefix);
|
|
257
|
+
* assert.deepStrictEqual(result, [0, 1, 2, 3]);
|
|
186
258
|
*
|
|
187
259
|
* @category concatenating
|
|
188
260
|
* @since 2.0.0
|
|
@@ -191,6 +263,13 @@ const prependAll = exports.prependAll = /*#__PURE__*/(0, _Function.dual)(2, (sel
|
|
|
191
263
|
/**
|
|
192
264
|
* Append an element to the end of an `Iterable`, creating a new `NonEmptyArray`.
|
|
193
265
|
*
|
|
266
|
+
* @example
|
|
267
|
+
* import { Array } from "effect"
|
|
268
|
+
*
|
|
269
|
+
* const original = [1, 2, 3];
|
|
270
|
+
* const result = Array.append(original, 4);
|
|
271
|
+
* assert.deepStrictEqual(result, [1, 2, 3, 4]);
|
|
272
|
+
*
|
|
194
273
|
* @category concatenating
|
|
195
274
|
* @since 2.0.0
|
|
196
275
|
*/
|
|
@@ -204,7 +283,22 @@ const append = exports.append = /*#__PURE__*/(0, _Function.dual)(2, (self, last)
|
|
|
204
283
|
*/
|
|
205
284
|
const appendAll = exports.appendAll = /*#__PURE__*/(0, _Function.dual)(2, (self, that) => fromIterable(self).concat(fromIterable(that)));
|
|
206
285
|
/**
|
|
207
|
-
*
|
|
286
|
+
* Accumulates values from an `Iterable` starting from the left, storing
|
|
287
|
+
* each intermediate result in an array. Useful for tracking the progression of
|
|
288
|
+
* a value through a series of transformations.
|
|
289
|
+
*
|
|
290
|
+
* @example
|
|
291
|
+
* import { Array } from "effect";
|
|
292
|
+
*
|
|
293
|
+
* const numbers = [1, 2, 3, 4]
|
|
294
|
+
* const result = Array.scan(numbers, 0, (acc, value) => acc + value)
|
|
295
|
+
* assert.deepStrictEqual(result, [0, 1, 3, 6, 10])
|
|
296
|
+
*
|
|
297
|
+
* // Explanation:
|
|
298
|
+
* // This function starts with the initial value (0 in this case)
|
|
299
|
+
* // and adds each element of the array to this accumulator one by one,
|
|
300
|
+
* // keeping track of the cumulative sum after each addition.
|
|
301
|
+
* // Each of these sums is captured in the resulting array.
|
|
208
302
|
*
|
|
209
303
|
* @category folding
|
|
210
304
|
* @since 2.0.0
|
|
@@ -219,7 +313,16 @@ const scan = exports.scan = /*#__PURE__*/(0, _Function.dual)(3, (self, b, f) =>
|
|
|
219
313
|
return out;
|
|
220
314
|
});
|
|
221
315
|
/**
|
|
222
|
-
*
|
|
316
|
+
* Accumulates values from an `Iterable` starting from the right, storing
|
|
317
|
+
* each intermediate result in an array. Useful for tracking the progression of
|
|
318
|
+
* a value through a series of transformations.
|
|
319
|
+
*
|
|
320
|
+
* @example
|
|
321
|
+
* import { Array } from "effect";
|
|
322
|
+
*
|
|
323
|
+
* const numbers = [1, 2, 3, 4]
|
|
324
|
+
* const result = Array.scanRight(numbers, 0, (acc, value) => acc + value)
|
|
325
|
+
* assert.deepStrictEqual(result, [10, 9, 7, 4, 0])
|
|
223
326
|
*
|
|
224
327
|
* @category folding
|
|
225
328
|
* @since 2.0.0
|
|
@@ -349,6 +452,12 @@ const unsafeGet = exports.unsafeGet = /*#__PURE__*/(0, _Function.dual)(2, (self,
|
|
|
349
452
|
/**
|
|
350
453
|
* Return a tuple containing the first element, and a new `Array` of the remaining elements, if any.
|
|
351
454
|
*
|
|
455
|
+
* @example
|
|
456
|
+
* import { Array } from "effect";
|
|
457
|
+
*
|
|
458
|
+
* const result = Array.unprepend([1, 2, 3, 4])
|
|
459
|
+
* assert.deepStrictEqual(result, [1, [2, 3, 4]])
|
|
460
|
+
*
|
|
352
461
|
* @category splitting
|
|
353
462
|
* @since 2.0.0
|
|
354
463
|
*/
|
|
@@ -356,6 +465,12 @@ const unprepend = self => [headNonEmpty(self), tailNonEmpty(self)];
|
|
|
356
465
|
/**
|
|
357
466
|
* Return a tuple containing a copy of the `NonEmptyReadonlyArray` without its last element, and that last element.
|
|
358
467
|
*
|
|
468
|
+
* @example
|
|
469
|
+
* import { Array } from "effect";
|
|
470
|
+
*
|
|
471
|
+
* const result = Array.unappend([1, 2, 3, 4])
|
|
472
|
+
* assert.deepStrictEqual(result, [[1, 2, 3], 4])
|
|
473
|
+
*
|
|
359
474
|
* @category splitting
|
|
360
475
|
* @since 2.0.0
|
|
361
476
|
*/
|
|
@@ -370,6 +485,14 @@ const unappend = self => [initNonEmpty(self), lastNonEmpty(self)];
|
|
|
370
485
|
exports.unappend = unappend;
|
|
371
486
|
const head = exports.head = /*#__PURE__*/get(0);
|
|
372
487
|
/**
|
|
488
|
+
* Get the first element of a non empty array.
|
|
489
|
+
*
|
|
490
|
+
* @example
|
|
491
|
+
* import { Array } from "effect"
|
|
492
|
+
*
|
|
493
|
+
* const result = Array.headNonEmpty([1, 2, 3, 4])
|
|
494
|
+
* assert.deepStrictEqual(result, 1)
|
|
495
|
+
*
|
|
373
496
|
* @category getters
|
|
374
497
|
* @since 2.0.0
|
|
375
498
|
*/
|
|
@@ -382,6 +505,14 @@ const headNonEmpty = exports.headNonEmpty = /*#__PURE__*/unsafeGet(0);
|
|
|
382
505
|
*/
|
|
383
506
|
const last = self => isNonEmptyReadonlyArray(self) ? O.some(lastNonEmpty(self)) : O.none();
|
|
384
507
|
/**
|
|
508
|
+
* Get the last element of a non empty array.
|
|
509
|
+
*
|
|
510
|
+
* @example
|
|
511
|
+
* import { Array } from "effect"
|
|
512
|
+
*
|
|
513
|
+
* const result = Array.lastNonEmpty([1, 2, 3, 4])
|
|
514
|
+
* assert.deepStrictEqual(result, 4)
|
|
515
|
+
*
|
|
385
516
|
* @category getters
|
|
386
517
|
* @since 2.0.0
|
|
387
518
|
*/
|
|
@@ -399,6 +530,14 @@ const tail = self => {
|
|
|
399
530
|
return isNonEmptyReadonlyArray(input) ? O.some(tailNonEmpty(input)) : O.none();
|
|
400
531
|
};
|
|
401
532
|
/**
|
|
533
|
+
* Get all but the first element of a `NonEmptyReadonlyArray`.
|
|
534
|
+
*
|
|
535
|
+
* @example
|
|
536
|
+
* import { Array } from "effect"
|
|
537
|
+
*
|
|
538
|
+
* const result = Array.tailNonEmpty([1, 2, 3, 4])
|
|
539
|
+
* assert.deepStrictEqual(result, [2, 3, 4])
|
|
540
|
+
*
|
|
402
541
|
* @category getters
|
|
403
542
|
* @since 2.0.0
|
|
404
543
|
*/
|
|
@@ -418,6 +557,12 @@ const init = self => {
|
|
|
418
557
|
/**
|
|
419
558
|
* Get all but the last element of a non empty array, creating a new array.
|
|
420
559
|
*
|
|
560
|
+
* @example
|
|
561
|
+
* import { Array } from "effect"
|
|
562
|
+
*
|
|
563
|
+
* const result = Array.initNonEmpty([1, 2, 3, 4])
|
|
564
|
+
* assert.deepStrictEqual(result, [1, 2, 3])
|
|
565
|
+
*
|
|
421
566
|
* @category getters
|
|
422
567
|
* @since 2.0.0
|
|
423
568
|
*/
|
|
@@ -428,6 +573,13 @@ const initNonEmpty = self => self.slice(0, -1);
|
|
|
428
573
|
*
|
|
429
574
|
* **Note**. `n` is normalized to a non negative integer.
|
|
430
575
|
*
|
|
576
|
+
* @example
|
|
577
|
+
* import { Array } from "effect"
|
|
578
|
+
*
|
|
579
|
+
* const numbers = [1, 2, 3, 4, 5]
|
|
580
|
+
* const result = Array.take(numbers, 3)
|
|
581
|
+
* assert.deepStrictEqual(result, [1, 2, 3])
|
|
582
|
+
*
|
|
431
583
|
* @category getters
|
|
432
584
|
* @since 2.0.0
|
|
433
585
|
*/
|
|
@@ -441,6 +593,13 @@ const take = exports.take = /*#__PURE__*/(0, _Function.dual)(2, (self, n) => {
|
|
|
441
593
|
*
|
|
442
594
|
* **Note**. `n` is normalized to a non negative integer.
|
|
443
595
|
*
|
|
596
|
+
* @example
|
|
597
|
+
* import { Array } from "effect"
|
|
598
|
+
*
|
|
599
|
+
* const numbers = [1, 2, 3, 4, 5]
|
|
600
|
+
* const result = Array.takeRight(numbers, 3)
|
|
601
|
+
* assert.deepStrictEqual(result, [3, 4, 5])
|
|
602
|
+
*
|
|
444
603
|
* @category getters
|
|
445
604
|
* @since 2.0.0
|
|
446
605
|
*/
|
|
@@ -452,6 +611,19 @@ const takeRight = exports.takeRight = /*#__PURE__*/(0, _Function.dual)(2, (self,
|
|
|
452
611
|
/**
|
|
453
612
|
* Calculate the longest initial subarray for which all element satisfy the specified predicate, creating a new `Array`.
|
|
454
613
|
*
|
|
614
|
+
* @example
|
|
615
|
+
* import { Array } from "effect"
|
|
616
|
+
*
|
|
617
|
+
* const numbers = [1, 3, 2, 4, 1, 2]
|
|
618
|
+
* const result = Array.takeWhile(numbers, x => x < 4)
|
|
619
|
+
* assert.deepStrictEqual(result, [1, 3, 2])
|
|
620
|
+
*
|
|
621
|
+
* // Explanation:
|
|
622
|
+
* // - The function starts with the first element (`1`), which is less than `4`, so it adds `1` to the result.
|
|
623
|
+
* // - The next element (`3`) is also less than `4`, so it adds `3`.
|
|
624
|
+
* // - The next element (`2`) is again less than `4`, so it adds `2`.
|
|
625
|
+
* // - The function then encounters `4`, which is not less than `4`. At this point, it stops checking further elements and finalizes the result.
|
|
626
|
+
*
|
|
455
627
|
* @category getters
|
|
456
628
|
* @since 2.0.0
|
|
457
629
|
*/
|
|
@@ -492,6 +664,13 @@ const span = exports.span = /*#__PURE__*/(0, _Function.dual)(2, (self, predicate
|
|
|
492
664
|
*
|
|
493
665
|
* **Note**. `n` is normalized to a non negative integer.
|
|
494
666
|
*
|
|
667
|
+
* @example
|
|
668
|
+
* import { Array } from "effect"
|
|
669
|
+
*
|
|
670
|
+
* const numbers = [1, 2, 3, 4, 5]
|
|
671
|
+
* const result = Array.drop(numbers, 2)
|
|
672
|
+
* assert.deepStrictEqual(result, [3, 4, 5])
|
|
673
|
+
*
|
|
495
674
|
* @category getters
|
|
496
675
|
* @since 2.0.0
|
|
497
676
|
*/
|
|
@@ -504,6 +683,13 @@ const drop = exports.drop = /*#__PURE__*/(0, _Function.dual)(2, (self, n) => {
|
|
|
504
683
|
*
|
|
505
684
|
* **Note**. `n` is normalized to a non negative integer.
|
|
506
685
|
*
|
|
686
|
+
* @example
|
|
687
|
+
* import { Array } from "effect"
|
|
688
|
+
*
|
|
689
|
+
* const numbers = [1, 2, 3, 4, 5]
|
|
690
|
+
* const result = Array.dropRight(numbers, 2)
|
|
691
|
+
* assert.deepStrictEqual(result, [1, 2, 3])
|
|
692
|
+
*
|
|
507
693
|
* @category getters
|
|
508
694
|
* @since 2.0.0
|
|
509
695
|
*/
|
|
@@ -514,6 +700,13 @@ const dropRight = exports.dropRight = /*#__PURE__*/(0, _Function.dual)(2, (self,
|
|
|
514
700
|
/**
|
|
515
701
|
* Remove the longest initial subarray for which all element satisfy the specified predicate, creating a new `Array`.
|
|
516
702
|
*
|
|
703
|
+
* @example
|
|
704
|
+
* import { Array } from "effect"
|
|
705
|
+
*
|
|
706
|
+
* const numbers = [1, 2, 3, 4, 5]
|
|
707
|
+
* const result = Array.dropWhile(numbers, x => x < 4)
|
|
708
|
+
* assert.deepStrictEqual(result, [4, 5])
|
|
709
|
+
*
|
|
517
710
|
* @category getters
|
|
518
711
|
* @since 2.0.0
|
|
519
712
|
*/
|
|
@@ -521,6 +714,13 @@ const dropWhile = exports.dropWhile = /*#__PURE__*/(0, _Function.dual)(2, (self,
|
|
|
521
714
|
/**
|
|
522
715
|
* Return the first index for which a predicate holds.
|
|
523
716
|
*
|
|
717
|
+
* @example
|
|
718
|
+
* import { Array, Option } from "effect"
|
|
719
|
+
*
|
|
720
|
+
* const numbers = [5, 3, 8, 9]
|
|
721
|
+
* const result = Array.findFirstIndex(numbers, x => x > 5)
|
|
722
|
+
* assert.deepStrictEqual(result, Option.some(2))
|
|
723
|
+
*
|
|
524
724
|
* @category elements
|
|
525
725
|
* @since 2.0.0
|
|
526
726
|
*/
|
|
@@ -537,6 +737,13 @@ const findFirstIndex = exports.findFirstIndex = /*#__PURE__*/(0, _Function.dual)
|
|
|
537
737
|
/**
|
|
538
738
|
* Return the last index for which a predicate holds.
|
|
539
739
|
*
|
|
740
|
+
* @example
|
|
741
|
+
* import { Array, Option } from "effect"
|
|
742
|
+
*
|
|
743
|
+
* const numbers = [1, 3, 8, 9]
|
|
744
|
+
* const result = Array.findLastIndex(numbers, x => x < 5)
|
|
745
|
+
* assert.deepStrictEqual(result, Option.some(1))
|
|
746
|
+
*
|
|
540
747
|
* @category elements
|
|
541
748
|
* @since 2.0.0
|
|
542
749
|
*/
|
|
@@ -553,12 +760,27 @@ const findLastIndex = exports.findLastIndex = /*#__PURE__*/(0, _Function.dual)(2
|
|
|
553
760
|
* Returns the first element that satisfies the specified
|
|
554
761
|
* predicate, or `None` if no such element exists.
|
|
555
762
|
*
|
|
763
|
+
* @example
|
|
764
|
+
* import { Array, Option } from "effect"
|
|
765
|
+
*
|
|
766
|
+
* const numbers = [1, 2, 3, 4, 5]
|
|
767
|
+
* const result = Array.findFirst(numbers, x => x > 3)
|
|
768
|
+
* assert.deepStrictEqual(result, Option.some(4))
|
|
769
|
+
*
|
|
556
770
|
* @category elements
|
|
557
771
|
* @since 2.0.0
|
|
558
772
|
*/
|
|
559
773
|
const findFirst = exports.findFirst = EffectIterable.findFirst;
|
|
560
774
|
/**
|
|
561
|
-
*
|
|
775
|
+
* Finds the last element in an iterable collection that satisfies the given predicate or refinement.
|
|
776
|
+
* Returns an `Option` containing the found element, or `Option.none` if no element matches.
|
|
777
|
+
*
|
|
778
|
+
* @example
|
|
779
|
+
* import { Array, Option } from "effect"
|
|
780
|
+
*
|
|
781
|
+
* const numbers = [1, 2, 3, 4, 5]
|
|
782
|
+
* const result = Array.findLast(numbers, n => n % 2 === 0)
|
|
783
|
+
* assert.deepStrictEqual(result, Option.some(4))
|
|
562
784
|
*
|
|
563
785
|
* @category elements
|
|
564
786
|
* @since 2.0.0
|
|
@@ -584,6 +806,13 @@ const findLast = exports.findLast = /*#__PURE__*/(0, _Function.dual)(2, (self, f
|
|
|
584
806
|
* Insert an element at the specified index, creating a new `NonEmptyArray`,
|
|
585
807
|
* or return `None` if the index is out of bounds.
|
|
586
808
|
*
|
|
809
|
+
* @example
|
|
810
|
+
* import { Array, Option } from "effect"
|
|
811
|
+
*
|
|
812
|
+
* const letters = ['a', 'b', 'c', 'e']
|
|
813
|
+
* const result = Array.insertAt(letters, 3, 'd')
|
|
814
|
+
* assert.deepStrictEqual(result, Option.some(['a', 'b', 'c', 'd', 'e']))
|
|
815
|
+
*
|
|
587
816
|
* @since 2.0.0
|
|
588
817
|
*/
|
|
589
818
|
const insertAt = exports.insertAt = /*#__PURE__*/(0, _Function.dual)(3, (self, i, b) => {
|
|
@@ -599,10 +828,26 @@ const insertAt = exports.insertAt = /*#__PURE__*/(0, _Function.dual)(3, (self, i
|
|
|
599
828
|
* Change the element at the specified index, creating a new `Array`,
|
|
600
829
|
* or return a copy of the input if the index is out of bounds.
|
|
601
830
|
*
|
|
831
|
+
* @example
|
|
832
|
+
* import { Array } from "effect"
|
|
833
|
+
*
|
|
834
|
+
* const letters = ['a', 'b', 'c', 'd']
|
|
835
|
+
* const result = Array.replace(1, 'z')(letters)
|
|
836
|
+
* assert.deepStrictEqual(result, ['a', 'z', 'c', 'd'])
|
|
837
|
+
*
|
|
602
838
|
* @since 2.0.0
|
|
603
839
|
*/
|
|
604
840
|
const replace = exports.replace = /*#__PURE__*/(0, _Function.dual)(3, (self, i, b) => modify(self, i, () => b));
|
|
605
841
|
/**
|
|
842
|
+
* Replaces an element in an array with the given value, returning an option of the updated array.
|
|
843
|
+
*
|
|
844
|
+
* @example
|
|
845
|
+
* import { Array, Option } from "effect"
|
|
846
|
+
*
|
|
847
|
+
* const numbers = [1, 2, 3]
|
|
848
|
+
* const result = Array.replaceOption(numbers, 1, 4)
|
|
849
|
+
* assert.deepStrictEqual(result, Option.some([1, 4, 3]))
|
|
850
|
+
*
|
|
606
851
|
* @since 2.0.0
|
|
607
852
|
*/
|
|
608
853
|
const replaceOption = exports.replaceOption = /*#__PURE__*/(0, _Function.dual)(3, (self, i, b) => modifyOption(self, i, () => b));
|
|
@@ -610,6 +855,13 @@ const replaceOption = exports.replaceOption = /*#__PURE__*/(0, _Function.dual)(3
|
|
|
610
855
|
* Apply a function to the element at the specified index, creating a new `Array`,
|
|
611
856
|
* or return a copy of the input if the index is out of bounds.
|
|
612
857
|
*
|
|
858
|
+
* @example
|
|
859
|
+
* import { Array } from "effect"
|
|
860
|
+
*
|
|
861
|
+
* const numbers = [1, 2, 3, 4]
|
|
862
|
+
* const result = Array.modify(numbers, 2, (n) => n * 2)
|
|
863
|
+
* assert.deepStrictEqual(result, [1, 2, 6, 4])
|
|
864
|
+
*
|
|
613
865
|
* @since 2.0.0
|
|
614
866
|
*/
|
|
615
867
|
const modify = exports.modify = /*#__PURE__*/(0, _Function.dual)(3, (self, i, f) => O.getOrElse(modifyOption(self, i, f), () => Array.from(self)));
|
|
@@ -617,6 +869,16 @@ const modify = exports.modify = /*#__PURE__*/(0, _Function.dual)(3, (self, i, f)
|
|
|
617
869
|
* Apply a function to the element at the specified index, creating a new `Array`,
|
|
618
870
|
* or return `None` if the index is out of bounds.
|
|
619
871
|
*
|
|
872
|
+
* @example
|
|
873
|
+
* import { Array, Option } from "effect"
|
|
874
|
+
*
|
|
875
|
+
* const numbers = [1, 2, 3, 4]
|
|
876
|
+
* const result = Array.modifyOption(numbers, 2, (n) => n * 2)
|
|
877
|
+
* assert.deepStrictEqual(result, Option.some([1, 2, 6, 4]))
|
|
878
|
+
*
|
|
879
|
+
* const outOfBoundsResult = Array.modifyOption(numbers, 5, (n) => n * 2)
|
|
880
|
+
* assert.deepStrictEqual(outOfBoundsResult, Option.none())
|
|
881
|
+
*
|
|
620
882
|
* @since 2.0.0
|
|
621
883
|
*/
|
|
622
884
|
const modifyOption = exports.modifyOption = /*#__PURE__*/(0, _Function.dual)(3, (self, i, f) => {
|
|
@@ -633,6 +895,16 @@ const modifyOption = exports.modifyOption = /*#__PURE__*/(0, _Function.dual)(3,
|
|
|
633
895
|
* Delete the element at the specified index, creating a new `Array`,
|
|
634
896
|
* or return a copy of the input if the index is out of bounds.
|
|
635
897
|
*
|
|
898
|
+
* @example
|
|
899
|
+
* import { Array } from "effect"
|
|
900
|
+
*
|
|
901
|
+
* const numbers = [1, 2, 3, 4]
|
|
902
|
+
* const result = Array.remove(numbers, 2)
|
|
903
|
+
* assert.deepStrictEqual(result, [1, 2, 4])
|
|
904
|
+
*
|
|
905
|
+
* const outOfBoundsResult = Array.remove(numbers, 5)
|
|
906
|
+
* assert.deepStrictEqual(outOfBoundsResult, [1, 2, 3, 4])
|
|
907
|
+
*
|
|
636
908
|
* @since 2.0.0
|
|
637
909
|
*/
|
|
638
910
|
const remove = exports.remove = /*#__PURE__*/(0, _Function.dual)(2, (self, i) => {
|
|
@@ -646,6 +918,13 @@ const remove = exports.remove = /*#__PURE__*/(0, _Function.dual)(2, (self, i) =>
|
|
|
646
918
|
/**
|
|
647
919
|
* Reverse an `Iterable`, creating a new `Array`.
|
|
648
920
|
*
|
|
921
|
+
* @example
|
|
922
|
+
* import { Array } from "effect"
|
|
923
|
+
*
|
|
924
|
+
* const numbers = [1, 2, 3, 4]
|
|
925
|
+
* const result = Array.reverse(numbers)
|
|
926
|
+
* assert.deepStrictEqual(result, [4, 3, 2, 1])
|
|
927
|
+
*
|
|
649
928
|
* @category elements
|
|
650
929
|
* @since 2.0.0
|
|
651
930
|
*/
|
|
@@ -664,13 +943,54 @@ const sort = exports.sort = /*#__PURE__*/(0, _Function.dual)(2, (self, O) => {
|
|
|
664
943
|
return out;
|
|
665
944
|
});
|
|
666
945
|
/**
|
|
946
|
+
* Sorts an array based on a provided mapping function and order. The mapping
|
|
947
|
+
* function transforms the elements into a value that can be compared, and the
|
|
948
|
+
* order defines how those values should be sorted.
|
|
949
|
+
*
|
|
950
|
+
* @example
|
|
951
|
+
* import { Array, Order } from "effect"
|
|
952
|
+
*
|
|
953
|
+
* const strings = ["aaa", "b", "cc"]
|
|
954
|
+
* const result = Array.sortWith(strings, (s) => s.length, Order.number)
|
|
955
|
+
* assert.deepStrictEqual(result, ["b", "cc", "aaa"])
|
|
956
|
+
*
|
|
957
|
+
* // Explanation:
|
|
958
|
+
* // The array of strings is sorted based on their lengths. The mapping function `(s) => s.length`
|
|
959
|
+
* // converts each string into its length, and the `Order.number` specifies that the lengths should
|
|
960
|
+
* // be sorted in ascending order.
|
|
961
|
+
*
|
|
667
962
|
* @since 2.0.0
|
|
668
963
|
* @category elements
|
|
669
964
|
*/
|
|
670
965
|
const sortWith = exports.sortWith = /*#__PURE__*/(0, _Function.dual)(3, (self, f, order) => sort(self, Order.mapInput(order, f)));
|
|
671
966
|
/**
|
|
672
|
-
*
|
|
673
|
-
* using first `orders
|
|
967
|
+
* Sorts the elements of an `Iterable` in increasing order based on the provided
|
|
968
|
+
* orders. The elements are compared using the first order in `orders`, then the
|
|
969
|
+
* second order if the first comparison is equal, and so on.
|
|
970
|
+
*
|
|
971
|
+
* @example
|
|
972
|
+
* import { Array, Order } from "effect"
|
|
973
|
+
*
|
|
974
|
+
* const users = [
|
|
975
|
+
* { name: "Alice", age: 30 },
|
|
976
|
+
* { name: "Bob", age: 25 },
|
|
977
|
+
* { name: "Charlie", age: 30 }
|
|
978
|
+
* ]
|
|
979
|
+
*
|
|
980
|
+
* const result = Array.sortBy(
|
|
981
|
+
* Order.mapInput(Order.number, (user: (typeof users)[number]) => user.age),
|
|
982
|
+
* Order.mapInput(Order.string, (user: (typeof users)[number]) => user.name)
|
|
983
|
+
* )(users)
|
|
984
|
+
*
|
|
985
|
+
* assert.deepStrictEqual(result, [
|
|
986
|
+
* { name: "Bob", age: 25 },
|
|
987
|
+
* { name: "Alice", age: 30 },
|
|
988
|
+
* { name: "Charlie", age: 30 }
|
|
989
|
+
* ])
|
|
990
|
+
*
|
|
991
|
+
* // Explanation:
|
|
992
|
+
* // The array of users is sorted first by age in ascending order. When ages are equal,
|
|
993
|
+
* // the users are further sorted by name in ascending order.
|
|
674
994
|
*
|
|
675
995
|
* @category sorting
|
|
676
996
|
* @since 2.0.0
|
|
@@ -690,6 +1010,14 @@ const sortBy = (...orders) => {
|
|
|
690
1010
|
* If one input `Iterable` is short, excess elements of the
|
|
691
1011
|
* longer `Iterable` are discarded.
|
|
692
1012
|
*
|
|
1013
|
+
* @example
|
|
1014
|
+
* import { Array } from "effect"
|
|
1015
|
+
*
|
|
1016
|
+
* const array1 = [1, 2, 3]
|
|
1017
|
+
* const array2 = ['a', 'b']
|
|
1018
|
+
* const result = Array.zip(array1, array2)
|
|
1019
|
+
* assert.deepStrictEqual(result, [[1, 'a'], [2, 'b']])
|
|
1020
|
+
*
|
|
693
1021
|
* @category zipping
|
|
694
1022
|
* @since 2.0.0
|
|
695
1023
|
*/
|
|
@@ -699,6 +1027,14 @@ const zip = exports.zip = /*#__PURE__*/(0, _Function.dual)(2, (self, that) => zi
|
|
|
699
1027
|
* Apply a function to pairs of elements at the same index in two `Iterable`s, collecting the results in a new `Array`. If one
|
|
700
1028
|
* input `Iterable` is short, excess elements of the longer `Iterable` are discarded.
|
|
701
1029
|
*
|
|
1030
|
+
* @example
|
|
1031
|
+
* import { Array } from "effect"
|
|
1032
|
+
*
|
|
1033
|
+
* const array1 = [1, 2, 3]
|
|
1034
|
+
* const array2 = [4, 5, 6]
|
|
1035
|
+
* const result = Array.zipWith(array1, array2, (a, b) => a + b)
|
|
1036
|
+
* assert.deepStrictEqual(result, [5, 7, 9])
|
|
1037
|
+
*
|
|
702
1038
|
* @category zipping
|
|
703
1039
|
* @since 2.0.0
|
|
704
1040
|
*/
|
|
@@ -718,6 +1054,12 @@ const zipWith = exports.zipWith = /*#__PURE__*/(0, _Function.dual)(3, (self, tha
|
|
|
718
1054
|
/**
|
|
719
1055
|
* This function is the inverse of `zip`. Takes an `Iterable` of pairs and return two corresponding `Array`s.
|
|
720
1056
|
*
|
|
1057
|
+
* @example
|
|
1058
|
+
* import { Array } from "effect"
|
|
1059
|
+
*
|
|
1060
|
+
* const result = Array.unzip([[1, "a"], [2, "b"], [3, "c"]])
|
|
1061
|
+
* assert.deepStrictEqual(result, [[1, 2, 3], ['a', 'b', 'c']])
|
|
1062
|
+
*
|
|
721
1063
|
* @since 2.0.0
|
|
722
1064
|
*/
|
|
723
1065
|
const unzip = self => {
|
|
@@ -737,6 +1079,13 @@ const unzip = self => {
|
|
|
737
1079
|
* Places an element in between members of an `Iterable`.
|
|
738
1080
|
* If the input is a non-empty array, the result is also a non-empty array.
|
|
739
1081
|
*
|
|
1082
|
+
* @example
|
|
1083
|
+
* import { Array } from "effect"
|
|
1084
|
+
*
|
|
1085
|
+
* const numbers = [1, 2, 3]
|
|
1086
|
+
* const result = Array.intersperse(numbers, 0)
|
|
1087
|
+
* assert.deepStrictEqual(result, [1, 0, 2, 0, 3])
|
|
1088
|
+
*
|
|
740
1089
|
* @since 2.0.0
|
|
741
1090
|
*/
|
|
742
1091
|
exports.unzip = unzip;
|
|
@@ -758,24 +1107,48 @@ const intersperse = exports.intersperse = /*#__PURE__*/(0, _Function.dual)(2, (s
|
|
|
758
1107
|
/**
|
|
759
1108
|
* Apply a function to the head, creating a new `NonEmptyReadonlyArray`.
|
|
760
1109
|
*
|
|
1110
|
+
* @example
|
|
1111
|
+
* import { Array } from "effect"
|
|
1112
|
+
*
|
|
1113
|
+
* const result = Array.modifyNonEmptyHead([1, 2, 3], n => n * 10)
|
|
1114
|
+
* assert.deepStrictEqual(result, [10, 2, 3])
|
|
1115
|
+
*
|
|
761
1116
|
* @since 2.0.0
|
|
762
1117
|
*/
|
|
763
1118
|
const modifyNonEmptyHead = exports.modifyNonEmptyHead = /*#__PURE__*/(0, _Function.dual)(2, (self, f) => [f(headNonEmpty(self)), ...tailNonEmpty(self)]);
|
|
764
1119
|
/**
|
|
765
1120
|
* Change the head, creating a new `NonEmptyReadonlyArray`.
|
|
766
1121
|
*
|
|
1122
|
+
* @example
|
|
1123
|
+
* import { Array } from "effect"
|
|
1124
|
+
*
|
|
1125
|
+
* const result = Array.setNonEmptyHead([1, 2, 3], 10)
|
|
1126
|
+
* assert.deepStrictEqual(result, [10, 2, 3])
|
|
1127
|
+
*
|
|
767
1128
|
* @since 2.0.0
|
|
768
1129
|
*/
|
|
769
1130
|
const setNonEmptyHead = exports.setNonEmptyHead = /*#__PURE__*/(0, _Function.dual)(2, (self, b) => modifyNonEmptyHead(self, () => b));
|
|
770
1131
|
/**
|
|
771
1132
|
* Apply a function to the last element, creating a new `NonEmptyReadonlyArray`.
|
|
772
1133
|
*
|
|
1134
|
+
* @example
|
|
1135
|
+
* import { Array } from "effect"
|
|
1136
|
+
*
|
|
1137
|
+
* const result = Array.modifyNonEmptyLast([1, 2, 3], n => n * 2)
|
|
1138
|
+
* assert.deepStrictEqual(result, [1, 2, 6])
|
|
1139
|
+
*
|
|
773
1140
|
* @since 2.0.0
|
|
774
1141
|
*/
|
|
775
1142
|
const modifyNonEmptyLast = exports.modifyNonEmptyLast = /*#__PURE__*/(0, _Function.dual)(2, (self, f) => append(initNonEmpty(self), f(lastNonEmpty(self))));
|
|
776
1143
|
/**
|
|
777
1144
|
* Change the last element, creating a new `NonEmptyReadonlyArray`.
|
|
778
1145
|
*
|
|
1146
|
+
* @example
|
|
1147
|
+
* import { Array } from "effect"
|
|
1148
|
+
*
|
|
1149
|
+
* const result = Array.setNonEmptyLast([1, 2, 3], 4)
|
|
1150
|
+
* assert.deepStrictEqual(result, [1, 2, 4])
|
|
1151
|
+
*
|
|
779
1152
|
* @since 2.0.0
|
|
780
1153
|
*/
|
|
781
1154
|
const setNonEmptyLast = exports.setNonEmptyLast = /*#__PURE__*/(0, _Function.dual)(2, (self, b) => modifyNonEmptyLast(self, () => b));
|
|
@@ -783,6 +1156,13 @@ const setNonEmptyLast = exports.setNonEmptyLast = /*#__PURE__*/(0, _Function.dua
|
|
|
783
1156
|
* Rotate an `Iterable` by `n` steps.
|
|
784
1157
|
* If the input is a non-empty array, the result is also a non-empty array.
|
|
785
1158
|
*
|
|
1159
|
+
* @example
|
|
1160
|
+
* import { Array } from "effect"
|
|
1161
|
+
*
|
|
1162
|
+
* const letters = ['a', 'b', 'c', 'd']
|
|
1163
|
+
* const result = Array.rotate(letters, 2)
|
|
1164
|
+
* assert.deepStrictEqual(result, ['c', 'd', 'a', 'b'])
|
|
1165
|
+
*
|
|
786
1166
|
* @since 2.0.0
|
|
787
1167
|
*/
|
|
788
1168
|
const rotate = exports.rotate = /*#__PURE__*/(0, _Function.dual)(2, (self, n) => {
|
|
@@ -805,6 +1185,15 @@ const rotate = exports.rotate = /*#__PURE__*/(0, _Function.dual)(2, (self, n) =>
|
|
|
805
1185
|
/**
|
|
806
1186
|
* Returns a function that checks if a `ReadonlyArray` contains a given value using a provided `isEquivalent` function.
|
|
807
1187
|
*
|
|
1188
|
+
* @example
|
|
1189
|
+
* import { Array } from "effect"
|
|
1190
|
+
*
|
|
1191
|
+
* const numbers = [1, 2, 3, 4]
|
|
1192
|
+
* const isEquivalent = (a: number, b: number) => a === b
|
|
1193
|
+
* const containsNumber = Array.containsWith(isEquivalent)
|
|
1194
|
+
* const result = containsNumber(3)(numbers)
|
|
1195
|
+
* assert.deepStrictEqual(result, true)
|
|
1196
|
+
*
|
|
808
1197
|
* @category elements
|
|
809
1198
|
* @since 2.0.0
|
|
810
1199
|
*/
|
|
@@ -821,6 +1210,13 @@ const _equivalence = /*#__PURE__*/Equal.equivalence();
|
|
|
821
1210
|
/**
|
|
822
1211
|
* Returns a function that checks if a `ReadonlyArray` contains a given value using the default `Equivalence`.
|
|
823
1212
|
*
|
|
1213
|
+
* @example
|
|
1214
|
+
* import { Array } from "effect"
|
|
1215
|
+
*
|
|
1216
|
+
* const letters = ['a', 'b', 'c', 'd']
|
|
1217
|
+
* const result = Array.contains('c')(letters)
|
|
1218
|
+
* assert.deepStrictEqual(result, true)
|
|
1219
|
+
*
|
|
824
1220
|
* @category elements
|
|
825
1221
|
* @since 2.0.0
|
|
826
1222
|
*/
|
|
@@ -830,6 +1226,18 @@ const contains = exports.contains = /*#__PURE__*/containsWith(_equivalence);
|
|
|
830
1226
|
* `Iterable`. Typically chop is called with some function that will consume an initial prefix of the `Iterable` and produce a
|
|
831
1227
|
* value and the rest of the `Array`.
|
|
832
1228
|
*
|
|
1229
|
+
* @example
|
|
1230
|
+
* import { Array } from "effect"
|
|
1231
|
+
*
|
|
1232
|
+
* const numbers = [1, 2, 3, 4, 5]
|
|
1233
|
+
* const result = Array.chop(numbers, (as): [number, Array<number>] => [as[0] * 2, as.slice(1)])
|
|
1234
|
+
* assert.deepStrictEqual(result, [2, 4, 6, 8, 10])
|
|
1235
|
+
*
|
|
1236
|
+
* // Explanation:
|
|
1237
|
+
* // The `chopFunction` takes the first element of the array, doubles it, and then returns it along with the rest of the array.
|
|
1238
|
+
* // The `chop` function applies this `chopFunction` recursively to the input array `[1, 2, 3, 4, 5]`,
|
|
1239
|
+
* // resulting in a new array `[2, 4, 6, 8, 10]`.
|
|
1240
|
+
*
|
|
833
1241
|
* @since 2.0.0
|
|
834
1242
|
*/
|
|
835
1243
|
const chop = exports.chop = /*#__PURE__*/(0, _Function.dual)(2, (self, f) => {
|
|
@@ -851,6 +1259,13 @@ const chop = exports.chop = /*#__PURE__*/(0, _Function.dual)(2, (self, f) => {
|
|
|
851
1259
|
* Splits an `Iterable` into two segments, with the first segment containing a maximum of `n` elements.
|
|
852
1260
|
* The value of `n` can be `0`.
|
|
853
1261
|
*
|
|
1262
|
+
* @example
|
|
1263
|
+
* import { Array } from "effect"
|
|
1264
|
+
*
|
|
1265
|
+
* const numbers = [1, 2, 3, 4, 5]
|
|
1266
|
+
* const result = Array.splitAt(numbers, 3)
|
|
1267
|
+
* assert.deepStrictEqual(result, [[1, 2, 3], [4, 5]])
|
|
1268
|
+
*
|
|
854
1269
|
* @category splitting
|
|
855
1270
|
* @since 2.0.0
|
|
856
1271
|
*/
|
|
@@ -869,6 +1284,12 @@ const splitAt = exports.splitAt = /*#__PURE__*/(0, _Function.dual)(2, (self, n)
|
|
|
869
1284
|
* Splits a `NonEmptyReadonlyArray` into two segments, with the first segment containing a maximum of `n` elements.
|
|
870
1285
|
* The value of `n` must be `>= 1`.
|
|
871
1286
|
*
|
|
1287
|
+
* @example
|
|
1288
|
+
* import { Array } from "effect"
|
|
1289
|
+
*
|
|
1290
|
+
* const result = Array.splitNonEmptyAt(["a", "b", "c", "d", "e"], 3)
|
|
1291
|
+
* assert.deepStrictEqual(result, [["a", "b", "c"], ["d", "e"]])
|
|
1292
|
+
*
|
|
872
1293
|
* @category splitting
|
|
873
1294
|
* @since 2.0.0
|
|
874
1295
|
*/
|
|
@@ -879,6 +1300,13 @@ const splitNonEmptyAt = exports.splitNonEmptyAt = /*#__PURE__*/(0, _Function.dua
|
|
|
879
1300
|
/**
|
|
880
1301
|
* Splits this iterable into `n` equally sized arrays.
|
|
881
1302
|
*
|
|
1303
|
+
* @example
|
|
1304
|
+
* import { Array } from "effect"
|
|
1305
|
+
*
|
|
1306
|
+
* const numbers = [1, 2, 3, 4, 5, 6, 7, 8]
|
|
1307
|
+
* const result = Array.split(numbers, 3)
|
|
1308
|
+
* assert.deepStrictEqual(result, [[1, 2, 3], [4, 5, 6], [7, 8]])
|
|
1309
|
+
*
|
|
882
1310
|
* @since 2.0.0
|
|
883
1311
|
* @category splitting
|
|
884
1312
|
*/
|
|
@@ -890,11 +1318,27 @@ const split = exports.split = /*#__PURE__*/(0, _Function.dual)(2, (self, n) => {
|
|
|
890
1318
|
* Splits this iterable on the first element that matches this predicate.
|
|
891
1319
|
* Returns a tuple containing two arrays: the first one is before the match, and the second one is from the match onward.
|
|
892
1320
|
*
|
|
1321
|
+
* @example
|
|
1322
|
+
* import { Array } from "effect"
|
|
1323
|
+
*
|
|
1324
|
+
* const numbers = [1, 2, 3, 4, 5]
|
|
1325
|
+
* const result = Array.splitWhere(numbers, n => n > 3)
|
|
1326
|
+
* assert.deepStrictEqual(result, [[1, 2, 3], [4, 5]])
|
|
1327
|
+
*
|
|
893
1328
|
* @category splitting
|
|
894
1329
|
* @since 2.0.0
|
|
895
1330
|
*/
|
|
896
1331
|
const splitWhere = exports.splitWhere = /*#__PURE__*/(0, _Function.dual)(2, (self, predicate) => span(self, (a, i) => !predicate(a, i)));
|
|
897
1332
|
/**
|
|
1333
|
+
* Copies an array.
|
|
1334
|
+
*
|
|
1335
|
+
* @example
|
|
1336
|
+
* import { Array } from "effect"
|
|
1337
|
+
*
|
|
1338
|
+
* const numbers = [1, 2, 3]
|
|
1339
|
+
* const copy = Array.copy(numbers)
|
|
1340
|
+
* assert.deepStrictEqual(copy, [1, 2, 3])
|
|
1341
|
+
*
|
|
898
1342
|
* @since 2.0.0
|
|
899
1343
|
*/
|
|
900
1344
|
const copy = self => self.slice();
|
|
@@ -909,6 +1353,19 @@ const copy = self => self.slice();
|
|
|
909
1353
|
*
|
|
910
1354
|
* whenever `n` evenly divides the length of `self`.
|
|
911
1355
|
*
|
|
1356
|
+
* @example
|
|
1357
|
+
* import { Array } from "effect"
|
|
1358
|
+
*
|
|
1359
|
+
* const numbers = [1, 2, 3, 4, 5]
|
|
1360
|
+
* const result = Array.chunksOf(numbers, 2)
|
|
1361
|
+
* assert.deepStrictEqual(result, [[1, 2], [3, 4], [5]])
|
|
1362
|
+
*
|
|
1363
|
+
* // Explanation:
|
|
1364
|
+
* // The `chunksOf` function takes an array of numbers `[1, 2, 3, 4, 5]` and a number `2`.
|
|
1365
|
+
* // It splits the array into chunks of length 2. Since the array length is not evenly divisible by 2,
|
|
1366
|
+
* // the last chunk contains the remaining elements.
|
|
1367
|
+
* // The result is `[[1, 2], [3, 4], [5]]`.
|
|
1368
|
+
*
|
|
912
1369
|
* @category splitting
|
|
913
1370
|
* @since 2.0.0
|
|
914
1371
|
*/
|
|
@@ -923,6 +1380,12 @@ const chunksOf = exports.chunksOf = /*#__PURE__*/(0, _Function.dual)(2, (self, n
|
|
|
923
1380
|
/**
|
|
924
1381
|
* Group equal, consecutive elements of a `NonEmptyReadonlyArray` into `NonEmptyArray`s using the provided `isEquivalent` function.
|
|
925
1382
|
*
|
|
1383
|
+
* @example
|
|
1384
|
+
* import { Array } from "effect"
|
|
1385
|
+
*
|
|
1386
|
+
* const result = Array.groupWith(["a", "a", "b", "b", "b", "c", "a"], (x, y) => x === y)
|
|
1387
|
+
* assert.deepStrictEqual(result, [["a", "a"], ["b", "b", "b"], ["c"], ["a"]])
|
|
1388
|
+
*
|
|
926
1389
|
* @category grouping
|
|
927
1390
|
* @since 2.0.0
|
|
928
1391
|
*/
|
|
@@ -943,6 +1406,12 @@ const groupWith = exports.groupWith = /*#__PURE__*/(0, _Function.dual)(2, (self,
|
|
|
943
1406
|
/**
|
|
944
1407
|
* Group equal, consecutive elements of a `NonEmptyReadonlyArray` into `NonEmptyArray`s.
|
|
945
1408
|
*
|
|
1409
|
+
* @example
|
|
1410
|
+
* import { Array } from "effect"
|
|
1411
|
+
*
|
|
1412
|
+
* const result = Array.group([1, 1, 2, 2, 2, 3, 1])
|
|
1413
|
+
* assert.deepStrictEqual(result, [[1, 1], [2, 2, 2], [3], [1]])
|
|
1414
|
+
*
|
|
946
1415
|
* @category grouping
|
|
947
1416
|
* @since 2.0.0
|
|
948
1417
|
*/
|
|
@@ -951,6 +1420,20 @@ const group = exports.group = /*#__PURE__*/groupWith( /*#__PURE__*/Equal.equival
|
|
|
951
1420
|
* Splits an `Iterable` into sub-non-empty-arrays stored in an object, based on the result of calling a `string`-returning
|
|
952
1421
|
* function on each element, and grouping the results according to values returned
|
|
953
1422
|
*
|
|
1423
|
+
* @example
|
|
1424
|
+
* import { Array } from "effect"
|
|
1425
|
+
*
|
|
1426
|
+
* const people = [
|
|
1427
|
+
* { name: "Alice", group: "A" },
|
|
1428
|
+
* { name: "Bob", group: "B" },
|
|
1429
|
+
* { name: "Charlie", group: "A" }
|
|
1430
|
+
* ]
|
|
1431
|
+
* const result = Array.groupBy(people, person => person.group)
|
|
1432
|
+
* assert.deepStrictEqual(result, {
|
|
1433
|
+
* A: [{ name: "Alice", group: "A" }, { name: "Charlie", group: "A" }],
|
|
1434
|
+
* B: [{ name: "Bob", group: "B" }]
|
|
1435
|
+
* })
|
|
1436
|
+
*
|
|
954
1437
|
* @category grouping
|
|
955
1438
|
* @since 2.0.0
|
|
956
1439
|
*/
|
|
@@ -967,6 +1450,16 @@ const groupBy = exports.groupBy = /*#__PURE__*/(0, _Function.dual)(2, (self, f)
|
|
|
967
1450
|
return out;
|
|
968
1451
|
});
|
|
969
1452
|
/**
|
|
1453
|
+
* Calculates the union of two arrays using the provided equivalence relation.
|
|
1454
|
+
*
|
|
1455
|
+
* @example
|
|
1456
|
+
* import { Array } from "effect"
|
|
1457
|
+
*
|
|
1458
|
+
* const array1 = [1, 2]
|
|
1459
|
+
* const array2 = [2, 3]
|
|
1460
|
+
* const union = Array.unionWith(array1, array2, (a, b) => a === b)
|
|
1461
|
+
* assert.deepStrictEqual(union, [1, 2, 3])
|
|
1462
|
+
*
|
|
970
1463
|
* @since 2.0.0
|
|
971
1464
|
*/
|
|
972
1465
|
const unionWith = exports.unionWith = /*#__PURE__*/(0, _Function.dual)(3, (self, that, isEquivalent) => {
|
|
@@ -982,6 +1475,16 @@ const unionWith = exports.unionWith = /*#__PURE__*/(0, _Function.dual)(3, (self,
|
|
|
982
1475
|
return b;
|
|
983
1476
|
});
|
|
984
1477
|
/**
|
|
1478
|
+
* Creates a union of two arrays, removing duplicates.
|
|
1479
|
+
*
|
|
1480
|
+
* @example
|
|
1481
|
+
* import { Array } from "effect"
|
|
1482
|
+
*
|
|
1483
|
+
* const array1 = [1, 2]
|
|
1484
|
+
* const array2 = [2, 3]
|
|
1485
|
+
* const result = Array.union(array1, array2)
|
|
1486
|
+
* assert.deepStrictEqual(result, [1, 2, 3])
|
|
1487
|
+
*
|
|
985
1488
|
* @since 2.0.0
|
|
986
1489
|
*/
|
|
987
1490
|
const union = exports.union = /*#__PURE__*/(0, _Function.dual)(2, (self, that) => unionWith(self, that, _equivalence));
|
|
@@ -989,6 +1492,15 @@ const union = exports.union = /*#__PURE__*/(0, _Function.dual)(2, (self, that) =
|
|
|
989
1492
|
* Creates an `Array` of unique values that are included in all given `Iterable`s using the provided `isEquivalent` function.
|
|
990
1493
|
* The order and references of result values are determined by the first `Iterable`.
|
|
991
1494
|
*
|
|
1495
|
+
* @example
|
|
1496
|
+
* import { Array } from "effect"
|
|
1497
|
+
*
|
|
1498
|
+
* const array1 = [{ id: 1 }, { id: 2 }, { id: 3 }]
|
|
1499
|
+
* const array2 = [{ id: 3 }, { id: 4 }, { id: 1 }]
|
|
1500
|
+
* const isEquivalent = (a: { id: number }, b: { id: number }) => a.id === b.id
|
|
1501
|
+
* const result = Array.intersectionWith(isEquivalent)(array2)(array1)
|
|
1502
|
+
* assert.deepStrictEqual(result, [{ id: 1 }, { id: 3 }])
|
|
1503
|
+
*
|
|
992
1504
|
* @since 2.0.0
|
|
993
1505
|
*/
|
|
994
1506
|
const intersectionWith = isEquivalent => {
|
|
@@ -999,6 +1511,14 @@ const intersectionWith = isEquivalent => {
|
|
|
999
1511
|
* Creates an `Array` of unique values that are included in all given `Iterable`s.
|
|
1000
1512
|
* The order and references of result values are determined by the first `Iterable`.
|
|
1001
1513
|
*
|
|
1514
|
+
* @example
|
|
1515
|
+
* import { Array } from "effect"
|
|
1516
|
+
*
|
|
1517
|
+
* const array1 = [1, 2, 3]
|
|
1518
|
+
* const array2 = [3, 4, 1]
|
|
1519
|
+
* const result = Array.intersection(array1, array2)
|
|
1520
|
+
* assert.deepStrictEqual(result, [1, 3])
|
|
1521
|
+
*
|
|
1002
1522
|
* @since 2.0.0
|
|
1003
1523
|
*/
|
|
1004
1524
|
exports.intersectionWith = intersectionWith;
|
|
@@ -1007,6 +1527,14 @@ const intersection = exports.intersection = /*#__PURE__*/intersectionWith(_equiv
|
|
|
1007
1527
|
* Creates a `Array` of values not included in the other given `Iterable` using the provided `isEquivalent` function.
|
|
1008
1528
|
* The order and references of result values are determined by the first `Iterable`.
|
|
1009
1529
|
*
|
|
1530
|
+
* @example
|
|
1531
|
+
* import { Array } from "effect"
|
|
1532
|
+
*
|
|
1533
|
+
* const array1 = [1, 2, 3]
|
|
1534
|
+
* const array2 = [2, 3, 4]
|
|
1535
|
+
* const difference = Array.differenceWith<number>((a, b) => a === b)(array1, array2)
|
|
1536
|
+
* assert.deepStrictEqual(difference, [1])
|
|
1537
|
+
*
|
|
1010
1538
|
* @since 2.0.0
|
|
1011
1539
|
*/
|
|
1012
1540
|
const differenceWith = isEquivalent => {
|
|
@@ -1017,6 +1545,14 @@ const differenceWith = isEquivalent => {
|
|
|
1017
1545
|
* Creates a `Array` of values not included in the other given `Iterable`.
|
|
1018
1546
|
* The order and references of result values are determined by the first `Iterable`.
|
|
1019
1547
|
*
|
|
1548
|
+
* @example
|
|
1549
|
+
* import { Array } from "effect"
|
|
1550
|
+
*
|
|
1551
|
+
* const array1 = [1, 2, 3]
|
|
1552
|
+
* const array2 = [2, 3, 4]
|
|
1553
|
+
* const difference = Array.difference(array1, array2)
|
|
1554
|
+
* assert.deepStrictEqual(difference, [1])
|
|
1555
|
+
*
|
|
1020
1556
|
* @since 2.0.0
|
|
1021
1557
|
*/
|
|
1022
1558
|
exports.differenceWith = differenceWith;
|
|
@@ -1060,13 +1596,35 @@ const flatMap = exports.flatMap = /*#__PURE__*/(0, _Function.dual)(2, (self, f)
|
|
|
1060
1596
|
return out;
|
|
1061
1597
|
});
|
|
1062
1598
|
/**
|
|
1063
|
-
*
|
|
1599
|
+
* Combines multiple arrays into a single array by concatenating all elements
|
|
1600
|
+
* from each nested array. This function ensures that the structure of nested
|
|
1601
|
+
* arrays is collapsed into a single, flat array.
|
|
1602
|
+
*
|
|
1603
|
+
* @example
|
|
1604
|
+
* import { Array } from "effect";
|
|
1605
|
+
*
|
|
1606
|
+
* const nestedArrays = [[1, 2], [], [3, 4], [], [5, 6]]
|
|
1607
|
+
* const result = Array.flatten(nestedArrays)
|
|
1608
|
+
*
|
|
1609
|
+
* assert.deepStrictEqual(result, [1, 2, 3, 4, 5, 6]);
|
|
1064
1610
|
*
|
|
1065
1611
|
* @category sequencing
|
|
1066
1612
|
* @since 2.0.0
|
|
1067
1613
|
*/
|
|
1068
1614
|
const flatten = exports.flatten = /*#__PURE__*/flatMap(_Function.identity);
|
|
1069
1615
|
/**
|
|
1616
|
+
* Applies a function to each element of the `Iterable` and filters based on the result, keeping the transformed values where the function returns `Some`.
|
|
1617
|
+
* This method combines filtering and mapping functionalities, allowing transformations and filtering of elements based on a single function pass.
|
|
1618
|
+
*
|
|
1619
|
+
* @example
|
|
1620
|
+
* import { Array, Option } from "effect";
|
|
1621
|
+
*
|
|
1622
|
+
* const data = [1, 2, 3, 4, 5];
|
|
1623
|
+
* const evenSquares = (x: number) => x % 2 === 0 ? Option.some(x * x) : Option.none();
|
|
1624
|
+
* const result = Array.filterMap(data, evenSquares);
|
|
1625
|
+
*
|
|
1626
|
+
* assert.deepStrictEqual(result, [4, 16]);
|
|
1627
|
+
*
|
|
1070
1628
|
* @category filtering
|
|
1071
1629
|
* @since 2.0.0
|
|
1072
1630
|
*/
|
|
@@ -1082,7 +1640,18 @@ const filterMap = exports.filterMap = /*#__PURE__*/(0, _Function.dual)(2, (self,
|
|
|
1082
1640
|
return out;
|
|
1083
1641
|
});
|
|
1084
1642
|
/**
|
|
1085
|
-
*
|
|
1643
|
+
* Applies a function to each element of the array and filters based on the result, stopping when a condition is not met.
|
|
1644
|
+
* This method combines filtering and mapping in a single pass, and short-circuits, i.e., stops processing, as soon as the function returns `None`.
|
|
1645
|
+
* This is useful when you need to transform an array but only up to the point where a certain condition holds true.
|
|
1646
|
+
*
|
|
1647
|
+
* @example
|
|
1648
|
+
* import { Array, Option } from "effect";
|
|
1649
|
+
*
|
|
1650
|
+
* const data = [2, 4, 5];
|
|
1651
|
+
* const toSquareTillOdd = (x: number) => x % 2 === 0 ? Option.some(x * x) : Option.none();
|
|
1652
|
+
* const result = Array.filterMapWhile(data, toSquareTillOdd);
|
|
1653
|
+
*
|
|
1654
|
+
* assert.deepStrictEqual(result, [4, 16]);
|
|
1086
1655
|
*
|
|
1087
1656
|
* @category filtering
|
|
1088
1657
|
* @since 2.0.0
|
|
@@ -1102,6 +1671,25 @@ const filterMapWhile = exports.filterMapWhile = /*#__PURE__*/(0, _Function.dual)
|
|
|
1102
1671
|
return out;
|
|
1103
1672
|
});
|
|
1104
1673
|
/**
|
|
1674
|
+
* Applies a function to each element of the `Iterable`, categorizing the results into two separate arrays.
|
|
1675
|
+
* This function is particularly useful for operations where each element can result in two possible types,
|
|
1676
|
+
* and you want to separate these types into different collections. For instance, separating validation results
|
|
1677
|
+
* into successes and failures.
|
|
1678
|
+
*
|
|
1679
|
+
* @example
|
|
1680
|
+
* import { Array, Either } from "effect";
|
|
1681
|
+
*
|
|
1682
|
+
* const data = [1, 2, 3, 4, 5]
|
|
1683
|
+
* const isEven = (x: number) => x % 2 === 0
|
|
1684
|
+
* const partitioned = Array.partitionMap(data, x =>
|
|
1685
|
+
* isEven(x) ? Either.right(x) : Either.left(x)
|
|
1686
|
+
* )
|
|
1687
|
+
*
|
|
1688
|
+
* assert.deepStrictEqual(partitioned, [
|
|
1689
|
+
* [1, 3, 5],
|
|
1690
|
+
* [2, 4]
|
|
1691
|
+
* ])
|
|
1692
|
+
*
|
|
1105
1693
|
* @category filtering
|
|
1106
1694
|
* @since 2.0.0
|
|
1107
1695
|
*/
|
|
@@ -1216,21 +1804,58 @@ const partition = exports.partition = /*#__PURE__*/(0, _Function.dual)(2, (self,
|
|
|
1216
1804
|
return [left, right];
|
|
1217
1805
|
});
|
|
1218
1806
|
/**
|
|
1807
|
+
* Separates an `Iterable` into two arrays based on a predicate.
|
|
1808
|
+
*
|
|
1809
|
+
* @example
|
|
1810
|
+
* import { Array } from "effect"
|
|
1811
|
+
*
|
|
1812
|
+
* const numbers = [1, 2, 3, 4]
|
|
1813
|
+
* const result = Array.partition(numbers, n => n % 2 === 0)
|
|
1814
|
+
* assert.deepStrictEqual(result, [[1, 3], [2, 4]])
|
|
1815
|
+
*
|
|
1219
1816
|
* @category filtering
|
|
1220
1817
|
* @since 2.0.0
|
|
1221
1818
|
*/
|
|
1222
1819
|
const separate = exports.separate = /*#__PURE__*/partitionMap(_Function.identity);
|
|
1223
1820
|
/**
|
|
1821
|
+
* Reduces an array from the left.
|
|
1822
|
+
*
|
|
1823
|
+
* @example
|
|
1824
|
+
* import { Array } from "effect"
|
|
1825
|
+
*
|
|
1826
|
+
* const numbers = [1, 2, 3]
|
|
1827
|
+
* const result = Array.reduce(numbers, 0, (acc, n) => acc + n)
|
|
1828
|
+
* assert.deepStrictEqual(result, 6)
|
|
1829
|
+
*
|
|
1224
1830
|
* @category folding
|
|
1225
1831
|
* @since 2.0.0
|
|
1226
1832
|
*/
|
|
1227
1833
|
const reduce = exports.reduce = /*#__PURE__*/(0, _Function.dual)(3, (self, b, f) => fromIterable(self).reduce((b, a, i) => f(b, a, i), b));
|
|
1228
1834
|
/**
|
|
1835
|
+
* Reduces an array from the right.
|
|
1836
|
+
*
|
|
1837
|
+
* @example
|
|
1838
|
+
* import { Array } from "effect"
|
|
1839
|
+
*
|
|
1840
|
+
* const numbers = [1, 2, 3]
|
|
1841
|
+
* const result = Array.reduceRight(numbers, 0, (acc, n) => acc + n)
|
|
1842
|
+
* assert.deepStrictEqual(result, 6)
|
|
1843
|
+
*
|
|
1229
1844
|
* @category folding
|
|
1230
1845
|
* @since 2.0.0
|
|
1231
1846
|
*/
|
|
1232
1847
|
const reduceRight = exports.reduceRight = /*#__PURE__*/(0, _Function.dual)(3, (self, b, f) => fromIterable(self).reduceRight((b, a, i) => f(b, a, i), b));
|
|
1233
1848
|
/**
|
|
1849
|
+
* Lifts a predicate into an array.
|
|
1850
|
+
*
|
|
1851
|
+
* @example
|
|
1852
|
+
* import { Array } from "effect"
|
|
1853
|
+
*
|
|
1854
|
+
* const isEven = (n: number) => n % 2 === 0
|
|
1855
|
+
* const to = Array.liftPredicate(isEven)
|
|
1856
|
+
* assert.deepStrictEqual(to(1), [])
|
|
1857
|
+
* assert.deepStrictEqual(to(2), [2])
|
|
1858
|
+
*
|
|
1234
1859
|
* @category lifting
|
|
1235
1860
|
* @since 2.0.0
|
|
1236
1861
|
*/
|
|
@@ -1254,12 +1879,49 @@ const fromNullable = a => a == null ? empty() : [a];
|
|
|
1254
1879
|
exports.fromNullable = fromNullable;
|
|
1255
1880
|
const liftNullable = f => (...a) => fromNullable(f(...a));
|
|
1256
1881
|
/**
|
|
1882
|
+
* Maps over an array and flattens the result, removing null and undefined values.
|
|
1883
|
+
*
|
|
1884
|
+
* @example
|
|
1885
|
+
* import { Array } from "effect"
|
|
1886
|
+
*
|
|
1887
|
+
* const numbers = [1, 2, 3]
|
|
1888
|
+
* const result = Array.flatMapNullable(numbers, n => (n % 2 === 0 ? null : n))
|
|
1889
|
+
* assert.deepStrictEqual(result, [1, 3])
|
|
1890
|
+
*
|
|
1891
|
+
* // Explanation:
|
|
1892
|
+
* // The array of numbers [1, 2, 3] is mapped with a function that returns null for even numbers
|
|
1893
|
+
* // and the number itself for odd numbers. The resulting array [1, null, 3] is then flattened
|
|
1894
|
+
* // to remove null values, resulting in [1, 3].
|
|
1895
|
+
*
|
|
1257
1896
|
* @category sequencing
|
|
1258
1897
|
* @since 2.0.0
|
|
1259
1898
|
*/
|
|
1260
1899
|
exports.liftNullable = liftNullable;
|
|
1261
|
-
const flatMapNullable = exports.flatMapNullable = /*#__PURE__*/(0, _Function.dual)(2, (self, f) =>
|
|
1900
|
+
const flatMapNullable = exports.flatMapNullable = /*#__PURE__*/(0, _Function.dual)(2, (self, f) => flatMap(self, a => fromNullable(f(a))));
|
|
1262
1901
|
/**
|
|
1902
|
+
* Lifts a function that returns an `Either` into a function that returns an array.
|
|
1903
|
+
* If the `Either` is a left, it returns an empty array.
|
|
1904
|
+
* If the `Either` is a right, it returns an array with the right value.
|
|
1905
|
+
*
|
|
1906
|
+
* @example
|
|
1907
|
+
* import { Array, Either } from "effect"
|
|
1908
|
+
*
|
|
1909
|
+
* const parseNumber = (s: string): Either.Either<number, Error> =>
|
|
1910
|
+
* isNaN(Number(s)) ? Either.left(new Error("Not a number")) : Either.right(Number(s))
|
|
1911
|
+
*
|
|
1912
|
+
* const liftedParseNumber = Array.liftEither(parseNumber)
|
|
1913
|
+
*
|
|
1914
|
+
* const result1 = liftedParseNumber("42")
|
|
1915
|
+
* assert.deepStrictEqual(result1, [42])
|
|
1916
|
+
*
|
|
1917
|
+
* const result2 = liftedParseNumber("not a number")
|
|
1918
|
+
* assert.deepStrictEqual(result2, [])
|
|
1919
|
+
*
|
|
1920
|
+
* // Explanation:
|
|
1921
|
+
* // The function parseNumber is lifted to return an array.
|
|
1922
|
+
* // When parsing "42", it returns an Either.left with the number 42, resulting in [42].
|
|
1923
|
+
* // When parsing "not a number", it returns an Either.right with an error, resulting in an empty array [].
|
|
1924
|
+
*
|
|
1263
1925
|
* @category lifting
|
|
1264
1926
|
* @since 2.0.0
|
|
1265
1927
|
*/
|
|
@@ -1283,14 +1945,45 @@ const every = exports.every = /*#__PURE__*/(0, _Function.dual)(2, (self, refinem
|
|
|
1283
1945
|
*/
|
|
1284
1946
|
const some = exports.some = /*#__PURE__*/(0, _Function.dual)(2, (self, predicate) => self.some(predicate));
|
|
1285
1947
|
/**
|
|
1948
|
+
* Extends an array with a function that maps each subarray to a value.
|
|
1949
|
+
*
|
|
1950
|
+
* @example
|
|
1951
|
+
* import { Array } from "effect"
|
|
1952
|
+
*
|
|
1953
|
+
* const numbers = [1, 2, 3]
|
|
1954
|
+
* const result = Array.extend(numbers, as => as.length)
|
|
1955
|
+
* assert.deepStrictEqual(result, [3, 2, 1])
|
|
1956
|
+
*
|
|
1957
|
+
* // Explanation:
|
|
1958
|
+
* // The function maps each subarray starting from each element to its length.
|
|
1959
|
+
* // The subarrays are: [1, 2, 3], [2, 3], [3].
|
|
1960
|
+
* // The lengths are: 3, 2, 1.
|
|
1961
|
+
* // Therefore, the result is [3, 2, 1].
|
|
1962
|
+
*
|
|
1286
1963
|
* @since 2.0.0
|
|
1287
1964
|
*/
|
|
1288
1965
|
const extend = exports.extend = /*#__PURE__*/(0, _Function.dual)(2, (self, f) => self.map((_, i, as) => f(as.slice(i))));
|
|
1289
1966
|
/**
|
|
1967
|
+
* Finds the minimum element in an array based on a comparator.
|
|
1968
|
+
*
|
|
1969
|
+
* @example
|
|
1970
|
+
* import { Array, Order } from "effect"
|
|
1971
|
+
*
|
|
1972
|
+
* const min = Array.min([3, 1, 2], Order.number)
|
|
1973
|
+
* assert.deepStrictEqual(min, 1)
|
|
1974
|
+
*
|
|
1290
1975
|
* @since 2.0.0
|
|
1291
1976
|
*/
|
|
1292
1977
|
const min = exports.min = /*#__PURE__*/(0, _Function.dual)(2, (self, O) => self.reduce(Order.min(O)));
|
|
1293
1978
|
/**
|
|
1979
|
+
* Finds the maximum element in an array based on a comparator.
|
|
1980
|
+
*
|
|
1981
|
+
* @example
|
|
1982
|
+
* import { Array, Order } from "effect"
|
|
1983
|
+
*
|
|
1984
|
+
* const max = Array.max([3, 1, 2], Order.number)
|
|
1985
|
+
* assert.deepStrictEqual(max, 3)
|
|
1986
|
+
*
|
|
1294
1987
|
* @since 2.0.0
|
|
1295
1988
|
*/
|
|
1296
1989
|
const max = exports.max = /*#__PURE__*/(0, _Function.dual)(2, (self, O) => self.reduce(Order.max(O)));
|
|
@@ -1321,12 +2014,28 @@ const unfold = (b, f) => {
|
|
|
1321
2014
|
exports.unfold = unfold;
|
|
1322
2015
|
const getOrder = exports.getOrder = Order.array;
|
|
1323
2016
|
/**
|
|
2017
|
+
* Creates an equivalence relation for arrays.
|
|
2018
|
+
*
|
|
2019
|
+
* @example
|
|
2020
|
+
* import { Array } from "effect"
|
|
2021
|
+
*
|
|
2022
|
+
* const numbers1 = [1, 2, 3]
|
|
2023
|
+
* const numbers2 = [1, 2, 3]
|
|
2024
|
+
* const eq = Array.getEquivalence<number>((a, b) => a === b)
|
|
2025
|
+
* assert.deepStrictEqual(eq(numbers1, numbers2), true)
|
|
2026
|
+
*
|
|
1324
2027
|
* @category instances
|
|
1325
2028
|
* @since 2.0.0
|
|
1326
2029
|
*/
|
|
1327
2030
|
const getEquivalence = exports.getEquivalence = Equivalence.array;
|
|
1328
2031
|
/**
|
|
1329
|
-
*
|
|
2032
|
+
* Performs a side-effect for each element of the `Iterable`.
|
|
2033
|
+
*
|
|
2034
|
+
* @example
|
|
2035
|
+
* import { Array } from "effect"
|
|
2036
|
+
*
|
|
2037
|
+
* const numbers = [1, 2, 3]
|
|
2038
|
+
* Array.forEach(numbers, n => console.log(n)) // 1, 2, 3
|
|
1330
2039
|
*
|
|
1331
2040
|
* @since 2.0.0
|
|
1332
2041
|
*/
|
|
@@ -1335,6 +2044,13 @@ const forEach = exports.forEach = /*#__PURE__*/(0, _Function.dual)(2, (self, f)
|
|
|
1335
2044
|
* Remove duplicates from an `Iterable` using the provided `isEquivalent` function,
|
|
1336
2045
|
* preserving the order of the first occurrence of each element.
|
|
1337
2046
|
*
|
|
2047
|
+
* @example
|
|
2048
|
+
* import { Array } from "effect"
|
|
2049
|
+
*
|
|
2050
|
+
* const numbers = [1, 2, 2, 3, 3, 3]
|
|
2051
|
+
* const unique = Array.dedupeWith(numbers, (a, b) => a === b)
|
|
2052
|
+
* assert.deepStrictEqual(unique, [1, 2, 3])
|
|
2053
|
+
*
|
|
1338
2054
|
* @since 2.0.0
|
|
1339
2055
|
*/
|
|
1340
2056
|
const dedupeWith = exports.dedupeWith = /*#__PURE__*/(0, _Function.dual)(2, (self, isEquivalent) => {
|
|
@@ -1361,6 +2077,13 @@ const dedupe = self => dedupeWith(self, Equal.equivalence());
|
|
|
1361
2077
|
/**
|
|
1362
2078
|
* Deduplicates adjacent elements that are identical using the provided `isEquivalent` function.
|
|
1363
2079
|
*
|
|
2080
|
+
* @example
|
|
2081
|
+
* import { Array } from "effect"
|
|
2082
|
+
*
|
|
2083
|
+
* const numbers = [1, 1, 2, 2, 3, 3]
|
|
2084
|
+
* const unique = Array.dedupeAdjacentWith(numbers, (a, b) => a === b)
|
|
2085
|
+
* assert.deepStrictEqual(unique, [1, 2, 3])
|
|
2086
|
+
*
|
|
1364
2087
|
* @since 2.0.0
|
|
1365
2088
|
*/
|
|
1366
2089
|
exports.dedupe = dedupe;
|
|
@@ -1378,12 +2101,26 @@ const dedupeAdjacentWith = exports.dedupeAdjacentWith = /*#__PURE__*/(0, _Functi
|
|
|
1378
2101
|
/**
|
|
1379
2102
|
* Deduplicates adjacent elements that are identical.
|
|
1380
2103
|
*
|
|
2104
|
+
* @example
|
|
2105
|
+
* import { Array } from "effect"
|
|
2106
|
+
*
|
|
2107
|
+
* const numbers = [1, 1, 2, 2, 3, 3]
|
|
2108
|
+
* const unique = Array.dedupeAdjacent(numbers)
|
|
2109
|
+
* assert.deepStrictEqual(unique, [1, 2, 3])
|
|
2110
|
+
*
|
|
1381
2111
|
* @since 2.0.0
|
|
1382
2112
|
*/
|
|
1383
2113
|
const dedupeAdjacent = exports.dedupeAdjacent = /*#__PURE__*/dedupeAdjacentWith( /*#__PURE__*/Equal.equivalence());
|
|
1384
2114
|
/**
|
|
1385
2115
|
* Joins the elements together with "sep" in the middle.
|
|
1386
2116
|
*
|
|
2117
|
+
* @example
|
|
2118
|
+
* import { Array } from "effect"
|
|
2119
|
+
*
|
|
2120
|
+
* const strings = ["a", "b", "c"]
|
|
2121
|
+
* const joined = Array.join(strings, "-")
|
|
2122
|
+
* assert.deepStrictEqual(joined, "a-b-c")
|
|
2123
|
+
*
|
|
1387
2124
|
* @since 2.0.0
|
|
1388
2125
|
* @category folding
|
|
1389
2126
|
*/
|
|
@@ -1391,6 +2128,13 @@ const join = exports.join = /*#__PURE__*/(0, _Function.dual)(2, (self, sep) => f
|
|
|
1391
2128
|
/**
|
|
1392
2129
|
* Statefully maps over the chunk, producing new elements of type `B`.
|
|
1393
2130
|
*
|
|
2131
|
+
* @example
|
|
2132
|
+
* import { Array } from "effect"
|
|
2133
|
+
*
|
|
2134
|
+
* const numbers = [1, 2, 3]
|
|
2135
|
+
* const result = Array.mapAccum(numbers, 0, (acc, n) => [acc + n, acc + n])
|
|
2136
|
+
* assert.deepStrictEqual(result, [6, [1, 3, 6]])
|
|
2137
|
+
*
|
|
1394
2138
|
* @since 2.0.0
|
|
1395
2139
|
* @category folding
|
|
1396
2140
|
*/
|
|
@@ -1409,6 +2153,14 @@ const mapAccum = exports.mapAccum = /*#__PURE__*/(0, _Function.dual)(3, (self, s
|
|
|
1409
2153
|
/**
|
|
1410
2154
|
* Zips this chunk crosswise with the specified chunk using the specified combiner.
|
|
1411
2155
|
*
|
|
2156
|
+
* @example
|
|
2157
|
+
* import { Array } from "effect"
|
|
2158
|
+
*
|
|
2159
|
+
* const array1 = [1, 2]
|
|
2160
|
+
* const array2 = ["a", "b"]
|
|
2161
|
+
* const product = Array.cartesianWith(array1, array2, (a, b) => `${a}-${b}`)
|
|
2162
|
+
* assert.deepStrictEqual(product, ["1-a", "1-b", "2-a", "2-b"])
|
|
2163
|
+
*
|
|
1412
2164
|
* @since 2.0.0
|
|
1413
2165
|
* @category elements
|
|
1414
2166
|
*/
|
|
@@ -1416,6 +2168,14 @@ const cartesianWith = exports.cartesianWith = /*#__PURE__*/(0, _Function.dual)(3
|
|
|
1416
2168
|
/**
|
|
1417
2169
|
* Zips this chunk crosswise with the specified chunk.
|
|
1418
2170
|
*
|
|
2171
|
+
* @example
|
|
2172
|
+
* import { Array } from "effect"
|
|
2173
|
+
*
|
|
2174
|
+
* const array1 = [1, 2]
|
|
2175
|
+
* const array2 = ["a", "b"]
|
|
2176
|
+
* const product = Array.cartesian(array1, array2)
|
|
2177
|
+
* assert.deepStrictEqual(product, [[1, "a"], [1, "b"], [2, "a"], [2, "b"]])
|
|
2178
|
+
*
|
|
1419
2179
|
* @since 2.0.0
|
|
1420
2180
|
* @category elements
|
|
1421
2181
|
*/
|