-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseq.go
44 lines (40 loc) · 842 Bytes
/
seq.go
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
package iter
import (
"golang.org/x/exp/constraints"
"iter"
)
// Keys converts a Seq2 to a Seq, iterating on just the first item in the sequence.
func Keys[K constraints.Ordered, V any](i1 iter.Seq2[K, V]) iter.Seq[K] {
return func(yield func(K) bool) {
for k, _ := range i1 {
if !yield(k) {
return
}
}
}
}
// Values converts a Seq2 to a Seq, iterating on just the second item in the sequence.
func Values[K constraints.Ordered, V any](i1 iter.Seq2[K, V]) iter.Seq[V] {
return func(yield func(V) bool) {
for _, v := range i1 {
if !yield(v) {
return
}
}
}
}
// Clip stops the iteration after n times.
func Clip[K any](i1 iter.Seq[K], n int) iter.Seq[K] {
return func(yield func(K) bool) {
var i int
for v := range i1 {
if i >= n {
return
}
if !yield(v) {
return
}
i++
}
}
}