v1.17.6
ADDED: * mapsx.(ListSortedKeys|ListSortedValues|SortedKeys|SortedValues)
This commit is contained in:
16 files changed
+960
-15
No files matched your search
@@ -1,5 +1,11 @@
|
||||
package mapsx
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"iter"
|
||||
"slices"
|
||||
)
|
||||
|
||||
/*
|
||||
Get mimics Python's [dict.get()] behavior, returning value `v` if key `k`
|
||||
is not found in map `m`.
|
||||
@@ -34,6 +40,7 @@ if `m` is non-nil but empty.
|
||||
|
||||
The ordering of `mapKeys` is unpredictable as maps are unsorted
|
||||
and no explicit sorting is performed.
|
||||
See [SortedKeys] and [ListSortedKeys] if sorting is desired.
|
||||
*/
|
||||
func ListKeys[Map ~map[K]V, K comparable, V any](m Map) (mapKeys []K) {
|
||||
|
||||
@@ -51,6 +58,36 @@ func ListKeys[Map ~map[K]V, K comparable, V any](m Map) (mapKeys []K) {
|
||||
return
|
||||
}
|
||||
|
||||
/*
|
||||
ListSortedKeys is like [ListKeys] but returns the keys in a sorted slice.
|
||||
|
||||
The keys in map `m` MUST conform to a [cmp.Ordered]!
|
||||
*/
|
||||
func ListSortedKeys[Map ~map[K]V, K cmp.Ordered, V any](m Map) (mapKeys []K) {
|
||||
|
||||
// All cmp.Ordered types are comparable, but not all comparable types are cmp.Ordered.
|
||||
mapKeys = ListKeys(m)
|
||||
|
||||
slices.Sort(mapKeys)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
/*
|
||||
ListSortedValues is like [ListValues] but returns the values in a sorted slice.
|
||||
|
||||
The values in map `m` MUST conform to a [cmp.Ordered]!
|
||||
*/
|
||||
func ListSortedValues[Map ~map[K]V, K comparable, V cmp.Ordered](m Map) (mapVals []V) {
|
||||
|
||||
// All cmp.Ordered types are comparable, but not all comparable types are cmp.Ordered.
|
||||
mapVals = ListValues(m)
|
||||
|
||||
slices.Sort(mapVals)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
/*
|
||||
ListValues returns map `m`'s values as a slice (unlike [maps.Values],
|
||||
which returns an iterator via [iter.Seq]).
|
||||
@@ -60,6 +97,7 @@ if `m` is non-nil but empty.
|
||||
|
||||
The ordering of `mapVals` is unpredictable as maps are unsorted
|
||||
and no explicit sorting is performed.
|
||||
See [SortedValues] and [ListSortedValues] if sorting is desired.
|
||||
*/
|
||||
func ListValues[Map ~map[K]V, K comparable, V any](m Map) (mapVals []V) {
|
||||
|
||||
@@ -77,6 +115,52 @@ func ListValues[Map ~map[K]V, K comparable, V any](m Map) (mapVals []V) {
|
||||
return
|
||||
}
|
||||
|
||||
/*
|
||||
SortedKeys is like [maps.Keys] but will sort the keys in the returned [iter.Seq].
|
||||
(There is a small cost to this.)
|
||||
|
||||
The keys in map `m` MUST conform to a [cmp.Ordered]!
|
||||
*/
|
||||
func SortedKeys[Map ~map[K]V, K cmp.Ordered, V any](m Map) (mapKeys iter.Seq[K]) {
|
||||
|
||||
var k K
|
||||
// All cmp.Ordered types are comparable, but not all comparable types are cmp.Ordered.
|
||||
var keys []K = ListKeys(m)
|
||||
|
||||
slices.Sort(keys)
|
||||
|
||||
return func(yield func(K) bool) {
|
||||
for _, k = range keys {
|
||||
if !yield(k) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
SortedValues is like [maps.Values] but will sort the values in the returned [iter.Seq].
|
||||
(There is a small cost to this.)
|
||||
|
||||
The values in map `m` MUST conform to a [cmp.Ordered]!
|
||||
*/
|
||||
func SortedValues[Map ~map[K]V, K comparable, V cmp.Ordered](m Map) (mapVals iter.Seq[V]) {
|
||||
|
||||
var v V
|
||||
// All cmp.Ordered types are comparable, but not all comparable types are cmp.Ordered.
|
||||
var vals []V = ListValues(m)
|
||||
|
||||
slices.Sort(vals)
|
||||
|
||||
return func(yield func(V) bool) {
|
||||
for _, v = range vals {
|
||||
if !yield(v) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Must, unlike [Get] or [GetOk], requires that `k` be in map `m`.
|
||||
|
||||
|
||||
Reference in new issue
Block a user