Skip to content

proposal: slices: add CollectKeys and CollectValues to collect from an iter.Seq2 #69706

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

Closed
jmillikin opened this issue Sep 30, 2024 · 3 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.

@ianlancetaylor ianlancetaylor moved this to Incoming in Proposals Sep 30, 2024
@seankhliao seankhliao changed the title proposal: slices: funcs CollectKeys and CollectValues to collect from an iter.Seq2 proposal: slices: add CollectKeys and CollectValues to collect from an iter.Seq2 Apr 6, 2025
@seankhliao
Copy link
Member

I think we'd prefer the composable versions as pointed out above

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Apr 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Incoming
Development

No branches or pull requests

5 participants