-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSek.java
More file actions
1665 lines (1521 loc) · 64.9 KB
/
Sek.java
File metadata and controls
1665 lines (1521 loc) · 64.9 KB
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 com.tinyield;
import kotlin.Pair;
import kotlin.Unit;
import kotlin.collections.Grouping;
import kotlin.collections.IndexedValue;
import kotlin.random.Random;
import kotlin.sequences.Sequence;
import kotlin.sequences.SequencesKt;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Spliterators;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.BinaryOperator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.function.ToDoubleFunction;
import java.util.function.ToIntFunction;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import static kotlin.sequences.SequencesKt.generateSequence;
import static kotlin.sequences.SequencesKt.sequenceOf;
/**
* Represents a bi-function that also accepts an int index.
*
* <p>This is a <a href="package-summary.html">functional interface</a>
* whose functional method is {@link #apply(int, Object, Object)}.
*
* @param <T> the type of the first argument to the function
* @param <U> the type of the second argument to the function
* @param <R> the type of the result of the function
*/
@FunctionalInterface
interface IndexedBiFunction<T,U,R> {
/**
* Applies this function to the given arguments.
*
* @param index
* @param t the first function argument
* @param u the second function argument
* @return the function result
*/
R apply(int index, T t, U u);
}
@SuppressWarnings("squid:S3252")
public interface Sek<T> extends Sequence<T> {
/**
* Creates a sequence that returns the specified values.
*
* @param elements values to be yielded by the sequence
* @return a Sek instance that will yield the provided {@param elements}
*/
@SafeVarargs
static <T> Sek<T> of(T... elements) {
return sequenceOf(elements)::iterator;
}
/**
* Creates a sequence that returns the specified values.
*
* @param elements values to be yielded by the sequence
* @return a Sek instance that will yield the provided {@param elements}
*/
static <T> Sek<T> of(Iterable<T> elements) {
return elements::iterator;
}
/**
* Returns a sequence which invokes the function to calculate the next value on each iteration until the function returns `null`.
*
* @param gen Supplier that generates values to be yielded by this sequence
* @return a Sek instance that will yield the elements provided by {@param gen}
* <p>
* The returned sequence is constrained to be iterated only once.
* <p>
* see {@code constrainOnce}
*/
static <T> Sek<T> generate(Supplier<T> gen) {
return generateSequence(gen::get)::iterator;
}
/**
* @return an empty sequence.
*/
static <T> Sek<T> empty() {
return SequencesKt.<T>emptySequence()::iterator;
}
/**
* @param predicate used to test elements of this {@code Sek}
* @return true if all elements match the given {@param predicate}, false otherwise
* <p>
* The operation is _terminal_.
*/
default boolean all(Predicate<? super T> predicate) {
return SequencesKt.all(this, predicate::test);
}
/**
* @return true if sequence has at least one element, false otherwise
* <p>
* The operation is _terminal_.
*/
default boolean any() {
return SequencesKt.any(this);
}
/**
* @param predicate used to test elements of this {@code Sek}
* @return true if sequence has at least one element matches the given {@param predicate}, false otherwise
* <p>
* The operation is _terminal_.
*
*/
default boolean any(Predicate<? super T> predicate) {
return SequencesKt.any(this, predicate::test);
}
/**
* Creates an {@link Iterable} instance that wraps the original {@code Sek} returning its elements when being iterated.
* @return an {@link Iterable} wrapping the original {@code Sek}.
*/
default Iterable<T> asIterable() {
return SequencesKt.asIterable(this);
}
/**
* @return this {@code Sek} as a Kotlin {@link Sequence}.
*/
default Sequence<T> asSequence() {
return this;
}
/**
* @return this {@code Sek} as a {@link Stream}.
*/
default Stream<T> asStream() {
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(this.iterator(), 0),false);
}
/**
* @param transform Function that maps elements into key-value pairs.
* @return a {@link Map} containing key-value pairs provided by {@param transform} function
* applied to elements of the given {@code Sek}.
* <p>
* If any of two pairs would have the same key the last one gets added to the map.
* <p>
* The returned map preserves the entry iteration order of the original sequence.
* <p>
* The operation is _terminal_.
*
*/
default <K, V> Map<K, V> associate(Function<? super T, Pair<K, V>> transform) {
return SequencesKt.associate(this, transform::apply);
}
/**
* @param keySelector Function that maps elements into a key
* @return a {@link Map} containing the elements from the given sequence indexed by the key
* returned from {@param keySelector} function applied to each element.
* <p>
* If any two elements would have the same key returned by {@param keySelector} the last one gets added to the map.
* <p>
* The returned map preserves the entry iteration order of the original sequence.
* <p>
* The operation is _terminal_.
*
*/
default <K> Map<K, T> associateBy(Function<? super T, ? extends K> keySelector) {
return SequencesKt.associateBy(this, keySelector::apply);
}
/**
* @return a {@link Map} containing the values provided by {@param valueTransform} and indexed by {@param keySelector} functions applied to elements of the given sequence.
* <p>
* If any two elements would have the same key returned by {@param keySelector} the last one gets added to the map.
* <p>
* The returned map preserves the entry iteration order of the original sequence.
* <p>
* The operation is _terminal_.
*
*/
default <K, V> Map<K, V> associateBy(Function<? super T, ? extends K> keySelector, Function<? super T, ? extends V> valueTransform) {
return SequencesKt.associateBy(this, keySelector::apply, valueTransform::apply);
}
/**
* Populates and returns the {@param destination} mutable map with key-value pairs,
* where key is provided by the {@param keySelector} function applied to each element of the given sequence
* and value is the element itself.
* <p>
* If any two elements would have the same key returned by {@param keySelector} the last one gets added to the map.
* <p>
* The operation is _terminal_.
*
*/
default <K, M extends Map<K, T>> M associateByTo(M destination, Function<? super T, ? extends K> keySelector) {
return SequencesKt.associateByTo(this, destination, keySelector::apply);
}
/**
* Populates and returns the {@param destination} mutable map with key-value pairs,
* where key is provided by the {@param keySelector} function and
* and value is provided by the {@param valueTransform} function applied to elements of the given sequence.
* <p>
* If any two elements would have the same key returned by {@param keySelector} the last one gets added to the map.
* <p>
* The operation is _terminal_.
*
*/
default <K, V, M extends Map<K, V>> M associateByTo(M destination, Function<? super T, ? extends K> keySelector, Function<? super T, ? extends V> valueTransform) {
return SequencesKt.associateByTo(this, destination, keySelector::apply, valueTransform::apply);
}
/**
* Populates and returns the {@param destination} mutable map with key-value pairs
* provided by {@param transform} function applied to each element of the given sequence.
* <p>
* If any of two pairs would have the same key the last one gets added to the map.
* <p>
* The operation is _terminal_.
*
*/
default <K, V, M extends Map<K, V>> M associateTo(M destination, Function<? super T, Pair<K, V>> transform) {
return SequencesKt.associateTo(this, destination, transform::apply);
}
/**
* @return a {@link Map} where keys are elements from the given sequence and values are
* produced by the {@param valueSelector} function applied to each element.
* <p>
* If any two elements are equal, the last one gets added to the map.
* <p>
* The returned map preserves the entry iteration order of the original sequence.
* <p>
* The operation is _terminal_.
*
*/
default <V> Map<T, V> associateWith(Function<? super T, ? extends V> valueSelector) {
return SequencesKt.associateWith(this, valueSelector::apply);
}
/**
* Populates and returns the {@param destination} mutable map with key-value pairs for each element of the given sequence,
* where key is the element itself and value is provided by the {@param valueSelector} function applied to that key.
* <p>
* If any two elements are equal, the last one overwrites the former value in the map.
* <p>
* The operation is _terminal_.
*
*/
default <V, M extends Map<T, V>> M associateWithTo(M destination, Function<? super T, ? extends V> valueSelector) {
return SequencesKt.associateWithTo(this, destination, valueSelector::apply);
}
/**
* Splits this sequence into a sequence of lists each not exceeding the given {@param size}.
* <p>
* The last list in the resulting sequence may have less elements than the given {@param size}.
*
* @param size the number of elements to take in each list, must be positive and can be greater than the number of elements in this sequence.
* <p>
* The operation is _intermediate_ and _stateful_.
*/
default Sek<List<T>> chunked(int size) {
return SequencesKt.chunked(this, size)::iterator;
}
/**
* Splits this sequence into several lists each not exceeding the given {@param size}
* and applies the given {@param transform} function to an each.
*
* @param size the number of elements to take in each list, must be positive and can be greater than the number of elements in this sequence.
* <p>
* The operation is _intermediate_ and _stateful_.
* @return sequence of results of the {@param transform} applied to an each list.
* <p>
* Note that the list passed to the {@param transform} function is ephemeral and is valid only inside that function.
* You should not store it or allow it to escape in some way, unless you made a snapshot of it.
* The last list may have less elements than the given {@param size}.
*/
default <R> Sek<R> chunked(int size, Function<? super List<? extends T>, ? extends R> transform) {
return SequencesKt.<T, R>chunked(this, size, transform::apply)::iterator;
}
/**
* @return a wrapper sequence that provides values of this sequence, but ensures it can be iterated only one time.
* <p>
* The operation is _intermediate_ and _stateless_.
* <p>
* {@link IllegalStateException} is thrown on iterating the returned sequence from the second time.
*/
default Sek<T> constrainOnce() {
return SequencesKt.constrainOnce(this)::iterator;
}
/**
* @return `true` if {@param element} is found in the sequence.
* <p>
* The operation is _terminal_.
*/
default boolean contains(T element) {
return SequencesKt.contains(this, element);
}
/**
* @return the number of elements in this sequence.
* <p>
* The operation is _terminal_.
*/
default int count() {
return SequencesKt.count(this);
}
/**
* @return the number of elements matching the given {@param predicate}.
* <p>
* The operation is _terminal_.
*/
default int count(Predicate<? super T> predicate) {
return SequencesKt.count(this, predicate::test);
}
/**
* @return a sequence containing only distinct elements from the given sequence.
* <p>
* Among equal elements of the given sequence, only the first one will be present in the resulting sequence.
* The elements in the resulting sequence are in the same order as they were in the source sequence.
* <p>
* The operation is _intermediate_ and _stateful_.
*
*/
default Sek<T> distinct() {
return SequencesKt.distinct(this)::iterator;
}
/**
* @param selector Function that calculates the key for each element
* @return a sequence containing only elements from the given sequence
* having distinct keys returned by the given {@param selector} function.
* <p>
* Among elements of the given sequence with equal keys, only the first one will be present in the resulting sequence.
* The elements in the resulting sequence are in the same order as they were in the source sequence.
* <p>
* The operation is _intermediate_ and _stateful_.
*
*/
default <K> Sek<T> distinctBy(Function<? super T, ? extends K> selector) {
return SequencesKt.distinctBy(this, selector::apply)::iterator;
}
/**
* @param n number of starting elements to discard
* @return a sequence containing all elements except first {@param n} elements.
* <p>
* The operation is _intermediate_ and _stateless_.
*
* @throws IllegalArgumentException if {@param n} is negative.
*/
default Sek<T> drop(int n) {
return SequencesKt.drop(this, n)::iterator;
}
/**
* @return a sequence containing all elements except first elements that satisfy the given {@param predicate}.
* <p>
* The operation is _intermediate_ and _stateless_.
*
*/
default Sek<T> dropWhile(Predicate<? super T> predicate) {
return SequencesKt.dropWhile(this, predicate::test)::iterator;
}
/**
* @return an element at the given {@param index} or throws an {@link IndexOutOfBoundsException} if the {@param index} is out of bounds of this sequence.
* <p>
* The operation is _terminal_.
*
*/
default T elementAt(int index) {
return SequencesKt.elementAt(this, index);
}
/**
* @return an element at the given {@param index} or the result of calling the {@param defaultValue} function if the {@param index} is out of bounds of this sequence.
* <p>
* The operation is _terminal_.
*
*/
default T elementAtOrElse(int index, IntFunction<? extends T> defaultValue) {
return SequencesKt.elementAtOrElse(this, index, defaultValue::apply);
}
/**
* @return an element at the given {@param index} or `null` if the {@param index} is out of bounds of this sequence.
* <p>
* The operation is _terminal_.
*
*/
default T elementAtOrNull(int index) {
return SequencesKt.elementAtOrNull(this, index);
}
/**
* @return a sequence containing only elements matching the given {@param predicate}.
* <p>
* The operation is _intermediate_ and _stateless_.
*
*/
default Sek<T> filter(Predicate<? super T> predicate) {
return SequencesKt.filter(this, predicate::test)::iterator;
}
/**
* Appends all elements matching the given {@param predicate} to the given {@param destination}.
*
* @param destination Collection in which the elements will be stored
* @param predicate Predicate that takes an element and returns the result of
* the predicate evaluation on the element.
* <p>
* The operation is _terminal_.
*
*/
default <C extends Collection<T>> C filterTo(C destination, Predicate<? super T> predicate) {
return SequencesKt.filterTo(this, destination, predicate::test);
}
/**
* @return a sequence containing only elements matching the given {@param predicate}.
*
* @param predicate BiPredicate that takes the index of an element and the element itself
* and returns the result of predicate evaluation on the element.
* <p>
* The operation is _intermediate_ and _stateless_.
*/
default Sek<T> filterIndexed(BiPredicate<Integer, ? super T> predicate) {
return SequencesKt.filterIndexed(this, predicate::test)::iterator;
}
/**
* Appends all elements matching the given predicate to the given destination.
*
* @param destination Collection in which the elements will be stored
* @param predicate BiPredicate that takes the index of an element and the element itself
* and returns the result of predicate evaluation on the element.
* <p>
* The operation is _terminal_.
*/
default <C extends Collection<T>> C filterIndexedTo(C destination, BiPredicate<Integer, ? super T> predicate) {
return SequencesKt.filterIndexedTo(this, destination, predicate::test);
}
/**
* @return a sequence containing all elements that are instances of specified class.
* <p>
* The operation is _intermediate_ and _stateless_.
*
*/
default <R> Sek<R> filterIsInstance(Class<R> klass) {
return SequencesKt.filterIsInstance(this, klass)::iterator;
}
/**
* Appends all elements that are instances of specified class to the given {@param destination}.
* <p>
* The operation is _terminal_.
*
*/
default <R, C extends Collection<R>> C filterIsInstanceTo(C destination, Class<R> klass) {
return SequencesKt.filterIsInstanceTo(this, destination, klass);
}
/**
* @return a sequence containing all elements not matching the given {@param predicate}.
* <p>
* The operation is _intermediate_ and _stateless_.
*
*/
default Sek<T> filterNot(Predicate<? super T> predicate) {
return SequencesKt.filterNot(this, predicate::test)::iterator;
}
/**
* Appends all elements not matching the given {@param predicate} to the given {@param destination}.
* <p>
* The operation is _terminal_.
*
*/
default <C extends Collection<T>> C filterNotTo(C destination, Predicate<? super T> predicate) {
return SequencesKt.filterNotTo(this, destination, predicate::test);
}
/**
* @return a sequence containing all elements that are not `null`.
* <p>
* The operation is _intermediate_ and _stateless_.
*
*/
default Sek<T> filterNotNull() {
return SequencesKt.filterNotNull(this)::iterator;
}
/**
* Appends all elements that are not `null` to the given {@param destination}.
* <p>
* The operation is _terminal_.
*
*/
default <C extends Collection<T>> C filterNotNullTo(C destination) {
return SequencesKt.filterNotNullTo(this, destination);
}
/**
* @return first element.
*
* @throws java.util.NoSuchElementException if the sequence is empty.
* <p>
* The operation is _terminal_.
*/
default T first() {
return SequencesKt.first(this);
}
/**
* @return the first element matching the given {@param predicate}.
*
* @throws java.util.NoSuchElementException if no such element is found.
* <p>
* The operation is _terminal_.
*/
default T first(Predicate<? super T> predicate) {
return SequencesKt.first(this, predicate::test);
}
/**
* @return the first element, or `null` if the sequence is empty.
* <p>
* The operation is _terminal_.
*/
default T firstOrNull() {
return SequencesKt.firstOrNull(this);
}
/**
* @return the first element matching the given {@param predicate}, or `null` if element was not found.
* <p>
* The operation is _terminal_.
*/
default T firstOrNull(Predicate<? super T> predicate) {
return SequencesKt.firstOrNull(this, predicate::test);
}
/**
* @return a sequence of all elements from all sequences in this sequence.
* @throws java.lang.ClassCastException if T is not a Sequence<R>
*
* The operation is _intermediate_ and _stateless_.
*/
@SuppressWarnings("unchecked")
default <R> Sek<R> flatten() {
return SequencesKt.flatten((Sequence<Sequence<R>>)this)::iterator;
}
/**
* @return a single sequence of all elements from results of {@param transform} function being invoked on each element of original sequence.
* <p>
* The operation is _intermediate_ and _stateless_.
*
*/
default <R> Sek<R> flatMap(Function<? super T, Sequence<R>> transform) {
return SequencesKt.flatMap(this, transform::apply)::iterator;
}
/**
* Appends all elements yielded from results of {@param transform} function being invoked on each element of original sequence, to the given {@param destination}.
* <p>
* The operation is _terminal_.
*/
default <R, C extends Collection<? super R>> C flatMapTo(C destination, Function<? super T, Sequence<R>> transform) {
return SequencesKt.flatMapTo(this, destination, transform::apply);
}
/**
* Accumulates value starting with {@param initial} value and applying {@param operation} from left to right
* to current accumulator value and each element.
* <p>
* @return the specified {@param initial} value if the sequence is empty.
*
* @param operation function that takes current accumulator value and an element, and calculates the next accumulator value.
* <p>
* The operation is _terminal_.
*/
default <R> R fold(R initial, BiFunction<? super R, ? super T, ? extends R> operation) {
return SequencesKt.fold(this, initial, operation::apply);
}
/**
* Accumulates value starting with {@param initial} value and applying {@param operation} from left to right
* to current accumulator value and each element with its index in the original sequence.
* <p>
* @return the specified {@param initial} value if the sequence is empty.
*
* @param operation function that takes the index of an element, current accumulator value
* and the element itself, and calculates the next accumulator value.
* <p>
* The operation is _terminal_.
*/
default <R> R foldIndexed(R initial, IndexedBiFunction<? super R, ? super T, ? extends R> operation) {
return SequencesKt.foldIndexed(this, initial, operation::apply);
}
/**
* Performs the given {@param action} on each element.
* <p>
* The operation is _terminal_.
*/
default void forEach(Consumer<? super T> action) {
SequencesKt.forEach(this, elem -> {
action.accept(elem);
return Unit.INSTANCE;
});
}
/**
* Performs the given {@param action} on each element, providing sequential index with the element.
*
* @param action function that takes the index of an element and the element itself
* and performs the action on the element.
* <p>
* The operation is _terminal_.
*/
default void forEachIndexed(BiConsumer<Integer, ? super T> action) {
SequencesKt.forEachIndexed(this, (index, elem) -> {
action.accept(index, elem);
return Unit.INSTANCE;
});
}
/**
* Groups elements of the original sequence by the key returned by the given {@param keySelector} function
* applied to each element and returns a map where each group key is associated with a list of corresponding elements.
*
* The returned map preserves the entry iteration order of the keys produced from the original sequence.
*
* The operation is _terminal_.
*
*/
default <K> Map<K, List<T>> groupBy(Function<? super T, ? extends K> keySelector) {
return SequencesKt.groupBy(this, keySelector::apply);
}
/**
* Groups elements of the original sequence by the key returned by the given {@param keySelector} function
* applied to each element and puts to the {@param destination} map each group key associated with a list of corresponding elements.
*
* @return The {@param destination} map.
*
* The operation is _terminal_.
*
*/
default <K, M extends Map<K, List<T>>> M groupByTo(M destination, Function<? super T, ? extends K> keySelector) {
return SequencesKt.groupByTo(this, destination, keySelector::apply);
}
/**
* Groups values returned by the {@param valueTransform} function applied to each element of the original sequence
* by the key returned by the given {@param keySelector} function applied to the element
* and returns a map where each group key is associated with a list of corresponding values.
*
* The returned map preserves the entry iteration order of the keys produced from the original sequence.
*
* The operation is _terminal_.
*
*/
default <K, V> Map<K, List<V>> groupBy(Function<? super T, ? extends K> keySelector, Function<? super T, ? extends V> valueTransform) {
return SequencesKt.groupBy(this, keySelector::apply, valueTransform::apply);
}
/**
* Groups values returned by the {@param valueTransform} function applied to each element of the original sequence
* by the key returned by the given {@param keySelector} function applied to the element
* and puts to the {@param destination} map each group key associated with a list of corresponding values.
*
* @return The {@param destination} map.
*
* The operation is _terminal_.
*
*/
default <K, V, M extends Map<K, List<V>>> M groupByTo(M destination, Function<? super T, ? extends K> keySelector, Function<? super T, ? extends V> valueTransform) {
return SequencesKt.groupByTo(this, destination, keySelector::apply, valueTransform::apply);
}
/**
* Creates a {@link Grouping} source from a sequence to be used later with one of group-and-fold operations
* using the specified {@param keySelector} function to extract a key from each element.
*
* The operation is _intermediate_ and _stateless_.
*
*/
default <K> Grouping<T,K> groupingBy(Function<? super T, ? extends K> keySelector) {
return SequencesKt.groupingBy(this, keySelector::apply);
}
/**
* @return a sequence that iterates through the elements either of this sequence
* or, if this sequence turns out to be empty, of the sequence returned by {@param defaultValue} function.
*
*/
default Sek<T> ifEmpty(Supplier<Sek<? extends T>> defaultValue) {
return SequencesKt.ifEmpty(this, defaultValue::get)::iterator;
}
/**
* @return first index of {@param element}, or -1 if the sequence does not contain element.
*
* The operation is _terminal_.
*/
default int indexOf(T element) {
return SequencesKt.indexOf(this, element);
}
/**
* @return index of the first element matching the given {@param predicate}, or -1 if the sequence does not contain such element.
*
* The operation is _terminal_.
*/
default int indexOfFirst(Predicate<? super T> predicate) {
return SequencesKt.indexOfFirst(this, predicate::test);
}
/**
* @return index of the last element matching the given {@param predicate}, or -1 if the sequence does not contain such element.
*
* The operation is _terminal_.
*/
default int indexOfLast(Predicate<? super T> predicate) {
return SequencesKt.indexOfLast(this, predicate::test);
}
/**
* Appends the string from all the elements separated using {@param separator} and using the given {@param prefix} and {@param postfix} if supplied.
*
* If the collection could be huge, you can specify a non-negative value of {@param limit}, in which case only the first {@param limit}
* elements will be appended, followed by the {@param truncated} string.
*
* The operation is _terminal_.
*
*/
default <A extends Appendable> A joinTo(A buffer, CharSequence separator, CharSequence prefix, CharSequence postfix, int limit, CharSequence truncated, Function<? super T, ? extends CharSequence> transform) {
return SequencesKt.joinTo(this, buffer, separator, prefix, postfix, limit, truncated, transform::apply);
}
/**
* Creates a string from all the elements separated using {@param separator} and using the given {@param prefix} and {@param postfix} if supplied.
*
* If the collection could be huge, you can specify a non-negative value of {@param limit}, in which case only the first {@param limit}
* elements will be appended, followed by the {@param truncated} string.
*
* The operation is _terminal_.
*
*/
default String joinToString(CharSequence separator, CharSequence prefix, CharSequence postfix, int limit, CharSequence truncated, Function<? super T, ? extends CharSequence> transform) {
return SequencesKt.joinToString(this, separator, prefix, postfix, limit, truncated, transform::apply);
}
/**
* @return the last element.
*
* The operation is _terminal_.
*
* @throws java.util.NoSuchElementException if the sequence is empty.
*
*/
default T last() {
return SequencesKt.last(this);
}
/**
* @return the last element matching the given {@param predicate}.
*
* The operation is _terminal_.
*
* @throws java.util.NoSuchElementException if no such element is found.
*
*/
default T last(Predicate<? super T> predicate) {
return SequencesKt.last(this, predicate::test);
}
/**
* @return last index of {@param element}, or -1 if the sequence does not contain element.
*
* The operation is _terminal_.
*/
default int lastIndexOf(T element) {
return SequencesKt.lastIndexOf(this, element);
}
/**
* @return the last element, or `null` if the sequence is empty.
*
* The operation is _terminal_.
*
*/
default T lastOrNull() {
return SequencesKt.lastOrNull(this);
}
/**
* @return the last element matching the given {@param predicate}, or `null` if no such element was found.
*
* The operation is _terminal_.
*
*/
default T lastOrNull(Predicate<? super T> predicate) {
return SequencesKt.lastOrNull(this, predicate::test);
}
/**
* @return a sequence containing the results of applying the given {@param transform} function
* to each element in the original sequence.
*
* The operation is _intermediate_ and _stateless_.
*
*/
default <R> Sek<R> map(Function<? super T, R> transform) {
return SequencesKt.map(this, transform::apply)::iterator;
}
/**
* Applies the given {@param transform} function to each element of the original sequence
* and appends the results to the given {@param destination}.
*
* The operation is _terminal_.
*/
default <R, C extends Collection<R>> C mapTo(C destination, Function<? super T, R> transform) {
return SequencesKt.mapTo(this, destination, transform::apply);
}
/**
* @return a sequence containing the results of applying the given {@param transform} function
* to each element and its index in the original sequence.
* @param transform function that takes the index of an element and the element itself
* and returns the result of the transform applied to the element.
*
* The operation is _intermediate_ and _stateless_.
*/
default <R> Sek<R> mapIndexed(BiFunction<Integer, ? super T, R> transform) {
return SequencesKt.mapIndexed(this, transform::apply)::iterator;
}
/**
* Applies the given {@param transform} function to each element and its index in the original sequence
* and appends the results to the given {@param destination}.
* @param transform function that takes the index of an element and the element itself
* and returns the result of the transform applied to the element.
*
* The operation is _terminal_.
*/
default <R, C extends Collection<R>> C mapIndexedTo(C destination, BiFunction<Integer, ? super T, R> transform) {
return SequencesKt.mapIndexedTo(this, destination, transform::apply);
}
/**
* @return a sequence containing only the non-null results of applying the given {@param transform} function
* to each element and its index in the original sequence.
* @param transform function that takes the index of an element and the element itself
* and returns the result of the transform applied to the element.
*
* The operation is _intermediate_ and _stateless_.
*/
default <R> Sek<R> mapIndexedNotNull(BiFunction<Integer, ? super T, R> transform) {
return SequencesKt.mapIndexedNotNull(this, transform::apply)::iterator;
}
/**
* Applies the given {@param transform} function to each element and its index in the original sequence
* and appends only the non-null results to the given {@param destination}.
* @param transform function that takes the index of an element and the element itself
* and returns the result of the transform applied to the element.
*
* The operation is _terminal_.
*/
default <R, C extends Collection<R>> C mapIndexedNotNullTo(C destination,BiFunction<Integer, ? super T, R> transform) {
return SequencesKt.mapIndexedNotNullTo(this, destination,transform::apply);
}
/**
* @return a sequence containing only the non-null results of applying the given {@param transform} function
* to each element in the original sequence.
*
* The operation is _intermediate_ and _stateless_.
*
*/
default <R> Sek<R> mapNotNull(Function<? super T, R> transform) {
return SequencesKt.mapNotNull(this, transform::apply)::iterator;
}
/**
* Applies the given {@param transform} function to each element in the original sequence
* and appends only the non-null results to the given {@param destination}.
*
* The operation is _terminal_.
*/
default <R, C extends Collection<R>> C mapNotNullTo(C destination,Function<? super T, R> transform) {
return SequencesKt.mapNotNullTo(this,destination, transform::apply);
}
/**
* @return the first element yielding the largest value of the given function or `null` if there are no elements.
*
* The operation is _terminal_.
*
*/
default <R extends Comparable<R>> T maxByOrNull(Function<? super T,? extends R> selector) {
return SequencesKt.maxByOrNull(this, selector::apply);
}
/**
* @return the first element having the largest value according to the provided {@param comparator} or `null` if there are no elements.
*
* The operation is _terminal_.
*/
default T maxWithOrNull(Comparator<? super T> comparator) {
return SequencesKt.maxWithOrNull(this, comparator);
}
/**
* @return the first element yielding the smallest value of the given function or `null` if there are no elements.
*
* The operation is _terminal_.
*
*/
default <R extends Comparable<R>> T minByOrNull(Function<? super T,? extends R> selector) {
return SequencesKt.minByOrNull(this, selector::apply);
}
/**
* @return a sequence containing all elements of the original sequence without the first occurrence of the given {@param element}.
*
* The operation is _intermediate_ and _stateless_.
*/
default Sek<T> minus(T element) {
return SequencesKt.minus(this, element)::iterator;
}
/**
* @return a sequence containing all elements of original sequence except the elements contained in the given {@param elements} array.
*
* Note that the source sequence and the array being subtracted are iterated only when an `iterator` is requested from
* the resulting sequence. Changing any of them between successive calls to `iterator` may affect the result.
*
* The {@param elements} array may be converted to a {@link HashSet} to speed up the operation, thus the elements are required to have
* a correct and stable implementation of `hashCode()` that doesn't change between successive invocations.
*
* The operation is _intermediate_ and _stateful_.
*/
default Sek<T> minus(T[] elements) {
return SequencesKt.minus(this, elements)::iterator;
}
/**
* @return a sequence containing all elements of original sequence except the elements contained in the given {@param elements} collection.
*
* Note that the source sequence and the collection being subtracted are iterated only when an `iterator` is requested from
* the resulting sequence. Changing any of them between successive calls to `iterator` may affect the result.
*
* The {@param elements} collection may be converted to a {@link HashSet} to speed up the operation, thus the elements are required to have
* a correct and stable implementation of `hashCode()` that doesn't change between successive invocations.
*
* The operation is _intermediate_ and _stateful_.
*/
default Sek<T> minus(Iterable<? extends T> elements) {
return SequencesKt.minus(this, elements)::iterator;
}
/**
* @return a sequence containing all elements of original sequence except the elements contained in the given {@param elements} sequence.
*
* Note that the source sequence and the sequence being subtracted are iterated only when an `iterator` is requested from
* the resulting sequence. Changing any of them between successive calls to `iterator` may affect the result.
*
* The operation is _intermediate_ for this sequence and _terminal_ and _stateful_ for the {@param elements} sequence.