Skip to content

proposal: maps: add functions to filter and transform map values #70061

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
pixel365 opened this issue Oct 26, 2024 · 3 comments
Closed

proposal: maps: add functions to filter and transform map values #70061

pixel365 opened this issue Oct 26, 2024 · 3 comments
Labels
Milestone

Comments

@pixel365
Copy link
Contributor

Proposal Details

I propose to add the following general-purpose functions to the maps package:

// FilterFunc returns an iterator with filtered key-value pairs.
func FilterFunc[M ~map[K]V, K comparable, V any](m M, filter func(K, V) bool) iter.Seq2[K, V] {
	return func(yield func(K, V) bool) {
		for k, v := range m {
			if filter(k, v) {
				if !yield(k, v) {
					return
				}
			}
		}
	}
}
// TransformValuesFunc modifies values ​​with a user-defined function.
func TransformValuesFunc[M ~map[K]V, K comparable, V any](m M, transform func(K, V) V) {
	for k, v := range m {
		m[k] = transform(k, v)
	}
}
@gopherbot gopherbot added this to the Proposal milestone Oct 26, 2024
@gabyhelp
Copy link

@seankhliao
Copy link
Member

This is just xiter.Filter(f, maps.All(m)) and xiter.Map(f, maps.All(m)) from #61898
No need for specialized functions

@seankhliao
Copy link
Member

Duplicate of #61898

@seankhliao seankhliao marked this as a duplicate of #61898 Oct 26, 2024
@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Oct 26, 2024
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