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: slices: funcs CollectKeys and CollectValues to collect from an iter.Seq2 #69706

Open
jmillikin opened this issue Sep 30, 2024 · 2 comments
Labels
Milestone

Comments

@jmillikin
Copy link

Proposal Details

The slices package currently has Collect() to perform iter.Seq[T] -> []T, but it doesn't have similar helper functions for collecting the keys or values from an iter.Seq2[K, V].

I propose to add the following two functions to slices:

func CollectKeys[K, V any](seq iter.Seq2[K, V]) []K {
	var s []K
	for k := range seq {
		s = append(s, k)
	}
	return s
}

func CollectValues[K, V any](seq iter.Seq2[K, V]) []V {
	var s []V
	for _, v := range seq {
		s = append(s, v)
	}
	return s
}
@gopherbot gopherbot added this to the Proposal milestone Sep 30, 2024
@gabyhelp
Copy link

Related Issues and Documentation

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

@gophun
Copy link

gophun commented Sep 30, 2024

The composable way would be xiter.Keys and xiter.Values, as proposed here combined with a normal slices.Collect.

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