-
-
Notifications
You must be signed in to change notification settings - Fork 504
/
Array.ts
1666 lines (1559 loc) · 43.1 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
/**
* @file Adapted from https://github.com/purescript/purescript-arrays
*/
import { Alternative1 } from './Alternative'
import { Applicative, Applicative1, Applicative2, Applicative2C, Applicative3, Applicative3C } from './Applicative'
import { Compactable1, Separated } from './Compactable'
import { Either } from './Either'
import { Extend1 } from './Extend'
import { FilterableWithIndex1 } from './FilterableWithIndex'
import { Foldable2v1 } from './Foldable2v'
import { FoldableWithIndex1 } from './FoldableWithIndex'
import { concat, Endomorphism, identity, Predicate, Refinement, tuple } from './function'
import { FunctorWithIndex1 } from './FunctorWithIndex'
import { HKT, Type, Type2, Type3, URIS, URIS2, URIS3 } from './HKT'
import { Monad1 } from './Monad'
import { Monoid } from './Monoid'
import { none, Option, some } from './Option'
import { getSemigroup, Ord, ordNumber, fromCompare } from './Ord'
import { Plus1 } from './Plus'
import { getArraySetoid, Setoid } from './Setoid'
import { TraversableWithIndex1 } from './TraversableWithIndex'
import { Unfoldable1 } from './Unfoldable'
import { Witherable1 } from './Witherable'
import { NonEmptyArray } from './NonEmptyArray2v'
import { Show } from './Show'
declare global {
interface Array<T> {
/** phantom property added by `fp-ts` */
_URI: URI
/** phantom property added by `fp-ts` */
_A: T
}
}
declare module './HKT' {
interface URI2HKT<A> {
Array: Array<A>
}
}
export const URI = 'Array'
export type URI = typeof URI
/**
* @since 1.17.0
*/
export const getShow = <A>(S: Show<A>): Show<Array<A>> => {
return {
show: arr => `[${arr.map(S.show).join(', ')}]`
}
}
/**
*
* @example
* import { getMonoid } from 'fp-ts/lib/Array'
*
* const M = getMonoid<number>()
* assert.deepStrictEqual(M.concat([1, 2], [3, 4]), [1, 2, 3, 4])
*
* @since 1.0.0
*/
export const getMonoid = <A = never>(): Monoid<Array<A>> => {
return {
concat,
empty
}
}
/**
* Derives a Setoid over the Array of a given element type from the Setoid of that type. The derived setoid defines two
* arrays as equal if all elements of both arrays are compared equal pairwise with the given setoid `S`. In case of
* arrays of different lengths, the result is non equality.
*
*
* @example
* import { ordString } from 'fp-ts/lib/Ord'
* import { getSetoid } from 'fp-ts/lib/Array'
*
* const O = getSetoid(ordString)
* assert.strictEqual(O.equals(['a', 'b'], ['a', 'b']), true)
* assert.strictEqual(O.equals(['a'], []), false)
*
* @since 1.0.0
*/
export const getSetoid = <A>(S: Setoid<A>): Setoid<Array<A>> => {
return getArraySetoid(S)
}
/**
* Derives an `Ord` over the Array of a given element type from the `Ord` of that type. The ordering between two such
* arrays is equal to: the first non equal comparison of each arrays elements taken pairwise in increasing order, in
* case of equality over all the pairwise elements; the longest array is considered the greatest, if both arrays have
* the same length, the result is equality.
*
*
* @example
* import { getOrd } from 'fp-ts/lib/Array'
* import { ordString } from 'fp-ts/lib/Ord'
*
* const O = getOrd(ordString)
* assert.strictEqual(O.compare(['b'], ['a']), 1)
* assert.strictEqual(O.compare(['a'], ['a']), 0)
* assert.strictEqual(O.compare(['a'], ['b']), -1)
*
*
* @since 1.2.0
*/
export const getOrd = <A>(O: Ord<A>): Ord<Array<A>> => {
return fromCompare((a, b) => {
const aLen = a.length
const bLen = b.length
const len = Math.min(aLen, bLen)
for (let i = 0; i < len; i++) {
const order = O.compare(a[i], b[i])
if (order !== 0) {
return order
}
}
return ordNumber.compare(aLen, bLen)
})
}
const map = <A, B>(fa: Array<A>, f: (a: A) => B): Array<B> => {
return fa.map(a => f(a))
}
const mapWithIndex = <A, B>(fa: Array<A>, f: (index: number, a: A) => B): Array<B> => {
return fa.map((a, i) => f(i, a))
}
const of = <A>(a: A): Array<A> => {
return [a]
}
const ap = <A, B>(fab: Array<(a: A) => B>, fa: Array<A>): Array<B> => {
return flatten(map(fab, f => map(fa, f)))
}
const chain = <A, B>(fa: Array<A>, f: (a: A) => Array<B>): Array<B> => {
let resLen = 0
const l = fa.length
const temp = new Array(l)
for (let i = 0; i < l; i++) {
const e = fa[i]
const arr = f(e)
resLen += arr.length
temp[i] = arr
}
const r = Array(resLen)
let start = 0
for (let i = 0; i < l; i++) {
const arr = temp[i]
const l = arr.length
for (let j = 0; j < l; j++) {
r[j + start] = arr[j]
}
start += l
}
return r
}
const reduce = <A, B>(fa: Array<A>, b: B, f: (b: B, a: A) => B): B => {
return reduceWithIndex(fa, b, (_, b, a) => f(b, a))
}
const foldMap = <M>(M: Monoid<M>): (<A>(fa: Array<A>, f: (a: A) => M) => M) => {
const foldMapWithIndexM = foldMapWithIndex(M)
return (fa, f) => foldMapWithIndexM(fa, (_, a) => f(a))
}
const reduceRight = <A, B>(fa: Array<A>, b: B, f: (a: A, b: B) => B): B => {
return foldrWithIndex(fa, b, (_, a, b) => f(a, b))
}
const reduceWithIndex = <A, B>(fa: Array<A>, b: B, f: (i: number, b: B, a: A) => B): B => {
const l = fa.length
let r = b
for (let i = 0; i < l; i++) {
r = f(i, r, fa[i])
}
return r
}
const foldMapWithIndex = <M>(M: Monoid<M>) => <A>(fa: Array<A>, f: (i: number, a: A) => M): M => {
return fa.reduce((b, a, i) => M.concat(b, f(i, a)), M.empty)
}
const foldrWithIndex = <A, B>(fa: Array<A>, b: B, f: (i: number, a: A, b: B) => B): B => {
return fa.reduceRight((b, a, i) => f(i, a, b), b)
}
/**
* Use `array.traverse` instead
*
* @since 1.0.0
* @deprecated
*/
export function traverse<F extends URIS3>(
F: Applicative3<F>
): <U, L, A, B>(ta: Array<A>, f: (a: A) => Type3<F, U, L, B>) => Type3<F, U, L, Array<B>>
export function traverse<F extends URIS3, U, L>(
F: Applicative3C<F, U, L>
): <A, B>(ta: Array<A>, f: (a: A) => Type3<F, U, L, B>) => Type3<F, U, L, Array<B>>
export function traverse<F extends URIS2>(
F: Applicative2<F>
): <L, A, B>(ta: Array<A>, f: (a: A) => Type2<F, L, B>) => Type2<F, L, Array<B>>
export function traverse<F extends URIS2, L>(
F: Applicative2C<F, L>
): <A, B>(ta: Array<A>, f: (a: A) => Type2<F, L, B>) => Type2<F, L, Array<B>>
export function traverse<F extends URIS>(
F: Applicative1<F>
): <A, B>(ta: Array<A>, f: (a: A) => Type<F, B>) => Type<F, Array<B>>
export function traverse<F>(F: Applicative<F>): <A, B>(ta: Array<A>, f: (a: A) => HKT<F, B>) => HKT<F, Array<B>>
export function traverse<F>(F: Applicative<F>): <A, B>(ta: Array<A>, f: (a: A) => HKT<F, B>) => HKT<F, Array<B>> {
const traverseWithIndexF = traverseWithIndex(F)
return (ta, f) => traverseWithIndexF(ta, (_, a) => f(a))
}
const sequence = <F>(F: Applicative<F>) => <A>(ta: Array<HKT<F, A>>): HKT<F, Array<A>> => {
return reduce(ta, F.of<Array<A>>(zero()), (fas, fa) => F.ap(F.map(fas, as => (a: A) => snoc(as, a)), fa))
}
/**
* An empty array
*
*
* @since 1.9.0
*/
export const empty: Array<never> = []
const zero = <A>(): Array<A> => empty
const alt = concat
const unfoldr = <A, B>(b: B, f: (b: B) => Option<[A, B]>): Array<A> => {
const ret: Array<A> = []
let bb = b
while (true) {
const mt = f(bb)
if (mt.isSome()) {
const [a, b] = mt.value
ret.push(a)
bb = b
} else {
break
}
}
return ret
}
/**
* Return a list of length `n` with element `i` initialized with `f(i)`
*
* @example
* import { makeBy } from 'fp-ts/lib/Array'
*
* const double = (n: number): number => n * 2
* assert.deepStrictEqual(makeBy(5, double), [0, 2, 4, 6, 8])
*
*
* @since 1.10.0
*/
export const makeBy = <A>(n: number, f: (i: number) => A): Array<A> => {
const r: Array<A> = []
for (let i = 0; i < n; i++) {
r.push(f(i))
}
return r
}
/**
* Create an array containing a range of integers, including both endpoints
*
* @example
* import { range } from 'fp-ts/lib/Array'
*
* assert.deepStrictEqual(range(1, 5), [1, 2, 3, 4, 5])
*
*
* @since 1.10.0
*/
export const range = (start: number, end: number): Array<number> => {
return makeBy(end - start + 1, i => start + i)
}
/**
* Create an array containing a value repeated the specified number of times
*
* @example
* import { replicate } from 'fp-ts/lib/Array'
*
* assert.deepStrictEqual(replicate(3, 'a'), ['a', 'a', 'a'])
*
*
* @since 1.10.0
*/
export const replicate = <A>(n: number, a: A): Array<A> => {
return makeBy(n, () => a)
}
const extend = <A, B>(fa: Array<A>, f: (fa: Array<A>) => B): Array<B> => {
return fa.map((_, i, as) => f(as.slice(i)))
}
/**
* Removes one level of nesting
*
* @example
* import { flatten } from 'fp-ts/lib/Array'
*
* assert.deepStrictEqual(flatten([[1], [2], [3]]), [1, 2, 3])
*
* @since 1.0.0
*/
export const flatten = <A>(ffa: Array<Array<A>>): Array<A> => {
let rLen = 0
const len = ffa.length
for (let i = 0; i < len; i++) {
rLen += ffa[i].length
}
const r = Array(rLen)
let start = 0
for (let i = 0; i < len; i++) {
const arr = ffa[i]
const l = arr.length
for (let j = 0; j < l; j++) {
r[j + start] = arr[j]
}
start += l
}
return r
}
/**
* Break an array into its first element and remaining elements
*
* @example
* import { fold } from 'fp-ts/lib/Array'
*
* const len = <A>(as: Array<A>): number => fold(as, 0, (_, tail) => 1 + len(tail))
* assert.strictEqual(len([1, 2, 3]), 3)
*
* @since 1.0.0
*/
export const fold = <A, B>(as: Array<A>, b: B, cons: (head: A, tail: Array<A>) => B): B => {
return isEmpty(as) ? b : cons(as[0], as.slice(1))
}
/**
* Lazy version of `fold`
*
* @since 1.0.0
*/
export const foldL = <A, B>(as: Array<A>, nil: () => B, cons: (head: A, tail: Array<A>) => B): B => {
return isEmpty(as) ? nil() : cons(as[0], as.slice(1))
}
/**
* Break an array into its initial elements and the last element
*
* @since 1.7.0
* @param as
* @param b
* @param cons
*/
export const foldr = <A, B>(as: Array<A>, b: B, cons: (init: Array<A>, last: A) => B): B => {
return isEmpty(as) ? b : cons(as.slice(0, as.length - 1), as[as.length - 1])
}
/**
* Lazy version of `foldr`
*
* @since 1.7.0
* @param as
* @param nil
* @param cons
*/
export const foldrL = <A, B>(as: Array<A>, nil: () => B, cons: (init: Array<A>, last: A) => B): B => {
return isEmpty(as) ? nil() : cons(as.slice(0, as.length - 1), as[as.length - 1])
}
/**
* Same as `reduce` but it carries over the intermediate steps
*
* ```ts
* import { scanLeft } from 'fp-ts/lib/Array'
*
* assert.deepStrictEqual(scanLeft([1, 2, 3], 10, (b, a) => b - a), [ 10, 9, 7, 4 ])
* ```
*
*
* @since 1.1.0
*/
export const scanLeft = <A, B>(as: Array<A>, b: B, f: (b: B, a: A) => B): Array<B> => {
const l = as.length
const r: Array<B> = new Array(l + 1)
r[0] = b
for (let i = 0; i < l; i++) {
r[i + 1] = f(r[i], as[i])
}
return r
}
/**
* Fold an array from the right, keeping all intermediate results instead of only the final result
*
* @example
* import { scanRight } from 'fp-ts/lib/Array'
*
* assert.deepStrictEqual(scanRight([1, 2, 3], 10, (a, b) => b - a), [ 4, 5, 7, 10 ])
*
*
* @since 1.1.0
*/
export const scanRight = <A, B>(as: Array<A>, b: B, f: (a: A, b: B) => B): Array<B> => {
const l = as.length
const r: Array<B> = new Array(l + 1)
r[l] = b
for (let i = l - 1; i >= 0; i--) {
r[i] = f(as[i], r[i + 1])
}
return r
}
/**
* Test whether an array is empty
*
* @example
* import { isEmpty } from 'fp-ts/lib/Array'
*
* assert.strictEqual(isEmpty([]), true)
*
* @since 1.0.0
*/
export const isEmpty = <A>(as: Array<A>): boolean => {
return as.length === 0
}
/**
* Test whether an array contains a particular index
*
* @since 1.0.0
*/
export const isOutOfBound = <A>(i: number, as: Array<A>): boolean => {
return i < 0 || i >= as.length
}
/**
* This function provides a safe way to read a value at a particular index from an array
*
* @example
* import { lookup } from 'fp-ts/lib/Array'
* import { some, none } from 'fp-ts/lib/Option'
*
* assert.deepStrictEqual(lookup(1, [1, 2, 3]), some(2))
* assert.deepStrictEqual(lookup(3, [1, 2, 3]), none)
*
* @since 1.14.0
*/
export const lookup = <A>(i: number, as: Array<A>): Option<A> => {
return isOutOfBound(i, as) ? none : some(as[i])
}
/**
* Use `lookup` instead
* @since 1.0.0
* @deprecated
*/
export const index = <A>(i: number, as: Array<A>): Option<A> => {
return lookup(i, as)
}
/**
* Attaches an element to the front of an array, creating a new non empty array
*
* @example
* import { cons } from 'fp-ts/lib/Array'
*
* assert.deepStrictEqual(cons(0, [1, 2, 3]), [0, 1, 2, 3])
*
* @since 1.0.0
*/
export const cons = <A>(a: A, as: Array<A>): NonEmptyArray<A> => {
const len = as.length
const r = Array(len + 1)
for (let i = 0; i < len; i++) {
r[i + 1] = as[i]
}
r[0] = a
return r as NonEmptyArray<A>
}
/**
* Append an element to the end of an array, creating a new non empty array
*
* @example
* import { snoc } from 'fp-ts/lib/Array'
*
* assert.deepStrictEqual(snoc([1, 2, 3], 4), [1, 2, 3, 4])
*
* @since 1.0.0
*/
export const snoc = <A>(as: Array<A>, a: A): NonEmptyArray<A> => {
const len = as.length
const r = Array(len + 1)
for (let i = 0; i < len; i++) {
r[i] = as[i]
}
r[len] = a
return r as NonEmptyArray<A>
}
/**
* Get the first element in an array, or `None` if the array is empty
*
* @example
* import { head } from 'fp-ts/lib/Array'
* import { some, none } from 'fp-ts/lib/Option'
*
* assert.deepStrictEqual(head([1, 2, 3]), some(1))
* assert.deepStrictEqual(head([]), none)
*
* @since 1.0.0
*/
export const head = <A>(as: Array<A>): Option<A> => {
return isEmpty(as) ? none : some(as[0])
}
/**
* Get the last element in an array, or `None` if the array is empty
*
* @example
* import { last } from 'fp-ts/lib/Array'
* import { some, none } from 'fp-ts/lib/Option'
*
* assert.deepStrictEqual(last([1, 2, 3]), some(3))
* assert.deepStrictEqual(last([]), none)
*
* @since 1.0.0
*/
export const last = <A>(as: Array<A>): Option<A> => {
return lookup(as.length - 1, as)
}
/**
* Get all but the first element of an array, creating a new array, or `None` if the array is empty
*
* @example
* import { tail } from 'fp-ts/lib/Array'
* import { some, none } from 'fp-ts/lib/Option'
*
* assert.deepStrictEqual(tail([1, 2, 3]), some([2, 3]))
* assert.deepStrictEqual(tail([]), none)
*
* @since 1.0.0
*/
export const tail = <A>(as: Array<A>): Option<Array<A>> => {
return isEmpty(as) ? none : some(as.slice(1))
}
/**
* Get all but the last element of an array, creating a new array, or `None` if the array is empty
*
* @example
* import { init } from 'fp-ts/lib/Array'
* import { some, none } from 'fp-ts/lib/Option'
*
* assert.deepStrictEqual(init([1, 2, 3]), some([1, 2]))
* assert.deepStrictEqual(init([]), none)
*
* @since 1.0.0
*/
export const init = <A>(as: Array<A>): Option<Array<A>> => {
const len = as.length
return len === 0 ? none : some(as.slice(0, len - 1))
}
/**
* Keep only a number of elements from the start of an array, creating a new array.
* `n` must be a natural number
*
* @example
* import { take } from 'fp-ts/lib/Array'
*
* assert.deepStrictEqual(take(2, [1, 2, 3]), [1, 2])
*
* @since 1.0.0
*/
export const take = <A>(n: number, as: Array<A>): Array<A> => {
return as.slice(0, n)
}
/**
* Keep only a number of elements from the end of an array, creating a new array.
* `n` must be a natural number
*
* @example
* import { takeEnd } from 'fp-ts/lib/Array'
*
* assert.deepStrictEqual(takeEnd(2, [1, 2, 3, 4, 5]), [4, 5])
*
*
* @since 1.10.0
*/
export const takeEnd = <A>(n: number, as: Array<A>): Array<A> => {
return n === 0 ? empty : as.slice(-n)
}
/**
* Calculate the longest initial subarray for which all element satisfy the specified predicate, creating a new array
*
* @example
* import { takeWhile } from 'fp-ts/lib/Array'
*
* assert.deepStrictEqual(takeWhile([2, 4, 3, 6], n => n % 2 === 0), [2, 4])
*
* @since 1.0.0
*/
export function takeWhile<A, B extends A>(as: Array<A>, predicate: Refinement<A, B>): Array<B>
export function takeWhile<A>(as: Array<A>, predicate: Predicate<A>): Array<A>
export function takeWhile<A>(as: Array<A>, predicate: Predicate<A>): Array<A> {
const i = spanIndexUncurry(as, predicate)
const init = Array(i)
for (let j = 0; j < i; j++) {
init[j] = as[j]
}
return init
}
const spanIndexUncurry = <A>(as: Array<A>, predicate: Predicate<A>): number => {
const l = as.length
let i = 0
for (; i < l; i++) {
if (!predicate(as[i])) {
break
}
}
return i
}
/**
* Split an array into two parts:
* 1. the longest initial subarray for which all elements satisfy the specified predicate
* 2. the remaining elements
*
* @example
* import { span } from 'fp-ts/lib/Array'
*
* assert.deepStrictEqual(span([1, 3, 2, 4, 5], n => n % 2 === 1), { init: [1, 3], rest: [2, 4, 5] })
*
* @since 1.0.0
*/
export function span<A, B extends A>(as: Array<A>, predicate: Refinement<A, B>): { init: Array<B>; rest: Array<A> }
export function span<A>(as: Array<A>, predicate: Predicate<A>): { init: Array<A>; rest: Array<A> }
export function span<A>(as: Array<A>, predicate: Predicate<A>): { init: Array<A>; rest: Array<A> } {
const i = spanIndexUncurry(as, predicate)
const init = Array(i)
for (let j = 0; j < i; j++) {
init[j] = as[j]
}
const l = as.length
const rest = Array(l - i)
for (let j = i; j < l; j++) {
rest[j - i] = as[j]
}
return { init, rest }
}
/**
* Drop a number of elements from the start of an array, creating a new array
*
* @example
* import { drop } from 'fp-ts/lib/Array'
*
* assert.deepStrictEqual(drop(2, [1, 2, 3]), [3])
*
* @since 1.0.0
*/
export const drop = <A>(n: number, as: Array<A>): Array<A> => {
return as.slice(n, as.length)
}
/**
* Drop a number of elements from the end of an array, creating a new array
*
* @example
* import { dropEnd } from 'fp-ts/lib/Array'
*
* assert.deepStrictEqual(dropEnd(2, [1, 2, 3, 4, 5]), [1, 2, 3])
*
*
* @since 1.10.0
*/
export const dropEnd = <A>(n: number, as: Array<A>): Array<A> => {
return as.slice(0, as.length - n)
}
/**
* Remove the longest initial subarray for which all element satisfy the specified predicate, creating a new array
*
* @example
* import { dropWhile } from 'fp-ts/lib/Array'
*
* assert.deepStrictEqual(dropWhile([1, 3, 2, 4, 5], n => n % 2 === 1), [2, 4, 5])
*
* @since 1.0.0
*/
export const dropWhile = <A>(as: Array<A>, predicate: Predicate<A>): Array<A> => {
const i = spanIndexUncurry(as, predicate)
const l = as.length
const rest = Array(l - i)
for (let j = i; j < l; j++) {
rest[j - i] = as[j]
}
return rest
}
/**
* Find the first index for which a predicate holds
*
* @example
* import { findIndex } from 'fp-ts/lib/Array'
* import { some, none } from 'fp-ts/lib/Option'
*
* assert.deepStrictEqual(findIndex([1, 2, 3], x => x === 2), some(1))
* assert.deepStrictEqual(findIndex([], x => x === 2), none)
*
* @since 1.0.0
*/
export const findIndex = <A>(as: Array<A>, predicate: Predicate<A>): Option<number> => {
const len = as.length
for (let i = 0; i < len; i++) {
if (predicate(as[i])) {
return some(i)
}
}
return none
}
/**
* Find the first element which satisfies a predicate (or a refinement) function
*
* @example
* import { findFirst } from 'fp-ts/lib/Array'
* import { some } from 'fp-ts/lib/Option'
*
* assert.deepStrictEqual(findFirst([{ a: 1, b: 1 }, { a: 1, b: 2 }], x => x.a === 1), some({ a: 1, b: 1 }))
*
* @since 1.0.0
*/
export function findFirst<A, B extends A>(as: Array<A>, predicate: Refinement<A, B>): Option<B>
export function findFirst<A>(as: Array<A>, predicate: Predicate<A>): Option<A>
export function findFirst<A>(as: Array<A>, predicate: Predicate<A>): Option<A> {
const len = as.length
for (let i = 0; i < len; i++) {
if (predicate(as[i])) {
return some(as[i])
}
}
return none
}
/**
* Find the first element returned by an option based selector function
*
* @example
* import { findFirstMap } from 'fp-ts/lib/Array'
* import { some, none } from 'fp-ts/lib/Option'
*
* interface Person {
* name: string
* age?: number
* }
*
* const persons: Array<Person> = [{ name: 'John' }, { name: 'Mary', age: 45 }, { name: 'Joey', age: 28 }]
*
* // returns the name of the first person that has an age
* assert.deepStrictEqual(findFirstMap(persons, p => (p.age === undefined ? none : some(p.name))), some('Mary'))
*
* @since 1.16.0
*/
export const findFirstMap = <A, B>(arr: Array<A>, f: (a: A) => Option<B>): Option<B> => {
const len = arr.length
for (let i = 0; i < len; i++) {
const v = f(arr[i])
if (v.isSome()) {
return v
}
}
return none
}
/**
* Find the last element which satisfies a predicate function
*
* @example
* import { findLast } from 'fp-ts/lib/Array'
* import { some } from 'fp-ts/lib/Option'
*
* assert.deepStrictEqual(findLast([{ a: 1, b: 1 }, { a: 1, b: 2 }], x => x.a === 1), some({ a: 1, b: 2 }))
*
* @since 1.0.0
*/
export function findLast<A, B extends A>(as: Array<A>, predicate: Refinement<A, B>): Option<B>
export function findLast<A>(as: Array<A>, predicate: Predicate<A>): Option<A>
export function findLast<A>(as: Array<A>, predicate: Predicate<A>): Option<A> {
const len = as.length
for (let i = len - 1; i >= 0; i--) {
if (predicate(as[i])) {
return some(as[i])
}
}
return none
}
/**
* Find the last element returned by an option based selector function
*
* @example
* import { findLastMap } from 'fp-ts/lib/Array'
* import { some, none } from 'fp-ts/lib/Option'
*
* interface Person {
* name: string
* age?: number
* }
*
* const persons: Array<Person> = [{ name: 'John' }, { name: 'Mary', age: 45 }, { name: 'Joey', age: 28 }]
*
* // returns the name of the last person that has an age
* assert.deepStrictEqual(findLastMap(persons, p => (p.age === undefined ? none : some(p.name))), some('Joey'))
*
* @since 1.16.0
*/
export const findLastMap = <A, B>(arr: Array<A>, f: (a: A) => Option<B>): Option<B> => {
const len = arr.length
for (let i = len - 1; i >= 0; i--) {
const v = f(arr[i])
if (v.isSome()) {
return v
}
}
return none
}
/**
* Returns the index of the last element of the list which matches the predicate
*
* @example
* import { findLastIndex } from 'fp-ts/lib/Array'
* import { some, none } from 'fp-ts/lib/Option'
*
* interface X {
* a: number
* b: number
* }
* const xs: Array<X> = [{ a: 1, b: 0 }, { a: 1, b: 1 }]
* assert.deepStrictEqual(findLastIndex(xs, x => x.a === 1), some(1))
* assert.deepStrictEqual(findLastIndex(xs, x => x.a === 4), none)
*
*
* @since 1.10.0
*/
export const findLastIndex = <A>(as: Array<A>, predicate: Predicate<A>): Option<number> => {
const len = as.length
for (let i = len - 1; i >= 0; i--) {
if (predicate(as[i])) {
return some(i)
}
}
return none
}
/**
* Use `array.filter` instead
*
* @since 1.0.0
* @deprecated
*/
export const refine = <A, B extends A>(as: Array<A>, refinement: Refinement<A, B>): Array<B> => {
// tslint:disable-next-line: deprecation
return filter(as, refinement)
}
/**
* @since 1.0.0
*/
export const copy = <A>(as: Array<A>): Array<A> => {
const l = as.length
const r = Array(l)
for (let i = 0; i < l; i++) {
r[i] = as[i]
}
return r
}
/**
*
* @since 1.0.0
*/
export const unsafeInsertAt = <A>(i: number, a: A, as: Array<A>): Array<A> => {
const xs = copy(as)
xs.splice(i, 0, a)
return xs
}
/**
* Insert an element at the specified index, creating a new array, or returning `None` if the index is out of bounds
*
* @example
* import { insertAt } from 'fp-ts/lib/Array'
* import { some } from 'fp-ts/lib/Option'
*
* assert.deepStrictEqual(insertAt(2, 5, [1, 2, 3, 4]), some([1, 2, 5, 3, 4]))
*
* @since 1.0.0
*/
export const insertAt = <A>(i: number, a: A, as: Array<A>): Option<Array<A>> => {
return i < 0 || i > as.length ? none : some(unsafeInsertAt(i, a, as))
}
/**
*
* @since 1.0.0
*/
export const unsafeUpdateAt = <A>(i: number, a: A, as: Array<A>): Array<A> => {
if (as[i] === a) {
return as
} else {
const xs = copy(as)
xs[i] = a
return xs
}
}
/**
* Change the element at the specified index, creating a new array, or returning `None` if the index is out of bounds
*
* @example
* import { updateAt } from 'fp-ts/lib/Array'
* import { some, none } from 'fp-ts/lib/Option'
*
* assert.deepStrictEqual(updateAt(1, 1, [1, 2, 3]), some([1, 1, 3]))
* assert.deepStrictEqual(updateAt(1, 1, []), none)
*
* @since 1.0.0
*/
export const updateAt = <A>(i: number, a: A, as: Array<A>): Option<Array<A>> => {
return isOutOfBound(i, as) ? none : some(unsafeUpdateAt(i, a, as))
}
/**
*
* @since 1.0.0
*/
export const unsafeDeleteAt = <A>(i: number, as: Array<A>): Array<A> => {
const xs = copy(as)
xs.splice(i, 1)
return xs
}
/**
* Delete the element at the specified index, creating a new array, or returning `None` if the index is out of bounds
*
* @example
* import { deleteAt } from 'fp-ts/lib/Array'
* import { some, none } from 'fp-ts/lib/Option'
*
* assert.deepStrictEqual(deleteAt(0, [1, 2, 3]), some([2, 3]))
* assert.deepStrictEqual(deleteAt(1, []), none)
*
* @since 1.0.0
*/
export const deleteAt = <A>(i: number, as: Array<A>): Option<Array<A>> => {
return isOutOfBound(i, as) ? none : some(unsafeDeleteAt(i, as))
}
/**
* Apply a function to the element at the specified index, creating a new array, or returning `None` if the index is out
* of bounds
*
* @example
* import { modifyAt } from 'fp-ts/lib/Array'
* import { some, none } from 'fp-ts/lib/Option'
*
* const double = (x: number): number => x * 2
* assert.deepStrictEqual(modifyAt([1, 2, 3], 1, double), some([1, 4, 3]))
* assert.deepStrictEqual(modifyAt([], 1, double), none)
*
* @since 1.0.0
*/
export const modifyAt = <A>(as: Array<A>, i: number, f: Endomorphism<A>): Option<Array<A>> => {
return isOutOfBound(i, as) ? none : some(unsafeUpdateAt(i, f(as[i]), as))
}
/**
* Reverse an array, creating a new array