You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The maps package currently provides iterator-based functions (Keys and Values)
that return iterators over a map's keys or values. While iterators are powerful
and composable, they require extra code to convert to slices, which is a common
use case.
Converting map keys or values to slices is a recurring pattern in Go code.
Currently, developers need to write boilerplate like:
KeysSlice[M ~map[K]V, K comparable, V any](m M) []K
ValuesSlice[M ~map[K]V, K comparable, V any](m M) []V
These functions would extract map keys/values into slices, complementing the
existing iterator-based functions and providing a direct solution for the common
use case of converting map contents to slices.
Proposed Implementation
The implementation would be straightforward:
// KeysSlice returns a new slice containing the keys of m.// The keys will be in an indeterminate order.funcKeysSlice[M~map[K]V, Kcomparable, Vany](mM) []K {
r:=make([]K, 0, len(m))
fork:=rangem {
r=append(r, k)
}
returnr
}
// ValuesSlice returns a new slice containing the values of m.// The values will be in an indeterminate order.funcValuesSlice[M~map[K]V, Kcomparable, Vany](mM) []V {
r:=make([]V, 0, len(m))
for_, v:=rangem {
r=append(r, v)
}
returnr
}
Use Cases
These functions would be useful for various common scenarios:
Working with map keys in a sorted manner:
keys:=maps.KeysSlice(myMap)
slices.Sort(keys)
// Process keys in sorted order
Getting a list of values for processing:
values:=maps.ValuesSlice(userRoles)
for_, role:=rangevalues {
// Process roles
}
Converting to a different representation:
// Easily convert map values to a slice for API responsesresponse.Items=maps.ValuesSlice(dataMap)
Alternatives Considered
Using existing iterator functions:
While Keys() and Values() combined with slices.Collect() can achieve
similar functionality, this requires importing multiple packages and is less
discoverable for users:
keys:=slices.Collect(maps.Keys(myMap))
Using range with assignment:
The current approach of manually creating slices and iterating is verbose and
requires boilerplate code.
Compatibility
This change adds new functionality without modifying existing behavior, so it has
no compatibility concerns.
/cc @golang/maps-owners
The text was updated successfully, but these errors were encountered:
Proposal Details
Background
The maps package currently provides iterator-based functions (
Keys
andValues
)that return iterators over a map's keys or values. While iterators are powerful
and composable, they require extra code to convert to slices, which is a common
use case.
Converting map keys or values to slices is a recurring pattern in Go code.
Currently, developers need to write boilerplate like:
Proposal
Add two new functions to the maps package:
KeysSlice[M ~map[K]V, K comparable, V any](m M) []K
ValuesSlice[M ~map[K]V, K comparable, V any](m M) []V
These functions would extract map keys/values into slices, complementing the
existing iterator-based functions and providing a direct solution for the common
use case of converting map contents to slices.
Proposed Implementation
The implementation would be straightforward:
Use Cases
These functions would be useful for various common scenarios:
Working with map keys in a sorted manner:
Getting a list of values for processing:
Converting to a different representation:
Alternatives Considered
Using existing iterator functions:
While
Keys()
andValues()
combined withslices.Collect()
can achievesimilar functionality, this requires importing multiple packages and is less
discoverable for users:
Using range with assignment:
The current approach of manually creating slices and iterating is verbose and
requires boilerplate code.
Compatibility
This change adds new functionality without modifying existing behavior, so it has
no compatibility concerns.
/cc @golang/maps-owners
The text was updated successfully, but these errors were encountered: