Repository navigation
Expand file tree
/
Copy pathseq.go
More file actions
1646 lines (1531 loc) · 48.7 KB
/
Copy pathseq.go
File metadata and controls
1646 lines (1531 loc) · 48.7 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 seq
import (
"cmp"
"context"
"iter"
"sync/atomic"
"time"
)
// With returns a sequence with the provided values. The values are iterated over lazily when the returned sequence is iterated
// over.
func With[T any](v ...T) iter.Seq[T] {
return func(yield func(T) bool) {
for _, t := range v {
if !yield(t) {
return
}
}
}
}
// KV pairs a key and a value together. Easiest way to use this is by declaring a local type with the K and V types you want
// to use and then use that, like so:
//
// func(...) {
// type lKV = KV[string, string]
// i := WithKV(lKV{"a", "1"}, lKV{"b", "2"}, lKV{"c", "3"})
// ...
type KV[K, V any] struct {
K K
V V
}
// WithKV returns a sequence with the provided key-value pairs. The key-value pairs are iterated over lazily when the returned
// sequence is iterated over.
func WithKV[K, V any](kv ...KV[K, V]) iter.Seq2[K, V] {
return func(yield func(K, V) bool) {
for _, kv := range kv {
if !yield(kv.K, kv.V) {
return
}
}
}
}
// FromChan returns a sequence that yields values from the provided channel. The sequence is iterated over lazily when the
// returned sequence is iterated over. The sequence will end when the channel is closed.
//
// This allows for collecting values from a channel into a slice or similar relatively easily:
//
// s := slices.Collect(FromChan(ch))
// // instead of
// var s []T
// for v := range ch {
// s = append(s, v)
// }
func FromChan[T any](ch <-chan T) iter.Seq[T] {
return func(yield func(T) bool) {
for t := range ch {
if !yield(t) {
return
}
}
}
}
// ToChan returns a channel that yields values from the provided sequence. The provided sequence is iterated over lazily when
// the returned channel is iterated over. The channel is closed when the sequence is exhausted. If the consumer stops
// receiving before the sequence is exhausted, the producing goroutine blocks forever; use [ToChanCtx] when the
// consumer may abandon the channel.
func ToChan[T any](seq iter.Seq[T]) <-chan T {
ch := make(chan T)
go func() {
defer close(ch)
for t := range seq {
ch <- t
}
}()
return ch
}
// ToChanCtx returns a channel that yields values from the provided sequence. The provided sequence is iterated over
// lazily when the returned channel is iterated over. The channel is closed when the sequence is exhausted or the
// context is canceled, whichever comes first.
func ToChanCtx[T any](ctx context.Context, seq iter.Seq[T]) <-chan T {
ch := make(chan T)
go func() {
defer close(ch)
for t := range seq {
select {
case <-ctx.Done():
return
default:
}
select {
case <-ctx.Done():
return
case ch <- t:
}
}
}()
return ch
}
// Map the values in the sequence to a new sequence of values by applying the function fn to each value. Function application
// happens lazily when the returned sequence is iterated over.
func Map[T, O any](seq iter.Seq[T], fn func(T) O) iter.Seq[O] {
return func(yield func(o O) bool) {
for o := range seq {
if !yield(fn(o)) {
return
}
}
}
}
// MapKV maps the key-value pairs in the sequence to a new sequence of key-value pairs by applying the function fn to each
// key-value pair. Function application happens lazily when the returned sequence is iterated over.
func MapKV[K, V, K1, V1 any](seq iter.Seq2[K, V], fn func(K, V) (K1, V1)) iter.Seq2[K1, V1] {
return func(yield func(K1, V1) bool) {
for k, v := range seq {
if !yield(fn(k, v)) {
return
}
}
}
}
// Append the items to the sequence and return an extended sequence. The provided sequence and appended items are iterated over
// lazily when the returned sequence is iterated over.
func Append[T any](seq iter.Seq[T], items ...T) iter.Seq[T] {
return func(yield func(T) bool) {
for item := range seq {
if !yield(item) {
return
}
}
for _, item := range items {
if !yield(item) {
return
}
}
}
}
// AppendKV appends the key-value pairs to the sequence and returns an extended sequence. The provided sequence and appended
// key-value pairs are iterated over lazily when the returned sequence is iterated over.
func AppendKV[K, V any](seq iter.Seq2[K, V], items ...KV[K, V]) iter.Seq2[K, V] {
return func(yield func(K, V) bool) {
for k, v := range seq {
if !yield(k, v) {
return
}
}
for _, kv := range items {
if !yield(kv.K, kv.V) {
return
}
}
}
}
// Filter the values in the sequence by applying fn to each value. Filtering happens when the returned sequence is
// iterated over.
func Filter[T any](seq iter.Seq[T], fn func(T) bool) iter.Seq[T] {
return func(yield func(T) bool) {
seq(func(t T) bool {
if fn(t) {
return yield(t)
}
return true
})
}
}
// FilterKV filters the key-value pairs in the sequence by applying fn to each key-value pair. Filtering happens when the
// returned sequence is iterated over.
func FilterKV[K, V any](seq iter.Seq2[K, V], fn func(K, V) bool) iter.Seq2[K, V] {
return func(yield func(K, V) bool) {
for k, v := range seq {
if fn(k, v) {
if !yield(k, v) {
return
}
}
}
}
}
// IntK returns a function that returns an increasing integer each time it is called, starting at 0. The returned function is stateful
// and is safe to call concurrently.
func IntK[V any]() func(V) int {
var i atomic.Int64
i.Store(-1)
return func(V) int {
return int(i.Add(1))
}
}
// IterKV converts an iter.Seq[V] to an iter.Seq2[K, V]. The provided sequence is iterated over lazily when the returned
// sequence is iterated over. keyFn is called for each value to get the key.
func IterKV[K, V any](seq iter.Seq[V], keyFn func(V) K) iter.Seq2[K, V] {
return func(yield func(k K, v V) bool) {
for v := range seq {
k := keyFn(v)
if !yield(k, v) {
return
}
}
}
}
// IterK converts an iter.Seq2[K, V] to an iter.Seq[K]. The provided sequence is iterated over lazily when the returned
// sequence is iterated over.
func IterK[K, V any](seq iter.Seq2[K, V]) iter.Seq[K] {
return func(yield func(k K) bool) {
for k := range seq {
if !yield(k) {
return
}
}
}
}
// IterV converts an iter.Seq2[K, V] to an iter.Seq[V]. The provided sequence is iterated over lazily when the returned
// sequence is iterated over.
func IterV[K, V any](seq iter.Seq2[K, V]) iter.Seq[V] {
return func(yield func(v V) bool) {
for _, v := range seq {
if !yield(v) {
return
}
}
}
}
// Max value of the sequence. Uses max builtin to compare values. The second value is false if the sequence is empty. The
// sequence is iterated over before Max returns.
func Max[T cmp.Ordered](seq iter.Seq[T]) (T, bool) {
var mt T
var value bool
for t := range seq {
if !value {
mt = t
value = true
} else {
mt = max(t, mt)
}
}
return mt, value
}
// MaxFunc is like [Max] but uses the function to compare elements. The provided sequence is iterated over before MaxFunc returns.
func MaxFunc[T any](seq iter.Seq[T], compare func(T, T) int) (T, bool) {
var mt T
var value bool
for t := range seq {
if !value {
mt = t
value = true
} else if compare(t, mt) > 0 {
mt = t
}
}
return mt, value
}
// MaxFuncKV is like [MaxFunc] but for key-value pairs. The provided sequence is iterated over before MaxFuncKV returns.
func MaxFuncKV[K, V any](seq iter.Seq2[K, V], compare func(KV[K, V], KV[K, V]) int) (KV[K, V], bool) {
var mt KV[K, V]
var value bool
for k, v := range seq {
t := KV[K, V]{K: k, V: v}
if !value {
mt = t
value = true
} else if compare(t, mt) > 0 {
mt = t
}
}
return mt, value
}
// Min value from the sequence. Uses min builtin to compare values. The second value is false if the sequence is empty. The
// sequence is iterated over before Min returns.
func Min[T cmp.Ordered](seq iter.Seq[T]) (T, bool) {
var mt T
var value bool
for t := range seq {
if !value {
mt = t
value = true
} else {
mt = min(t, mt)
}
}
return mt, value
}
// MinFunc is like [Min] but uses the function to compare elements. The provided sequence is iterated over before MinFunc returns.
func MinFunc[T any](seq iter.Seq[T], compare func(T, T) int) (T, bool) {
var mt T
var value bool
for t := range seq {
if !value {
mt = t
value = true
} else if compare(t, mt) < 0 {
mt = t
}
}
return mt, value
}
// MinFuncKV is like [MinFunc] but for key-value pairs. The provided sequence is iterated over before MinFuncKV returns.
func MinFuncKV[K, V any](seq iter.Seq2[K, V], compare func(KV[K, V], KV[K, V]) int) (KV[K, V], bool) {
var mt KV[K, V]
var value bool
for k, v := range seq {
t := KV[K, V]{K: k, V: v}
if !value {
mt = t
value = true
} else if compare(t, mt) < 0 {
mt = t
}
}
return mt, value
}
// Reduce the sequence to a single value by applying the function fn to each value. The provided sequence is iterated
// over before Reduce returns.
func Reduce[T, O any](seq iter.Seq[T], initial O, fn func(agg O, t T) O) O {
agg := initial
for t := range seq {
agg = fn(agg, t)
}
return agg
}
// ReduceKV reduces the sequence to a single value by applying the function fn to each key-value pair. The provided sequence is iterated
// over before ReduceKV returns.
func ReduceKV[K, V, O any](seq iter.Seq2[K, V], initial O, fn func(agg O, k K, v V) O) O {
agg := initial
for k, v := range seq {
agg = fn(agg, k, v)
}
return agg
}
// Compact returns an iterator that yields all values that are not equal to the previous value. The provided sequence is iterated
// over lazily when the returned sequence is iterated over.
func Compact[T comparable](seq iter.Seq[T]) iter.Seq[T] {
return func(yield func(T) bool) {
var prev T
first := true
for t := range seq {
if first || prev != t {
first = false
prev = t
if !yield(t) {
return
}
}
}
}
}
// CompactFunc is like [Compact] but uses the function to compare elements. For runs of elements that compare equal,
// CompactFunc only yields the first one. The provided sequence is iterated over lazily when the returned sequence is
// iterated over.
func CompactFunc[T any](seq iter.Seq[T], equal func(T, T) bool) iter.Seq[T] {
return func(yield func(T) bool) {
var prev T
first := true
for t := range seq {
if first || !equal(prev, t) {
first = false
prev = t
if !yield(t) {
return
}
}
}
}
}
// CompactKV returns an iterator that yields all key-value pairs that are not equal to the previous key-value pair. The provided
// sequence is iterated over lazily when the returned sequence is iterated over.
func CompactKV[K, V comparable](seq iter.Seq2[K, V]) iter.Seq2[K, V] {
return func(yield func(K, V) bool) {
var prev KV[K, V]
first := true
for k, v := range seq {
if first || prev.K != k || prev.V != v {
first = false
prev.K = k
prev.V = v
if !yield(k, v) {
return
}
}
}
}
}
// CompactKVFunc is like [CompactKV] but uses the function to compare key-value pairs. For runs of key-value pairs that compare
// equal, CompactKVFunc only yields the first one. The provided sequence is iterated over lazily when the returned sequence is
// iterated over.
func CompactKVFunc[K, V any](seq iter.Seq2[K, V], equal func(KV[K, V], KV[K, V]) bool) iter.Seq2[K, V] {
return func(yield func(K, V) bool) {
var prev KV[K, V]
first := true
for k, v := range seq {
if first || !equal(prev, KV[K, V]{K: k, V: v}) {
first = false
prev.K = k
prev.V = v
if !yield(k, v) {
return
}
}
}
}
}
// Chunk the sequence into chunks of size. The provided sequence is iterated over lazily when the returned sequence is iterated
// over. The last chunk may have fewer than size elements. The size must be at least 1; if not, the function will panic.
func Chunk[T any](seq iter.Seq[T], size int) iter.Seq[iter.Seq[T]] {
if size < 1 {
panic("seq: Chunk size must be at least 1")
}
return func(yield func(iter.Seq[T]) bool) {
var chunk []T
// The first chunk grows via append so a sequence shorter than size never
// over-allocates; once a chunk has filled, later ones preallocate exactly size.
full := false
for t := range seq {
if chunk == nil && full {
chunk = make([]T, 0, size)
}
chunk = append(chunk, t)
if len(chunk) == size {
full = true
if !yield(With(chunk...)) {
return
}
chunk = nil
}
}
if len(chunk) > 0 {
yield(With(chunk...))
}
}
}
// ChunkKV is like [Chunk] but for key-value pairs. The provided sequence is iterated over lazily when the returned sequence is
// iterated over. The last chunk may have fewer than size elements. The size must be at least 1; if not, the function will panic.
func ChunkKV[K, V any](seq iter.Seq2[K, V], size int) iter.Seq[iter.Seq2[K, V]] {
if size < 1 {
panic("seq: ChunkKV size must be at least 1")
}
return func(yield func(iter.Seq2[K, V]) bool) {
var chunk []KV[K, V]
// The first chunk grows via append so a sequence shorter than size never
// over-allocates; once a chunk has filled, later ones preallocate exactly size.
full := false
for k, v := range seq {
if chunk == nil && full {
chunk = make([]KV[K, V], 0, size)
}
chunk = append(chunk, KV[K, V]{K: k, V: v})
if len(chunk) == size {
full = true
if !yield(WithKV(chunk...)) {
return
}
chunk = nil
}
}
if len(chunk) > 0 {
yield(WithKV(chunk...))
}
}
}
// Compare is like [CompareFunc] but uses the cmp.Compare function to compare elements.
func Compare[T cmp.Ordered](a, b iter.Seq[T]) int {
return CompareFunc(a, b, cmp.Compare)
}
// CompareFunc compares the elements of a and b, using the compare func on each pair of elements. The elements are
// compared sequentially, until one element is not equal to the other. The result of comparing the first non-matching
// elements is returned verbatim — it is not normalized to -1 or +1. If both sequences are equal until one of them
// ends, the shorter sequence is considered less than the longer one: the result is +1 if a is longer, -1 if b is
// longer, and 0 if the sequences are equal.
func CompareFunc[T any](a, b iter.Seq[T], compare func(T, T) int) int {
next, stop := iter.Pull(b)
defer stop()
for av := range a {
bv, ok := next()
if !ok { // b is shorter than a
return 1
}
if c := compare(av, bv); c != 0 {
return c
}
}
// done with a, check if b is longer
if _, ok := next(); ok {
return -1
}
// a and b are equal
return 0
}
// CompareKV is like [CompareKVFunc] but uses the cmp.Compare function to compare keys and values.
func CompareKV[K, V cmp.Ordered](a, b iter.Seq2[K, V]) int {
return CompareKVFunc(a, b, func(a, b KV[K, V]) int {
if c := cmp.Compare(a.K, b.K); c != 0 {
return c
}
return cmp.Compare(a.V, b.V)
})
}
// CompareKVFunc compares the key-value pairs of a and b, using the compare func on each pair of key-value pairs. The key-value
// pairs are compared sequentially, until one key-value pair is not equal to the other. The result of comparing the first
// non-matching key-value pairs is returned verbatim — it is not normalized to -1 or +1. If both sequences are equal
// until one of them ends, the shorter sequence is considered less than the longer one: the result is +1 if a is
// longer, -1 if b is longer, and 0 if the sequences are equal.
func CompareKVFunc[AK, AV, BK, BV any](a iter.Seq2[AK, AV], b iter.Seq2[BK, BV], compare func(a KV[AK, AV], b KV[BK, BV]) int) int {
next, stop := iter.Pull2(b)
defer stop()
for ak, av := range a {
bk, bv, ok := next()
if !ok { // b is shorter than a
return 1
}
if c := compare(KV[AK, AV]{K: ak, V: av}, KV[BK, BV]{K: bk, V: bv}); c != 0 {
return c
}
}
// done with a, check if b is longer
if _, _, ok := next(); ok {
return -1
}
// a and b are equal
return 0
}
// Contains returns true if the value is in the sequence. The sequence is iterated over when Contains is called.
func Contains[T comparable](seq iter.Seq[T], value T) bool {
for t := range seq {
if t == value {
return true
}
}
return false
}
// ContainsKV returns true if the key-value pair is in the sequence. The sequence is iterated over when ContainsKV is called.
func ContainsKV[K, V comparable](seq iter.Seq2[K, V], key K, value V) bool {
for k, v := range seq {
if k == key && v == value {
return true
}
}
return false
}
// ContainsFunc returns true if the predicate function returns true for any value in the sequence. The sequence is
// iterated over when ContainsFunc is called.
func ContainsFunc[T any](seq iter.Seq[T], predicate func(T) bool) bool {
for t := range seq {
if predicate(t) {
return true
}
}
return false
}
// ContainsKVFunc returns true if the predicate function returns true for any key-value pair in the sequence. The
// sequence is iterated over when ContainsKVFunc is called.
func ContainsKVFunc[K, V any](seq iter.Seq2[K, V], predicate func(K, V) bool) bool {
for k, v := range seq {
if predicate(k, v) {
return true
}
}
return false
}
// Equal returns true if the sequences are equal. The sequences are compared sequentially, until one element is not equal to
// the other.
func Equal[T comparable](a, b iter.Seq[T]) bool {
return CompareFunc(a, b, func(a, b T) int {
if a == b {
return 0
}
return -1
}) == 0
}
// EqualKV returns true if the key-value pairs in the sequences are equal. The key-value pairs are compared sequentially, until
// one key-value pair is not equal to the other.
func EqualKV[K, V comparable](a, b iter.Seq2[K, V]) bool {
return CompareKVFunc(a, b, func(a, b KV[K, V]) int {
if a.K == b.K && a.V == b.V {
return 0
}
if a.K != b.K {
return -1
}
return 1
}) == 0
}
// EqualFunc is like [Equal] but uses the function to compare elements.
func EqualFunc[T any](a, b iter.Seq[T], equal func(T, T) bool) bool {
return CompareFunc(a, b, func(a, b T) int {
if equal(a, b) {
return 0
}
return -1
}) == 0
}
// EqualKVFunc is like [EqualKV] but uses the function to compare key-value pairs.
func EqualKVFunc[AK, AV, BK, BV any](a iter.Seq2[AK, AV], b iter.Seq2[BK, BV], equal func(a KV[AK, AV], b KV[BK, BV]) bool) bool {
return CompareKVFunc(a, b, func(a KV[AK, AV], b KV[BK, BV]) int {
if equal(a, b) {
return 0
}
return 1
}) == 0
}
// Repeat returns a sequence which repeats the value n times.
func Repeat[T any](n int, t T) iter.Seq[T] {
return func(yield func(T) bool) {
for range n {
if !yield(t) {
return
}
}
}
}
// RepeatKV returns a sequence which repeats the key-value pair n times.
func RepeatKV[K, V any](n int, k K, v V) iter.Seq2[K, V] {
return func(yield func(K, V) bool) {
for range n {
if !yield(k, v) {
return
}
}
}
}
// Replace the old value with the new value in the sequence. The provided sequence is iterated over lazily when the
// returned sequence is iterated over.
func Replace[T comparable](seq iter.Seq[T], old, new T) iter.Seq[T] {
return func(yield func(T) bool) {
for t := range seq {
if t == old {
t = new
}
if !yield(t) {
return
}
}
}
}
// ReplaceKV replaces the old key-value pair with the new key-value pair in the sequence. The provided sequence is iterated
// over lazily when the returned sequence is iterated over.
func ReplaceKV[K, V comparable](seq iter.Seq2[K, V], old KV[K, V], new KV[K, V]) iter.Seq2[K, V] {
return func(yield func(K, V) bool) {
for k, v := range seq {
if k == old.K && v == old.V {
k = new.K
v = new.V
}
if !yield(k, v) {
return
}
}
}
}
// IsSorted returns true if the sequence is sorted. The provided sequence is iterated over before IsSorted returns.
// [cmp.Compare] is used to compare elements.
func IsSorted[T cmp.Ordered](seq iter.Seq[T]) bool {
var prev T
first := true
for t := range seq {
if !first && cmp.Compare(t, prev) < 0 {
return false
}
first = false
prev = t
}
return true
}
// IsSortedKV returns true if the sequence is sorted. The keys and values are each compared independently with
// [cmp.Compare]: both the keys and the values must be non-decreasing. Note this is stricter than the lexicographic
// (key, then value) ordering used by [CompareKV]. The provided sequence is iterated over before IsSortedKV returns.
func IsSortedKV[K, V cmp.Ordered](seq iter.Seq2[K, V]) bool {
var prev KV[K, V]
first := true
for k, v := range seq {
if !first && ((cmp.Compare(k, prev.K) < 0) || (cmp.Compare(v, prev.V) < 0)) {
return false
}
first = false
prev.K = k
prev.V = v
}
return true
}
// Coalesce returns the first non zero value in the sequence. The provided sequence is iterated over when Coalesce is
// called, stopping at the first non-zero value. If no non-zero value is found, the second return value is false.
func Coalesce[T comparable](seq iter.Seq[T]) (T, bool) {
var zero T
for t := range seq {
if t != zero {
return t, true
}
}
return zero, false
}
// CoalesceKV returns the first key-value pair in the sequence whose value is non zero. The provided sequence is
// iterated over when CoalesceKV is called, stopping at the first non-zero value. If no non-zero value is found, the
// second return value is false.
func CoalesceKV[K, V comparable](seq iter.Seq2[K, V]) (KV[K, V], bool) {
var zero V
for k, v := range seq {
if v != zero {
return KV[K, V]{K: k, V: v}, true
}
}
return KV[K, V]{}, false
}
// Count returns the number of elements in the sequence. The sequence is iterated over before Count returns.
func Count[T any](seq iter.Seq[T]) int {
var count int
for range seq {
count++
}
return count
}
// CountKV returns the number of key-value pairs in the sequence. The sequence is iterated over before CountKV returns.
func CountKV[K, V any](seq iter.Seq2[K, V]) int {
var count int
for range seq {
count++
}
return count
}
// CountBy returns the number of elements in the sequence for which the function returns true. The sequence is iterated over
// before CountBy returns.
func CountBy[T any](seq iter.Seq[T], fn func(T) bool) int {
var count int
for t := range seq {
if fn(t) {
count++
}
}
return count
}
// CountKVBy returns the number of key-value pairs in the sequence for which the function returns true. The sequence is
// iterated over before CountKVBy returns.
func CountKVBy[K, V any](seq iter.Seq2[K, V], fn func(K, V) bool) int {
var count int
for k, v := range seq {
if fn(k, v) {
count++
}
}
return count
}
// CountValues returns a key-value sequence where the keys are the values in the original sequence and the values are
// the number of times that value appears in the original sequence. The returned key-value sequence is unordered. The
// provided sequence is iterated over before CountValues returns.
func CountValues[T comparable](seq iter.Seq[T]) iter.Seq2[T, int] {
m := make(map[T]int)
for t := range seq {
m[t]++
}
return func(yield func(T, int) bool) {
for k, v := range m {
if !yield(k, v) {
return
}
}
}
}
// Drop n elements from the start of the sequence. The provided sequence is iterated over lazily when the returned
// sequence is iterated over.
func Drop[T any](seq iter.Seq[T], n int) iter.Seq[T] {
return func(yield func(T) bool) {
i := -1
for t := range seq {
i++
if i < n {
continue
}
if !yield(t) {
return
}
}
}
}
// DropKV n key-value pairs from the start of the sequence. The provided sequence is iterated over lazily when the returned
// sequence is iterated over.
func DropKV[K, V any](seq iter.Seq2[K, V], n int) iter.Seq2[K, V] {
return func(yield func(K, V) bool) {
i := -1
for k, v := range seq {
i++
if i < n {
continue
}
if !yield(k, v) {
return
}
}
}
}
// DropBy returns a sequence with all elements for which the function returns true removed. The provided sequence is
// iterated over lazily when the returned sequence is iterated over. This is the opposite of Filter.
func DropBy[T any](seq iter.Seq[T], fn func(T) bool) iter.Seq[T] {
return Filter(seq, func(t T) bool {
return !fn(t)
})
}
// DropKVBy returns a sequence with all key-value pairs for which the function returns true removed. The provided sequence
// is iterated over lazily when the returned sequence is iterated over. This is the opposite of FilterKV.
func DropKVBy[K, V any](seq iter.Seq2[K, V], fn func(K, V) bool) iter.Seq2[K, V] {
return FilterKV(seq, func(k K, v V) bool {
return !fn(k, v)
})
}
// EveryUntil returns a sequence that yields the time every d duration until the provided time. The ticker will adjust
// the time interval or drop ticks to make up for slow iteratee. The duration d must be greater than zero; if not,
// the function will panic. Waits d long before yielding the first element.
func EveryUntil(d time.Duration, until time.Time) iter.Seq[time.Time] {
if d <= 0 {
panic("seq: EveryUntil interval must be positive")
}
return func(yield func(time.Time) bool) {
for now := range time.Tick(d) {
if now.After(until) {
return
}
if !yield(now) {
return
}
// Re-check the clock after the yield returns: a slow iteratee may have consumed the
// remaining time, and ending here beats waiting out another tick to notice. Checking
// now again would be useless — it cannot have changed since the check above.
if time.Now().After(until) {
return
}
}
}
}
// EveryN returns a sequence that yields the time every d duration n times. The ticker will adjust the time interval or
// drop ticks to make up for slow iteratee. The duration d must be greater than zero; if not, the function will panic.
// Waits d long before yielding the first element. If times is not positive, the sequence is empty.
func EveryN(d time.Duration, times int) iter.Seq[time.Time] {
if d <= 0 {
panic("seq: EveryN interval must be positive")
}
return func(yield func(time.Time) bool) {
if times <= 0 {
return
}
for now := range time.Tick(d) {
if !yield(now) {
return
}
times--
if times == 0 {
return
}
}
}
}
// MapToKV maps the values in the sequence to a new sequence of key-value pairs by applying the function fn to each value. Function
// application happens lazily when the returned sequence is iterated over.
func MapToKV[T, K, V any](seq iter.Seq[T], fn func(T) (K, V)) iter.Seq2[K, V] {
return func(yield func(K, V) bool) {
for t := range seq {
k, v := fn(t)
if !yield(k, v) {
return
}
}
}
}
// At returns the value at the given 0-based index in the sequence and true. If
// the index is out of range (negative or beyond sequence length), it returns
// the zero value and false. The provided sequence is iterated over up to and
// including the target index when At is called.
func At[T any](seq iter.Seq[T], index int) (T, bool) {
if index < 0 {
var z T
return z, false
}
var i int
for v := range seq {
if i == index {
return v, true
}
i++
}
var z T
return z, false
}
// AtKV returns the key and value at the given 0-based index in the sequence and true. If
// the index is out of range (negative or beyond sequence length), it returns
// the zero values and false. The provided sequence is iterated over up to and
// including the target index when AtKV is called. To find a value by key, use
// FindByKey instead.
func AtKV[K any, V any](seq iter.Seq2[K, V], index int) (K, V, bool) {
if index < 0 {
var zk K
var zv V
return zk, zv, false
}
var i int
for k, v := range seq {
if i == index {
return k, v, true
}
i++
}
var zk K
var zv V
return zk, zv, false
}
// Find returns the 0-based index of the first occurrence of the value in the sequence and true. If the value is not
// found, the first return value is the length of the sequence and the second return value is false. The provided
// sequence is iterated over when Find is called.
func Find[T comparable](seq iter.Seq[T], value T) (int, bool) {
var i int
for t := range seq {
if t == value {
return i, true
}
i++
}
return i, false
}
// FindBy returns the first value in the sequence for which the function returns true, the "index" (0 based) of the
// value, and true. If no value is found, the first return value is the zero value of the type, the second return value
// is the length of the sequence, and the third return value is false. The provided sequence is iterated over when FindBy is called.
func FindBy[T any](seq iter.Seq[T], fn func(T) bool) (T, int, bool) {
var i int
for t := range seq {
if fn(t) {
return t, i, true
}
i++
}
var z T
return z, i, false
}
// FindByKey returns the value of the first key-value pair in the sequence for which the function returns true, the
// "index" (0 based) of the value, and true. If the key is not found, the first return value is the zero value of the
// value type, the second return value is the length of the sequence, and the third return value is false. The provided