/
List.java
1902 lines (1708 loc) · 58.3 KB
/
List.java
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
package fj.data;
import static fj.Bottom.error;
import fj.Effect;
import fj.Equal;
import fj.F;
import fj.F2;
import fj.F3;
import fj.Function;
import fj.Hash;
import fj.Monoid;
import fj.Ord;
import fj.P;
import fj.P1;
import fj.P2;
import fj.Show;
import fj.Unit;
import static fj.Function.curry;
import static fj.Function.constant;
import static fj.Function.identity;
import static fj.Function.compose;
import static fj.P.p;
import static fj.P.p2;
import static fj.Unit.unit;
import static fj.data.Array.mkArray;
import static fj.data.List.Buffer.*;
import static fj.data.Option.none;
import static fj.data.Option.some;
import static fj.function.Booleans.not;
import static fj.Ordering.GT;
import static fj.Ord.intOrd;
import fj.Ordering;
import fj.control.Trampoline;
import java.util.AbstractCollection;
import java.util.Collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* Provides an in-memory, immutable, singly linked list.
*
* @version %build.number%
*/
public abstract class List<A> implements Iterable<A> {
private List() {
}
/**
* Returns an iterator for this list. This method exists to permit the use in a <code>for</code>-each loop.
*
* @return A iterator for this list.
*/
public final Iterator<A> iterator() {
return toCollection().iterator();
}
/**
* The first element of the linked list or fails for the empty list.
*
* @return The first element of the linked list or fails for the empty list.
*/
public abstract A head();
/**
* The list without the first element or fails for the empty list.
*
* @return The list without the first element or fails for the empty list.
*/
public abstract List<A> tail();
/**
* The length of this list.
*
* @return The length of this list.
*/
public final int length() {
return foldLeft(new F<Integer, F<A, Integer>>() {
public F<A, Integer> f(final Integer i) {
return new F<A, Integer>() {
public Integer f(final A a) {
return i + 1;
}
};
}
}, 0);
}
/**
* Returns <code>true</code> if this list is empty, <code>false</code> otherwise.
*
* @return <code>true</code> if this list is empty, <code>false</code> otherwise.
*/
public final boolean isEmpty() {
return this instanceof Nil;
}
/**
* Returns <code>false</code> if this list is empty, <code>true</code> otherwise.
*
* @return <code>false</code> if this list is empty, <code>true</code> otherwise.
*/
public final boolean isNotEmpty() {
return this instanceof Cons;
}
/**
* Performs a reduction on this list using the given arguments.
*
* @param nil The value to return if this list is empty.
* @param cons The function to apply to the head and tail of this list if it is not empty.
* @return A reduction on this list.
*/
public final <B> B list(final B nil, final F<A, F<List<A>, B>> cons) {
return isEmpty() ? nil : cons.f(head()).f(tail());
}
/**
* Returns the head of this list if there is one or the given argument if this list is empty.
*
* @param a The argument to return if this list is empty.
* @return The head of this list if there is one or the given argument if this list is empty.
*/
public final A orHead(final P1<A> a) {
return isEmpty() ? a._1() : head();
}
/**
* Returns the tail of this list if there is one or the given argument if this list is empty.
*
* @param as The argument to return if this list is empty.
* @return The tail of this list if there is one or the given argument if this list is empty.
*/
public final List<A> orTail(final P1<List<A>> as) {
return isEmpty() ? as._1() : tail();
}
/**
* Returns an option projection of this list; <code>None</code> if empty, or the first element in
* <code>Some</code>.
*
* @return An option projection of this list.
*/
public final Option<A> toOption() {
return isEmpty() ? Option.<A>none() : some(head());
}
/**
* Returns an either projection of this list; the given argument in <code>Left</code> if empty, or
* the first element in <code>Right</code>.
*
* @param x The value to return in left if this list is empty.
* @return An either projection of this list.
*/
public final <X> Either<X, A> toEither(final P1<X> x) {
return isEmpty() ? Either.<X, A>left(x._1()) : Either.<X, A>right(head());
}
/**
* Returns a stream projection of this list.
*
* @return A stream projection of this list.
*/
public final Stream<A> toStream() {
final Stream<A> nil = Stream.nil();
return foldRight(new F<A, F<Stream<A>, Stream<A>>>() {
public F<Stream<A>, Stream<A>> f(final A a) {
return new F<Stream<A>, Stream<A>>() {
public Stream<A> f(final Stream<A> as) {
return as.cons(a);
}
};
}
}, nil);
}
/**
* Returns a array projection of this list.
*
* @return A array projection of this list.
*/
@SuppressWarnings({"unchecked"})
public final Array<A> toArray() {
final Object[] a = new Object[length()];
List<A> x = this;
for (int i = 0; i < length(); i++) {
a[i] = x.head();
x = x.tail();
}
return mkArray(a);
}
/**
* Returns a array projection of this list.
*
* @param c The class type of the array to return.
* @return A array projection of this list.
*/
@SuppressWarnings({"unchecked", "UnnecessaryFullyQualifiedName"})
public final Array<A> toArray(final Class<A[]> c) {
final A[] a = (A[]) java.lang.reflect.Array.newInstance(c.getComponentType(), length());
List<A> x = this;
for (int i = 0; i < length(); i++) {
a[i] = x.head();
x = x.tail();
}
return Array.array(a);
}
/**
* Returns an array from this list.
*
* @param c The class type of the array to return.
* @return An array from this list.
*/
public final A[] array(final Class<A[]> c) {
return toArray(c).array(c);
}
/**
* Prepends (cons) the given element to this list to product a new list.
*
* @param a The element to prepend.
* @return A new list with the given element at the head.
*/
public final List<A> cons(final A a) {
return new Cons<A>(a, this);
}
/**
* Prepends (cons) the given element to this list to product a new list. This method is added to prevent conflict with
* overloads.
*
* @param a The element to prepend.
* @return A new list with the given element at the head.
*/
public final List<A> conss(final A a) {
return new Cons<A>(a, this);
}
/**
* Maps the given function across this list.
*
* @param f The function to map across this list.
* @return A new list after the given function has been applied to each element.
*/
public final <B> List<B> map(final F<A, B> f) {
final Buffer<B> bs = empty();
for (List<A> xs = this; xs.isNotEmpty(); xs = xs.tail()) {
bs.snoc(f.f(xs.head()));
}
return bs.toList();
}
/**
* Performs a side-effect for each element of this list.
*
* @param f The side-effect to perform for the given element.
* @return The unit value.
*/
public final Unit foreach(final F<A, Unit> f) {
for (List<A> xs = this; xs.isNotEmpty(); xs = xs.tail()) {
f.f(xs.head());
}
return unit();
}
/**
* Performs a side-effect for each element of this list.
*
* @param f The side-effect to perform for the given element.
*/
public final void foreach(final Effect<A> f) {
for (List<A> xs = this; xs.isNotEmpty(); xs = xs.tail()) {
f.e(xs.head());
}
}
/**
* Filters elements from this list by returning only elements which produce <code>true</code> when
* the given function is applied to them.
*
* @param f The predicate function to filter on.
* @return A new list whose elements all match the given predicate.
*/
public final List<A> filter(final F<A, Boolean> f) {
final Buffer<A> b = empty();
for (List<A> xs = this; xs.isNotEmpty(); xs = xs.tail()) {
final A h = xs.head();
if (f.f(h)) {
b.snoc(h);
}
}
return b.toList();
}
/**
* Filters elements from this list by returning only elements which produce <code>false</code> when
* the given function is applied to them.
*
* @param f The predicate function to filter on.
* @return A new list whose elements do not match the given predicate.
*/
public final List<A> removeAll(final F<A, Boolean> f) {
return filter(compose(not, f));
}
/**
* Removes the first element that equals the given object.
* To remove all matches, use <code>removeAll(e.eq(a))</code>
*
* @param a The element to remove
* @param e An <code>Equals</code> instance for the element's type.
* @return A new list whose elements do not match the given predicate.
*/
public final List<A> delete(final A a, final Equal<A> e) {
final P2<List<A>, List<A>> p = span(compose(not, e.eq(a)));
return p._2().isEmpty() ? p._1() : p._1().append(p._2().tail());
}
/**
* Returns the first elements of the head of this list that match the given predicate function.
*
* @param f The predicate function to apply on this list until it finds an element that does not
* hold, or the list is exhausted.
* @return The first elements of the head of this list that match the given predicate function.
*/
public final List<A> takeWhile(final F<A, Boolean> f) {
final Buffer<A> b = empty();
boolean taking = true;
for (List<A> xs = this; xs.isNotEmpty() && taking; xs = xs.tail()) {
final A h = xs.head();
if (f.f(h)) {
b.snoc(h);
} else {
taking = false;
}
}
return b.toList();
}
/**
* Removes elements from the head of this list that do not match the given predicate function
* until an element is found that does match or the list is exhausted.
*
* @param f The predicate function to apply through this list.
* @return The list whose first element does not match the given predicate function.
*/
public final List<A> dropWhile(final F<A, Boolean> f) {
List<A> xs;
//noinspection StatementWithEmptyBody
for (xs = this; xs.isNotEmpty() && f.f(xs.head()); xs = xs.tail()) ;
return xs;
}
/**
* Returns a tuple where the first element is the longest prefix of this list that satisfies
* the given predicate and the second element is the remainder of the list.
*
* @param p A predicate to be satisfied by a prefix of this list.
* @return A tuple where the first element is the longest prefix of this list that satisfies
* the given predicate and the second element is the remainder of the list.
*/
public final P2<List<A>, List<A>> span(final F<A, Boolean> p) {
final Buffer<A> b = empty();
for (List<A> xs = this; xs.isNotEmpty(); xs = xs.tail()) {
if (p.f(xs.head()))
b.snoc(xs.head());
else
return P.p(b.toList(), xs);
}
return P.p(b.toList(), List.<A>nil());
}
/**
* Returns a tuple where the first element is the longest prefix of this list that does not satisfy
* the given predicate and the second element is the remainder of the list.
*
* @param p A predicate for an element to not satisfy by a prefix of this list.
* @return A tuple where the first element is the longest prefix of this list that does not satisfy
* the given predicate and the second element is the remainder of the list.
*/
public final P2<List<A>, List<A>> breakk(final F<A, Boolean> p) {
return span(new F<A, Boolean>() {
public Boolean f(final A a) {
return !p.f(a);
}
});
}
/**
* Groups elements according to the given equality implementation.
*
* @param e The equality implementation for the elements.
* @return A list of grouped elements.
*/
public final List<List<A>> group(final Equal<A> e) {
if (isEmpty())
return nil();
else {
final P2<List<A>, List<A>> z = tail().span(e.eq(head()));
return cons(z._1().cons(head()), z._2().group(e));
}
}
/**
* Binds the given function across each element of this list with a final join.
*
* @param f The function to apply to each element of this list.
* @return A new list after performing the map, then final join.
*/
public final <B> List<B> bind(final F<A, List<B>> f) {
final Buffer<B> b = empty();
for (List<A> xs = this; xs.isNotEmpty(); xs = xs.tail()) {
b.append(f.f(xs.head()));
}
return b.toList();
}
/**
* Binds the given function across each element of this list and the given list with a final
* join.
*
* @param lb A given list to bind the given function with.
* @param f The function to apply to each element of this list and the given list.
* @return A new list after performing the map, then final join.
*/
public final <B, C> List<C> bind(final List<B> lb, final F<A, F<B, C>> f) {
return lb.apply(map(f));
}
/**
* Binds the given function across each element of this list and the given list with a final
* join.
*
* @param lb A given list to bind the given function with.
* @param f The function to apply to each element of this list and the given list.
* @return A new list after performing the map, then final join.
*/
public final <B, C> List<C> bind(final List<B> lb, final F2<A, B, C> f) {
return bind(lb, curry(f));
}
/**
* Promotes the given function of arity-2 to a function on lists.
*
* @param f The function to promote to a function on lists.
* @return The given function, promoted to operate on lists.
*/
public static <A, B, C> F<List<A>, F<List<B>, List<C>>> liftM2(final F<A, F<B, C>> f) {
return curry(new F2<List<A>, List<B>, List<C>>() {
public List<C> f(final List<A> as, final List<B> bs) {
return as.bind(bs, f);
}
});
}
/**
* Binds the given function across each element of this list and the given lists with a final
* join.
*
* @param lb A given list to bind the given function with.
* @param lc A given list to bind the given function with.
* @param f The function to apply to each element of this list and the given lists.
* @return A new list after performing the map, then final join.
*/
public final <B, C, D> List<D> bind(final List<B> lb, final List<C> lc, final F<A, F<B, F<C, D>>> f) {
return lc.apply(bind(lb, f));
}
/**
* Binds the given function across each element of this list and the given lists with a final
* join.
*
* @param lb A given list to bind the given function with.
* @param lc A given list to bind the given function with.
* @param ld A given list to bind the given function with.
* @param f The function to apply to each element of this list and the given lists.
* @return A new list after performing the map, then final join.
*/
public final <B, C, D, E> List<E> bind(final List<B> lb, final List<C> lc, final List<D> ld,
final F<A, F<B, F<C, F<D, E>>>> f) {
return ld.apply(bind(lb, lc, f));
}
/**
* Binds the given function across each element of this list and the given lists with a final
* join.
*
* @param lb A given list to bind the given function with.
* @param lc A given list to bind the given function with.
* @param ld A given list to bind the given function with.
* @param le A given list to bind the given function with.
* @param f The function to apply to each element of this list and the given lists.
* @return A new list after performing the map, then final join.
*/
public final <B, C, D, E, F$> List<F$> bind(final List<B> lb, final List<C> lc, final List<D> ld, final List<E> le,
final F<A, F<B, F<C, F<D, F<E, F$>>>>> f) {
return le.apply(bind(lb, lc, ld, f));
}
/**
* Binds the given function across each element of this list and the given lists with a final
* join.
*
* @param lb A given list to bind the given function with.
* @param lc A given list to bind the given function with.
* @param ld A given list to bind the given function with.
* @param le A given list to bind the given function with.
* @param lf A given list to bind the given function with.
* @param f The function to apply to each element of this list and the given lists.
* @return A new list after performing the map, then final join.
*/
public final <B, C, D, E, F$, G> List<G> bind(final List<B> lb, final List<C> lc, final List<D> ld, final List<E> le,
final List<F$> lf, final F<A, F<B, F<C, F<D, F<E, F<F$, G>>>>>> f) {
return lf.apply(bind(lb, lc, ld, le, f));
}
/**
* Binds the given function across each element of this list and the given lists with a final
* join.
*
* @param lb A given list to bind the given function with.
* @param lc A given list to bind the given function with.
* @param ld A given list to bind the given function with.
* @param le A given list to bind the given function with.
* @param lf A given list to bind the given function with.
* @param lg A given list to bind the given function with.
* @param f The function to apply to each element of this list and the given lists.
* @return A new list after performing the map, then final join.
*/
public final <B, C, D, E, F$, G, H> List<H> bind(final List<B> lb, final List<C> lc, final List<D> ld, final List<E> le,
final List<F$> lf, final List<G> lg,
final F<A, F<B, F<C, F<D, F<E, F<F$, F<G, H>>>>>>> f) {
return lg.apply(bind(lb, lc, ld, le, lf, f));
}
/**
* Binds the given function across each element of this list and the given lists with a final
* join.
*
* @param lb A given list to bind the given function with.
* @param lc A given list to bind the given function with.
* @param ld A given list to bind the given function with.
* @param le A given list to bind the given function with.
* @param lf A given list to bind the given function with.
* @param lg A given list to bind the given function with.
* @param lh A given list to bind the given function with.
* @param f The function to apply to each element of this list and the given lists.
* @return A new list after performing the map, then final join.
*/
public final <B, C, D, E, F$, G, H, I> List<I> bind(final List<B> lb, final List<C> lc, final List<D> ld, final List<E> le,
final List<F$> lf, final List<G> lg, final List<H> lh,
final F<A, F<B, F<C, F<D, F<E, F<F$, F<G, F<H, I>>>>>>>> f) {
return lh.apply(bind(lb, lc, ld, le, lf, lg, f));
}
/**
* Performs a bind across each list element, but ignores the element value each time.
*
* @param bs The list to apply in the final join.
* @return A new list after the final join.
*/
public final <B> List<B> sequence(final List<B> bs) {
final F<A, List<B>> c = constant(bs);
return bind(c);
}
/**
* Performs function application within a list (applicative functor pattern).
*
* @param lf The list of functions to apply.
* @return A new list after applying the given list of functions through this list.
*/
public final <B> List<B> apply(final List<F<A, B>> lf) {
return lf.bind(new F<F<A, B>, List<B>>() {
public List<B> f(final F<A, B> f) {
return map(f);
}
});
}
/**
* Appends the given list to this list.
*
* @param as The list to append to this one.
* @return A new list that has appended the given list.
*/
public final List<A> append(final List<A> as) {
return fromList(this).append(as).toList();
}
/**
* Performs a right-fold reduction across this list. This function uses O(length) stack space.
*
* @param f The function to apply on each element of the list.
* @param b The beginning value to start the application from.
* @return The final result after the right-fold reduction.
*/
public final <B> B foldRight(final F<A, F<B, B>> f, final B b) {
return isEmpty() ? b : f.f(head()).f(tail().foldRight(f, b));
}
/**
* Performs a right-fold reduction across this list. This function uses O(length) stack space.
*
* @param f The function to apply on each element of the list.
* @param b The beginning value to start the application from.
* @return The final result after the right-fold reduction.
*/
public final <B> B foldRight(final F2<A, B, B> f, final B b) {
return foldRight(curry(f), b);
}
/**
* Performs a right-fold reduction across this list in O(1) stack space.
* @param f The function to apply on each element of the list.
* @param b The beginning value to start the application from.
* @return A Trampoline containing the final result after the right-fold reduction.
*/
public final <B> Trampoline<B> foldRightC(final F2<A, B, B> f, final B b) {
return Trampoline.suspend(new P1<Trampoline<B>>() {
public Trampoline<B> _1() {
return isEmpty() ? Trampoline.pure(b) : tail().foldRightC(f, b).map(f.f(head()));
}
});
}
/**
* Performs a left-fold reduction across this list. This function runs in constant space.
*
* @param f The function to apply on each element of the list.
* @param b The beginning value to start the application from.
* @return The final result after the left-fold reduction.
*/
public final <B> B foldLeft(final F<B, F<A, B>> f, final B b) {
B x = b;
for (List<A> xs = this; !xs.isEmpty(); xs = xs.tail()) {
x = f.f(x).f(xs.head());
}
return x;
}
/**
* Performs a left-fold reduction across this list. This function runs in constant space.
*
* @param f The function to apply on each element of the list.
* @param b The beginning value to start the application from.
* @return The final result after the left-fold reduction.
*/
public final <B> B foldLeft(final F2<B, A, B> f, final B b) {
return foldLeft(curry(f), b);
}
/**
* Takes the first 2 elements of the list and applies the function to them,
* then applies the function to the result and the third element and so on.
*
* @param f The function to apply on each element of the list.
* @return The final result after the left-fold reduction.
*/
public final A foldLeft1(final F2<A, A, A> f) {
return foldLeft1(curry(f));
}
/**
* Takes the first 2 elements of the list and applies the function to them,
* then applies the function to the result and the third element and so on.
*
* @param f The function to apply on each element of the list.
* @return The final result after the left-fold reduction.
*/
public final A foldLeft1(final F<A, F<A, A>> f) {
if (isEmpty())
throw error("Undefined: foldLeft1 on empty list");
return tail().foldLeft(f, head());
}
/**
* Reverse this list in constant stack space.
*
* @return A new list that is the reverse of this one.
*/
public final List<A> reverse() {
return foldLeft(new F<List<A>, F<A, List<A>>>() {
public F<A, List<A>> f(final List<A> as) {
return new F<A, List<A>>() {
public List<A> f(final A a) {
return cons(a, as);
}
};
}
}, List.<A>nil());
}
/**
* Returns the element at the given index if it exists, fails otherwise.
*
* @param i The index at which to get the element to return.
* @return The element at the given index if it exists, fails otherwise.
*/
public final A index(final int i) {
if (i < 0 || i > length() - 1)
throw error("index " + i + " out of range on list with length " + length());
else {
List<A> xs = this;
for (int c = 0; c < i; c++) {
xs = xs.tail();
}
return xs.head();
}
}
/**
* Takes the given number of elements from the head of this list if they are available.
*
* @param i The maximum number of elements to take from this list.
* @return A new list with a length the same, or less than, this list.
*/
public final List<A> take(final int i) {
return i <= 0 || isEmpty() ? List.<A>nil() : cons(head(), tail().take(i - 1));
}
/**
* Drops the given number of elements from the head of this list if they are available.
*
* @param i The number of elements to drop from the head of this list.
* @return A list with a length the same, or less than, this list.
*/
public final List<A> drop(final int i) {
int c = 0;
List<A> xs = this;
for (; xs.isNotEmpty() && c < i; xs = xs.tail())
c++;
return xs;
}
/**
* Splits this list into two lists at the given index. If the index goes out of bounds, then it is
* normalised so that this function never fails.
*
* @param i The index at which to split this list in two parts.
* @return A pair of lists split at the given index of this list.
*/
public final P2<List<A>, List<A>> splitAt(final int i) {
P2<List<A>, List<A>> s = p(List.<A>nil(), List.<A>nil());
int c = 0;
for (List<A> xs = this; xs.isNotEmpty(); xs = xs.tail()) {
final A h = xs.head();
s = c < i ? s.map1(new F<List<A>, List<A>>() {
public List<A> f(final List<A> as) {
return as.snoc(h);
}
}) : s.map2(new F<List<A>, List<A>>() {
public List<A> f(final List<A> as) {
return as.snoc(h);
}
});
c++;
}
return s;
}
/**
* Splits this list into lists of the given size. If the size of this list is not evenly divisible by
* the given number, the last partition will contain the remainder.
*
* @param n The size of the partitions into which to split this list.
* @return A list of sublists of this list, of at most the given size.
*/
public final List<List<A>> partition(final int n) {
if (n < 1)
throw error("Can't create list partitions shorter than 1 element long.");
if (isEmpty())
throw error("Partition on empty list.");
return unfold(new F<List<A>, Option<P2<List<A>, List<A>>>>() {
public Option<P2<List<A>, List<A>>> f(final List<A> as) {
return as.isEmpty() ? Option.<P2<List<A>, List<A>>>none() : some(as.splitAt(n));
}
}, this);
}
/**
* Returns the list of initial segments of this list, shortest first.
*
* @return The list of initial segments of this list, shortest first.
*/
public final List<List<A>> inits() {
List<List<A>> s = single(List.<A>nil());
if (isNotEmpty())
s = s.append(tail().inits().map(List.<A>cons().f(head())));
return s;
}
/**
* Returns the list of final segments of this list, longest first.
*
* @return The list of final segments of this list, longest first.
*/
public final List<List<A>> tails() {
return isEmpty() ? single(List.<A>nil()) : cons(this, tail().tails());
}
/**
* Sorts this list using the given order over elements using a <em>merge sort</em> algorithm.
*
* @param o The order over the elements of this list.
* @return A sorted list according to the given order.
*/
public final List<A> sort(final Ord<A> o) {
if (isEmpty())
return nil();
else if (tail().isEmpty())
return this;
else {
final class Merge<A> {
List<A> merge(List<A> xs, List<A> ys, final Ord<A> o) {
final Buffer<A> buf = empty();
while (true) {
if (xs.isEmpty()) {
buf.append(ys);
break;
}
if (ys.isEmpty()) {
buf.append(xs);
break;
}
final A x = xs.head();
final A y = ys.head();
if (o.isLessThan(x, y)) {
buf.snoc(x);
xs = xs.tail();
} else {
buf.snoc(y);
ys = ys.tail();
}
}
return buf.toList();
}
}
final P2<List<A>, List<A>> s = splitAt(length() / 2);
return new Merge<A>().merge(s._1().sort(o), s._2().sort(o), o);
}
}
/**
* Zips this list with the given list using the given function to produce a new list. If this list
* and the given list have different lengths, then the longer list is normalised so this function
* never fails.
*
* @param bs The list to zip this list with.
* @param f The function to zip this list and the given list with.
* @return A new list with a length the same as the shortest of this list and the given list.
*/
public final <B, C> List<C> zipWith(List<B> bs, final F<A, F<B, C>> f) {
final Buffer<C> buf = empty();
List<A> as = this;
while (as.isNotEmpty() && bs.isNotEmpty()) {
buf.snoc(f.f(as.head()).f(bs.head()));
as = as.tail();
bs = bs.tail();
}
return buf.toList();
}
/**
* Zips this list with the given list using the given function to produce a new list. If this list
* and the given list have different lengths, then the longer list is normalised so this function
* never fails.
*
* @param bs The list to zip this list with.
* @param f The function to zip this list and the given list with.
* @return A new list with a length the same as the shortest of this list and the given list.
*/
public final <B, C> List<C> zipWith(final List<B> bs, final F2<A, B, C> f) {
return zipWith(bs, curry(f));
}
/**
* Provides a first-class version of zipWith
*
* @return The first-class version of zipWith
*/
public static <A, B, C> F<List<A>, F<List<B>, F<F<A, F<B, C>>, List<C>>>> zipWith() {
return curry(new F3<List<A>, List<B>, F<A, F<B, C>>, List<C>>() {
public List<C> f(final List<A> as, final List<B> bs, final F<A, F<B, C>> f) {
return as.zipWith(bs, f);
}
});
}
/**
* Zips this list with the given list to produce a list of pairs. If this list and the given list
* have different lengths, then the longer list is normalised so this function never fails.
*
* @param bs The list to zip this list with.
* @return A new list with a length the same as the shortest of this list and the given list.
*/
public final <B> List<P2<A, B>> zip(final List<B> bs) {
final F<A, F<B, P2<A, B>>> __2 = p2();
return zipWith(bs, __2);
}
/**
* The first-class version of the zip function.
*
* @return A function that zips the given lists to produce a list of pairs.
*/
public static <A, B> F<List<A>, F<List<B>, List<P2<A, B>>>> zip() {
return curry(new F2<List<A>, List<B>, List<P2<A, B>>>() {
public List<P2<A, B>> f(final List<A> as, final List<B> bs) {
return as.zip(bs);
}
});
}
/**
* Zips this list with the index of its element as a pair.
*
* @return A new list with the same length as this list.
*/
public final List<P2<A, Integer>> zipIndex() {
return zipWith(range(0, length()), new F<A, F<Integer, P2<A, Integer>>>() {
public F<Integer, P2<A, Integer>> f(final A a) {
return new F<Integer, P2<A, Integer>>() {
public P2<A, Integer> f(final Integer i) {
return p(a, i);
}
};
}
});
}
/**
* Appends (snoc) the given element to this list to produce a new list.
*
* @param a The element to append to this list.
* @return A new list with the given element appended.
*/
public final List<A> snoc(final A a) {
return fromList(this).snoc(a).toList();
}
/**
* Returns <code>true</code> if the predicate holds for all of the elements of this list,
* <code>false</code> otherwise (<code>true</code> for the empty list).
*
* @param f The predicate function to test on each element of this list.
* @return <code>true</code> if the predicate holds for all of the elements of this list,
* <code>false</code> otherwise.
*/
public final boolean forall(final F<A, Boolean> f) {
return isEmpty() || f.f(head()) && tail().forall(f);
}
/**
* Returns <code>true</code> if the predicate holds for at least one of the elements of this list,
* <code>false</code> otherwise (<code>false</code> for the empty list).
*
* @param f The predicate function to test on the elements of this list.
* @return <code>true</code> if the predicate holds for at least one of the elements of this
* list.
*/
public final boolean exists(final F<A, Boolean> f) {
return find(f).isSome();
}