-
Notifications
You must be signed in to change notification settings - Fork 7
/
array.ts
1567 lines (1476 loc) · 38.4 KB
/
array.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* This file contains a collection of utilities and
* algebraic structure implementations for ReadonlyArray
* in JavaScript.
*
* @module Array
* @since 2.0.0
*/
import type { $, AnySub, Intersect, Kind, Out } from "./kind.ts";
import type { Applicable } from "./applicable.ts";
import type { Combinable } from "./combinable.ts";
import type { Comparable } from "./comparable.ts";
import type { Either } from "./either.ts";
import type { Filterable } from "./filterable.ts";
import type { Bind, Flatmappable, Tap } from "./flatmappable.ts";
import type { Initializable } from "./initializable.ts";
import type { BindTo, Mappable } from "./mappable.ts";
import type { Option } from "./option.ts";
import type { Pair } from "./pair.ts";
import type { Foldable } from "./foldable.ts";
import type { Showable } from "./showable.ts";
import type { Sortable } from "./sortable.ts";
import type { Traversable } from "./traversable.ts";
import type { Wrappable } from "./wrappable.ts";
import { createBind, createTap } from "./flatmappable.ts";
import { createBindTo } from "./mappable.ts";
import { pair } from "./pair.ts";
import { isRight } from "./either.ts";
import { fromCompare } from "./comparable.ts";
import { fromSort, sign } from "./sortable.ts";
import { isSome, none, some } from "./option.ts";
import { identity, pipe } from "./fn.ts";
/**
* This type can be used as a placeholder for an array of any type.
*
* @since 2.0.0
*/
// deno-lint-ignore no-explicit-any
export type AnyArray = ReadonlyArray<any>;
/**
* This type alias unwraps the inner type of a ReadonlyArray.
*
* @since 2.0.0
*/
export type TypeOf<T> = T extends ReadonlyArray<infer A> ? A : never;
/**
* This type alias represents a ReadonlyArray conuaining
* at least one value at the head.
*
* @since 2.0.0
*/
export type NonEmptyArray<A> = readonly [A, ...A[]];
/**
* This type can be used as a placeholder for a non-init array of any type.
*
* @since 2.0.0
*/
// deno-lint-ignore no-explicit-any
export type AnyNonEmptyArray = NonEmptyArray<any>;
/**
* Specifies ReadonlyArray as a Higher Kinded Type, with covariant
* parameter A corresponding to the 0th index of any substitutions.
*
* @since 2.0.0
*/
export interface KindReadonlyArray extends Kind {
readonly kind: ReadonlyArray<Out<this, 0>>;
}
/**
* *UNSAFE* This operation creates a new array from ua with the value a inserted
* at the given index. The insertion index must be tested as in bounds before
* calling this function. This function is intended for internal use only and
* thus has no api guaruntees.
*
* @since 2.0.0
*/
export function _unsafeInsertAt<A>(
index: number,
a: A,
ua: ReadonlyArray<A>,
): ReadonlyArray<A> {
const result = ua.slice();
result.splice(index, 0, a);
return result;
}
/**
* *UNSAFE* This operation creates a new array from ua with the value a changed
* at the given index. The insertion index must be tested as in bounds before
* calling this function. This function is intended for internal use only and
* thus has no api guaruntees.
*
* @since 2.0.0
*/
export function _unsafeUpdateAt<A>(
index: number,
a: A,
ua: ReadonlyArray<A>,
): ReadonlyArray<A> {
if (ua[index] === a) {
return ua;
} else {
const result = ua.slice();
result[index] = a;
return result;
}
}
/**
* *UNSAFE* This operation creates a new array from ua with the value deleted
* at the given index. The deletiong index must be tested as in bounds before
* calling this function. This function is intended for internal use only and
* thus has no api guaruntees.
*
* @since 2.0.0
*/
export function _unsafeDeleteAt<A>(
index: number,
ua: ReadonlyArray<A>,
): ReadonlyArray<A> {
const result = ua.slice();
result.splice(index, 1);
return result;
}
/**
* *UNSAFE* This operation mutates a standard Array<A> by pushing onto it.
* This function is intended for internal use only and thus has no api
* guaruntees.
*
* @since 2.0.0
*/
export function _unsafeAppend<A>(
last: A,
): (ua: Array<A>) => Array<A> {
return (ua) => {
ua.push(last);
return ua;
};
}
/**
* *UNSAFE* This operation mutates a standard Array<A> by pushing onto it.
* This function is intended for internal use only and thus has no api
* guaruntees.
*
* @since 2.0.0
*/
export function _unsafePush<A>(ua: Array<A>, a: A): Array<A> {
ua.push(a);
return ua;
}
/**
* *UNSAFE* This operation muuates a standard Array<A> by unshifting onto it.
* This function is intended for internal use only and thus has no api
* guaruntees.
*
* @since 2.0.0
*/
export function _unsafePrepend<A>(
head: A,
): (ua: Array<A>) => Array<A> {
return (ua) => {
ua.unshift(head);
return ua;
};
}
/**
* *UNSAFE* This operation mutates a standard Array<A> by adding all elements
* from a second array to it. This function is intended for internal use only
* and thus has no api guaruntees.
*
* @since 2.0.0
*/
export function _unsafeJoin<A>(
into: Array<A>,
from: ReadonlyArray<A>,
): Array<A> {
const length = from.length;
let index = -1;
while (++index < length) {
into.push(from[index]);
}
return into;
}
/**
* Given an index and a ReadonlyArray<A>, return true if the index is valid
* for the given array. This tests whether index is between 0 and arr.length
* inclusive.
*
* @example
* ```ts
* import * as A from "./array.ts";
*
* const arr = A.wrap(1);
*
* const result1 = A.isOutOfBounds(0, arr); // false
* const result2 = A.isOutOfBounds(-1, arr); // true
* const result3 = A.isOutOfBounds(10, arr); // true
* ```
*
* @since 2.0.0
*/
export function isOutOfBounds<A>(index: number, ua: ReadonlyArray<A>): boolean {
return index < 0 || index >= ua.length;
}
/**
* This predicate over ReadonlyArray<A> returns true when called with an
* default array, otherwise it returns false.
*
* @example
* ```ts
* import * as A from "./array.ts";
*
* const arr1 = A.init<number>();
* const arr2 = A.wrap(1);
*
* const result1 = A.isEmpty(arr1); // true
* const result2 = A.isEmpty(arr2); // false
* ```
*
* @since 2.0.0
*/
export function isEmpty<A>(ua: ReadonlyArray<A>): boolean {
return ua.length === 0;
}
/**
* A Refinement<ReadonlyArray<A>, NonEmptyArray<A>>, returning true if
* called with an array that has at least one item.
*
* @example
* ```ts
* import * as A from "./array.ts";
*
* const arr1 = [1]
* const arr2 = A.init<number>();
*
* const result1 = A.isNonEmpty(arr1);
* // true and arr1 has type NonEmptyArray<number>
* const result2 = A.isNonEmpty(arr2);
* // false
* ```
*
* @since 2.0.0
*/
export function isNonEmpty<A>(a: ReadonlyArray<A>): a is NonEmptyArray<A> {
return a.length > 0;
}
/**
* Create a NonEmptyArray<A> from a variadic number of arguments.
*
* @example
* ```ts
* import * as A from "./array.ts";
*
* const result = A.array(1, 2, 3, 4); // [1, 2, 3, 4]
* ```
*
* @since 2.0.0
*/
export function array<A>(...a: NonEmptyArray<A>): NonEmptyArray<A> {
return a;
}
/**
* Create a range of numbers with count values, starting at start (default 0)
* and stepping by step (default 1).
*
* @example
* ```ts
* import * as A from "./array.ts";
*
* const result1 = A.range(3); // [0, 1, 2]
* const result2 = A.range(3, 1); // [1, 2, 3]
* const result3 = A.range(3, -1, 0.1); // [-1, -0.9, -0.8]
* const result4 = A.range(2.5); // [0, 1]
* const result5 = A.range(-1); // []
* ```
*
* @since 2.0.0
*/
export function range(
count: number,
start = 0,
step = 1,
): ReadonlyArray<number> {
const length = Math.max(0, Math.floor(count));
const result = new Array(length);
let index = -1;
let value = start;
while (++index < length) {
result[index] = value;
value += step;
}
return result;
}
/**
* Create an init array of type A (defaulting to never).
*
* @example
* ```ts
* import * as A from "./array.ts";
*
* const result = A.init<number>(); // ReadonlyArray<number>
* ```
*
* @since 2.0.0
*/
export function init<A = never>(): ReadonlyArray<A> {
return [];
}
/**
* Create a NonEmptyArray<A> conuaining the value A.
*
* @example
* ```ts
* import * as A from "./array.ts";
*
* const result = A.wrap(1); // [1] of type NonEmptyArray<number>
* ```
*
* @since 2.0.0
*/
export function wrap<A>(a: A): NonEmptyArray<A> {
return [a];
}
/**
* Given two arrays first and second, if first is default the return second,
* otherwise return first.
*
* @example
* ```ts
* import * as A from "./array.ts";
* import { pipe } from "./fn.ts";
*
* const result1 = pipe(
* A.init<number>(),
* A.alt(A.wrap(1)),
* ); // [1]
* const result2 = pipe(
* A.array(1, 2, 3),
* A.alt(A.array(3, 2, 1)),
* ); // [1, 2, 3]
* ```
*
* @since 2.0.0
*/
export function alt<A>(
second: ReadonlyArray<A>,
): (first: ReadonlyArray<A>) => ReadonlyArray<A> {
return (first) => isEmpty(first) ? second : first;
}
/**
* Applicable the function fai: (A, index) => I to every element in the array ua.
*
* @example
* ```ts
* import * as A from "./array.ts";
* import { pipe } from "./fn.ts";
*
* const result = pipe(
* A.array("Hello", "World"),
* A.map(s => s.length),
* ); // [5, 5]
* ```
*
* @since 2.0.0
*/
export function map<A, I>(
fai: (a: A, i: number) => I,
): (ua: ReadonlyArray<A>) => ReadonlyArray<I> {
return (ua) => {
let index = -1;
const length = ua.length;
const result = new Array(length);
while (++index < length) {
result[index] = fai(ua[index], index);
}
return result;
};
}
/**
* Reduce an array from left to right, accumulating into a type O via the
* function foao: (O, A, index) => O and an initial value O.
*
* @example
* ```ts
* import * as A from "./array.ts";
* import { pipe } from "./fn.ts";
*
* const result = pipe(
* A.range(5, 1),
* A.fold((sum, value, index) => sum + value + index, 0),
* );
* // 0 + 0 + 0 = 0
* // 0 + 1 + 1 = 2
* // 2 + 2 + 2 = 6
* // 6 + 3 + 3 = 12
* // 12 + 4 + 4 = 20
* // 20
* ```
*
* @since 2.0.0
*/
export function fold<A, O>(
foao: (o: O, a: A, i: number) => O,
o: O,
): (ua: ReadonlyArray<A>) => O {
return (ua) => {
let result = o;
let index = -1;
const length = ua.length;
while (++index < length) {
result = foao(result, ua[index], index);
}
return result;
};
}
/**
* Given two arrays first and second, join them into a new array effectively
* doing [...first, ...second].
*
* @example
* ```ts
* import * as A from "./array.ts";
* import { pipe } from "./fn.ts";
*
* const result = pipe(
* A.range(3, 1),
* A.combine(A.range(3, 3, -1))
* ); // [1, 2, 3, 3, 2, 1]
* ```
*
* @since 2.0.0
*/
export function combine<A>(
second: ReadonlyArray<A>,
): (first: ReadonlyArray<A>) => ReadonlyArray<A> {
return (first) => {
if (isEmpty(second)) {
return first;
} else if (isEmpty(first)) {
return second;
}
const firstLength = first.length;
const length = firstLength + second.length;
const result = Array(length);
let index = -1;
while (++index < firstLength) {
result[index] = first[index];
}
index--;
while (++index < length) {
result[index] = second[index - firstLength];
}
return result;
};
}
/**
* Given an array of arrays, flatten all inner arrays into a single
* external array.
*
* @example
* ```ts
* import * as A from "./array.ts";
*
* const result = A.join(A.array(
* A.range(3),
* A.range(2),
* A.range(1),
* )); // [0, 1, 2, 0, 1, 0]
* ```
*
* @since 2.0.0
*/
export function join<A>(
uaa: ReadonlyArray<ReadonlyArray<A>>,
): ReadonlyArray<A> {
let index = -1;
const length = uaa.length;
const result = new Array<A>();
while (++index < length) {
const ua = uaa[index];
let _index = -1;
const _length = ua.length;
while (++_index < _length) {
result.push(ua[_index]);
}
}
return result;
}
/**
* Given a function A -> ReadonlyArray<I> and a ReadonlyArray<A> apply the
* function to every value in the array and combine all results, returning a
* ReadonlyArray<I>.
*
* @example
* ```ts
* import * as A from "./array.ts";
* import { pipe } from "./fn.ts";
*
* const result = pipe(
* A.range(3, 1, 3), // [1, 4, 7]
* A.flatmap(n => [n, n + 1, n + 2]), // ie. 1 -> [1, 2, 3]
* ); // [1, 2, 3, 4, 5, 6, 7, 8, 9]
* ```
*
* @since 2.0.0
*/
export function flatmap<A, I>(
fati: (a: A, index: number) => ReadonlyArray<I>,
): (ua: ReadonlyArray<A>) => ReadonlyArray<I> {
return (ua) => {
const length = ua.length;
const result = new Array<I>();
let index = -1;
while (++index < length) {
const chained = fati(ua[index], index);
// Mutates result
// This is okay because result is a local mutable variable
// in a tight loop
_unsafeJoin(result, chained);
}
return result;
};
}
/**
* Given an array of functions ReadonlyArray<A -> I> and a ReadonlyArray<A>
* apply every function in the function array to every value in the
* ReadonlyArray<A>. This implementation loops first over the functions, and then
* over the values, so the order of results will be [fn1(val1), fn2(val1),
* fn3(val1), ..., fn1(val2), fn2(val2), ... fn1(valN), ... fnN(valN)].
*
* @example
* ```ts
* import * as A from "./array.ts";
* import { pipe } from "./fn.ts";
*
* const result = pipe(
* A.wrap((n: number) => n + 1),
* A.apply(A.array(1, 2, 3)),
* ); // [2, 3, 4]
* ```
*
* @since 2.0.0
*/
export function apply<A>(
ua: ReadonlyArray<A>,
): <I>(ufai: ReadonlyArray<(a: A, index: number) => I>) => ReadonlyArray<I> {
return <I>(
ufai: ReadonlyArray<(a: A, index: number) => I>,
): ReadonlyArray<I> => {
const fnlength = ufai.length;
const vallength = ua.length;
const result = new Array<I>(fnlength * vallength);
let fnindex = -1;
while (++fnindex < fnlength) {
let valindex = -1;
while (++valindex < vallength) {
const index = (vallength * fnindex) + valindex;
const value = ua[valindex];
const fn = ufai[fnindex];
result[index] = fn(value, valindex);
}
}
return result;
};
}
/**
* Given a Predicate or Refinement, apply the predicate or refinement to
* every value in an array, removing (and refining) the elements that
* the predicate or refinement return false for.
*
* @example
* ```ts
* import * as A from "./array.ts";
* import { pipe } from "./fn.ts";
*
* const result = pipe(
* A.array(1, 2, 3, 4, 5, 6),
* A.filter(n => n % 2 === 0),
* ); // [2, 4, 6]
* ```
*
* @since 2.0.0
*/
export function filter<A, B extends A>(
refinement: (a: A, index: number) => a is B,
): (ua: ReadonlyArray<A>) => ReadonlyArray<B>;
export function filter<A>(
predicate: (a: A, index: number) => boolean,
): (ua: ReadonlyArray<A>) => ReadonlyArray<A>;
export function filter<A>(
predicate: (a: A, index: number) => boolean,
): (ua: ReadonlyArray<A>) => ReadonlyArray<A> {
return (ua) => {
let index = -1;
let resultIndex = 0;
const length = ua.length;
const result = [];
while (++index < length) {
const value = ua[index];
if (predicate(value, index)) {
result[resultIndex++] = value;
}
}
return result;
};
}
/**
* Filter and map over an ReadonlyArray<A> in the same step. This function
* applies the predicate to each value in an array. If the predicate
* returns Some<I>, then the inner I is added to the output array.
*
* @example
* ```ts
* import * as A from "./array.ts";
* import * as O from "./option.ts";
* import { pipe } from "./fn.ts";
*
* const result = pipe(
* A.array("Hello", "Big", "World"),
* A.filterMap(s => s.includes("o") ? O.some(s.toUpperCase()) : O.none),
* ); // ["HELLO", "WORLD"]
* ```
*
* @since 2.0.0
*/
export function filterMap<A, I>(
predicate: (a: A, index: number) => Option<I>,
): (ua: ReadonlyArray<A>) => ReadonlyArray<I> {
return (ua) => {
let index = -1;
let resultIndex = 0;
const length = ua.length;
const result = [];
while (++index < length) {
const value = ua[index];
const filtered = predicate(value, index);
if (isSome(filtered)) {
result[resultIndex++] = filtered.value;
}
}
return result;
};
}
/**
* Partition a ReadonlyArray<A> into two ReadonlyArrays using a predicate or
* refinement to do the sorting. If the predicate or refinement returns true for
* a value, the value is pushed into the first array in a Pair, otherwise it is
* pushed into the second array in a pair.
*
* @example
* ```ts
* import * as A from "./array.ts";
* import { pipe } from "./fn.ts";
*
* const result = pipe(
* A.range(10, 1), // [1, 2, 3, ..., 10]
* A.partition(n => n % 2 === 0),
* ); // Pair<[2, 4, 6, 8, 10], [1, 3, 5, 7, 9]>
* ```
*
* @since 2.0.0
*/
export function partition<A, B extends A>(
refinement: (a: A, index: number) => a is B,
): (ua: ReadonlyArray<A>) => Pair<ReadonlyArray<B>, ReadonlyArray<A>>;
export function partition<A>(
predicate: (a: A, index: number) => boolean,
): (ua: ReadonlyArray<A>) => Pair<ReadonlyArray<A>, ReadonlyArray<A>>;
export function partition<A>(
refinement: (a: A, index: number) => boolean,
): (ua: ReadonlyArray<A>) => Pair<ReadonlyArray<A>, ReadonlyArray<A>> {
return (ua) => {
const first: Array<A> = [];
const second: Array<A> = [];
const length = ua.length;
let index = -1;
while (++index < length) {
const value = ua[index];
if (refinement(value, index)) {
first.push(value);
} else {
second.push(value);
}
}
return pair(first, second);
};
}
/**
* Partition and map over a ReadonlyArray<A> in the same loop. Given a predicate
* A => Either<J, I>, this function passes each element in an array into the
* predicate. If the predicate returns Right<I> then the inner I is pushed into
* the first array in a pair. If the predicate returns Left<J> then the inner J
* is pushed into the second array in a pair.
*
* @example
* ```ts
* import * as A from "./array.ts";
* import * as E from "./either.ts";
* import { pipe } from "./fn.ts";
*
* const result = pipe(
* A.range(10, 1), // [1, 2, 3, ..., 10]
* A.partitionMap(n => n % 2 === 0 ? E.right(n * 100) : E.left(n / 10)),
* ); // Pair<[200, 400, 600, 800, 1000], [0.1, 0.3, 0.5, 0.7, 0.9]>
* ```
*
* @since 2.0.0
*/
export function partitionMap<A, I, J>(
predicate: (a: A, index: number) => Either<J, I>,
): (ua: ReadonlyArray<A>) => Pair<ReadonlyArray<I>, ReadonlyArray<J>> {
return (ua) => {
const first: Array<I> = [];
const second: Array<J> = [];
const length = ua.length;
let index = -1;
while (++index < length) {
const value = ua[index];
const filtered = predicate(value, index);
if (isRight(filtered)) {
first.push(filtered.right);
} else {
second.push(filtered.left);
}
}
return pair(first, second);
};
}
/**
* Traverse a ReadonlyArray<A> using an Applicable over V and a mapping
* function A => V<I>.
*
* @example
* ```ts
* import * as A from "./array.ts";
* import { pipe, identity } from "./fn.ts";
*
* const traverse = A.traverse(A.ApplicableArray);
*
* const result = pipe(
* [[1, 2], [3, 4]],
* traverse(identity),
* ); // [[1, 3], [1, 4], [2, 3], [2, 4]]
* ```
*
* @since 2.0.0
*/
export function traverse<V extends Kind>(
A: Applicable<V>,
): <A, I, J = never, K = never, L = unknown, M = unknown>(
favi: (a: A, i: number) => $<V, [I, J, K], [L], [M]>,
) => (ua: ReadonlyArray<A>) => $<V, [ReadonlyArray<I>, J, K], [L], [M]> {
return <A, I, J = never, K = never, L = unknown, M = unknown>(
favi: (a: A, i: number) => $<V, [I, J, K], [L], [M]>,
): (ua: ReadonlyArray<A>) => $<V, [ReadonlyArray<I>, J, K], [L], [M]> => {
const pusher = (is: I[]) => (i: I) => [...is, i];
return fold(
(vis, a: A, index) =>
pipe(
vis,
A.map(pusher),
A.apply(favi(a, index)),
),
A.wrap<I[], J, K, L, M>([] as I[]),
);
};
}
// deno-lint-ignore no-explicit-any
type ANY_ARR = any[];
/**
* The Sequence inverts a tuple of substitutions over V into V containing a
* tuple of inferred values of the substitution.
*
* ie.
* [Option<number>, Option<string>]
* becomes
* Option<[number, string]>
*
* or
*
* [Either<number, number> Either<string, string>]
* becomes
* Either<string | number, [number, string]>
*/
// deno-fmt-ignore
type Sequence<U extends Kind, R extends AnySub<U>[]> = $<U, [
{ [K in keyof R]: R[K] extends $<U, [infer A, infer _, infer _], ANY_ARR, ANY_ARR> ? A : never; },
{ [K in keyof R]: R[K] extends $<U, [infer _, infer B, infer _], ANY_ARR, ANY_ARR> ? B : never; }[number],
{ [K in keyof R]: R[K] extends $<U, [infer _, infer _, infer C], ANY_ARR, ANY_ARR> ? C : never; }[number],
], [
Intersect<{ [K in keyof R]: R[K] extends $<U, ANY_ARR, [infer D], ANY_ARR> ? D : never; }[number]>,
], [
Intersect<{ [K in keyof R]: R[K] extends $<U, ANY_ARR, ANY_ARR, [infer E]> ? E : never; }[number]>,
]
>;
/**
* The return type of sequence for use with type inference.
*
* @since 2.0.0
*/
export type SequenceArray<U extends Kind> = <const US extends AnySub<U>[]>(
...uas: US
) => Sequence<U, US>;
/**
* Sequence over an array of type V, inverting the relationship between V and
* ReadonlyArray. This function also keeps the indexed types of in each V at
* covariant position 0. In other words sequence over [Option<number>,
* Option<string>] becomes Option<[number, string]>.
*
* @example
* ```ts
* import * as A from "./array.ts";
* import * as O from "./option.ts";
*
* const sequence = A.sequence(O.ApplicableOption);
*
* const result1 = sequence(O.some(1), O.some("Hello")); // Some([1, "Hello"])
* const result2 = sequence(O.none, O.some("Uh Oh")); // None
* ```
*
* @since 2.0.0
*/
export function sequence<V extends Kind>(
A: Applicable<V>,
): <const VS extends AnySub<V>[]>(
...ua: VS
) => Sequence<V, VS> {
// deno-lint-ignore no-explicit-any
const sequence = traverse(A)(identity as any);
return <VS extends AnySub<V>[]>(...vs: VS): Sequence<V, VS> =>
sequence(vs) as Sequence<V, VS>;
}
/**
* Create a new array by appending an item to the end of an existing array.
*
* @example
* ```ts
* import * as A from "./array.ts";
* import { pipe } from "./fn.ts";
*
* const result = pipe(
* [1, 2, 3],
* A.append(4),
* ); // [1, 2, 3, 4]
* ```
*
* @since 2.0.0
*/
export function append<A>(
last: A,
): (ua: ReadonlyArray<A>) => ReadonlyArray<A> {
return (ma) => [...ma, last];
}
/**
* Create a new array by prepending an item to the head of an existing array.
*
* @example
* ```ts
* import * as A from "./array.ts";
* import { pipe } from "./fn.ts";
*
* const result = pipe(
* [1, 2, 3],
* A.prepend(4),
* ); // [4, 1, 2, 3]
* ```
*
* @since 2.0.0
*/
export function prepend<A>(
head: A,
): (ua: ReadonlyArray<A>) => ReadonlyArray<A> {
return (ma) => [head, ...ma];
}
/**
* Create a new array by inserting a value into an array at an index. If the
* index is out of range of the existing array then no change is made.
*
* @example
* ```ts
* import * as A from "./array.ts";
* import { pipe } from "./fn.ts";
*
* const insert = A.insert(100); // Insert the value 100
* const arr = [1, 2, 3];
*
* const result1 = pipe(arr, insert(0)); // [100, 1, 2, 3]
* const result2 = pipe(arr, insert(1)); // [1, 100, 2, 3]
* const result3 = pipe(arr, insert(4)); // [1, 2, 3, 100]
* const result4 = pipe(arr, insert(4)); // [1, 2, 3]
* ```
*
* @since 2.0.0
*/
export function insert<A>(
value: A,
): (index: number) => (arr: ReadonlyArray<A>) => ReadonlyArray<A> {
return (index: number) => (arr: ReadonlyArray<A>): ReadonlyArray<A> =>
index < 0 || index > arr.length ? arr : _unsafeInsertAt(index, value, arr);
}
/**
* Create a new array by inserting a value into an array at an index. If the
* index is out of range of the existing array then no change is made.
*
* @example
* ```ts
* import * as A from "./array.ts";
* import { pipe } from "./fn.ts";
*
* const insertAt = A.insertAt(0); // Insert at index 0
* const arr = [1, 2, 3];
*
* const result1 = pipe(arr, insertAt(0)); // [0, 1, 2, 3]
* const result2 = pipe(arr, insertAt(1)); // [1, 1, 2, 3]
* const result3 = pipe(
* arr,
* A.insertAt(100)(100),
* ); // [1, 2, 3]
* ```
*
* @since 2.0.0
*/
export function insertAt(
index: number,
): <A>(value: A) => (arr: ReadonlyArray<A>) => ReadonlyArray<A> {
return <A>(value: A) => (arr: ReadonlyArray<A>): ReadonlyArray<A> =>
index < 0 || index > arr.length ? arr : _unsafeInsertAt(index, value, arr);
}
/**
* Create a new array by replacing a value of an array at an index. If the
* index is out of range of the existing array then no change is made.
*
* @example
* ```ts
* import * as A from "./array.ts";
* import { pipe } from "./fn.ts";
*
* const update = A.update(100); // Update the value 100
* const arr = [1, 2, 3];
*
* const result1 = pipe(arr, update(0)); // [100, 2, 3]
* const result2 = pipe(arr, update(1)); // [1, 100, 3]
* const result3 = pipe(arr, update(4)); // [1, 2, 3]