-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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: maps: Add Group and GroupFunc #66948
Comments
What's a typical implementation of Seq2 in this scenario? (Often a Seq2 is a map, which doesn't have repeated keys.) |
type Person struct {
Name string
Age int
Gender bool
}
func PersonAll(ps []Person) iter.Seq2[string, Person] {
return func(yield func(string, Person) bool) {
for _, p := range ps {
if !yield(p.Name, p) {
return
}
}
}
} |
|
|
Assume we have a m := make(map[string][]Person)
for p := range people.All() {
m[p.name] = append(m[p.name], p)
} Would using a helper function save much here? Sometimes a loop is more clear than a helper function. |
For simple use cases like this, I think
|
A generalization of |
Cross linking #69123, which is also about a grouping iterator. |
I'm not sure about func KeyBy[K, V any](key func(V) K, seq iter.Seq[V]) iter.Seq2[K, V] that yields |
Proposal Details
I propose to add the functions
Group
andGroupFunc
to the package maps , as I find myself reimplementing these often.They work similarly to
Object.GroupBy
in JavaScript.The text was updated successfully, but these errors were encountered: