Open
Description
Proposal Details
I propose a function to allow a loop over a map in stable order. It takes all keys of the map, sorts them, and loops over the slides, each loop, yields a key-pair.
// SortedByKey returns an iterator for the given map that
// yields the key-value pairs in sorted order.
func SortedByKey[Map ~map[K]V, K cmp.Ordered, V any](m Map) iter.Seq2[K, V] {
return func(yield func(K, V) bool) {
for _, k := range slices.Sorted(Keys(m)) {
if !yield(k, m[k]) {
return
}
}
}
}
Metadata
Metadata
Assignees
Type
Projects
Status
Incoming