Proposal Details
I think it's will be common to work with iter.Seq
and iter.Seq2
simultaneously, but there is no quick way to go from iter.Seq
to iter.Seq2
and vice versa, so I propose to add new functions for iter
package
func Enumerate[T any](it iter.Seq[T]) iter.Seq2[int, T] {
return func(yield func(int, T) bool) {
i := 0
for v := range it {
if !yield(i, v) {
return
}
i++
}
}
}
func Keys[T1, T2 any](it iter.Seq2[T1, T2]) iter.Seq[T1] {
return func(yield func(T1) bool) {
for k := range it {
if !yield(k) {
return
}
}
}
}
func Values[T1, T2 any](it iter.Seq2[T1, T2]) iter.Seq[T2] {
return func(yield func(T2) bool) {
for _, v := range it {
if !yield(v) {
return
}
}
}
}
Comment From: gabyhelp
Related Issues and Documentation
- proposal: iter: convert between Seq and Seq2 #67334
- proposal: Go 2: range-over-function + range over types implementing iterator interface #65742 (closed)
- iter: new package for iterators #61897 (closed)
- proposal: Go 2: iterators #40605 (closed)
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)
Comment From: mymmrac
Proposal #67334 introduces more flexible, but complex ways to handle this issue
Proposal #61898 doesn't touch on issue of going from iter.Seq
to iter.Seq2
and vice versa
Comment From: jimmyfrasche
The top level comment of #61898 does not touch on but these all have come up in the discussion (along with their generalizations).
Comment From: mymmrac
I found a comment about those functions, and the explanations seem reasonable, so I guess this is just a duplicate issue, closing it