Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: iter: Enumerate, Keys and Values functions #68887

Closed
mymmrac opened this issue Aug 15, 2024 · 4 comments
Closed

proposal: iter: Enumerate, Keys and Values functions #68887

mymmrac opened this issue Aug 15, 2024 · 4 comments
Labels
Milestone

Comments

@mymmrac
Copy link

mymmrac commented Aug 15, 2024

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
			}
		}
	}
}
@gopherbot gopherbot added this to the Proposal milestone Aug 15, 2024
@gabyhelp
Copy link

Related Issues and Documentation

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

@mymmrac
Copy link
Author

mymmrac commented Aug 15, 2024

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

@ianlancetaylor ianlancetaylor moved this to Incoming in Proposals Aug 15, 2024
@jimmyfrasche
Copy link
Member

The top level comment of #61898 does not touch on but these all have come up in the discussion (along with their generalizations).

@mymmrac
Copy link
Author

mymmrac commented Aug 15, 2024

I found a comment about those functions, and the explanations seem reasonable, so I guess this is just a duplicate issue, closing it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants