From 5108b09df5a0ed2d4ac523f427217dfb9afa6096 Mon Sep 17 00:00:00 2001 From: brent saner Date: Wed, 15 Jul 2026 19:04:00 -0400 Subject: [PATCH] v1.16.13 ADDED: * mapsx: ** ListKeys() ** ListValues() --- mapsx/funcs.go | 58 ++++++++++++++++++++++++++++++++++++++--- tplx/sprigx/README.html | 2 +- tplx/sprigx/README.md | 2 +- 3 files changed, 57 insertions(+), 5 deletions(-) diff --git a/mapsx/funcs.go b/mapsx/funcs.go index 9d0d1df..ff91c94 100644 --- a/mapsx/funcs.go +++ b/mapsx/funcs.go @@ -26,10 +26,62 @@ func GetOk[Map ~map[K]V, K comparable, V any](m Map, k K, v V) (val V, found boo } /* - Must, unlike [Get] or [GetOk], requires that `k` be in map `m`. +ListKeys returns map `m`'s keys as a slice (unlike [maps.Keys], +which returns an iterator via [iter.Seq]). - A panic with error [ErrNotFound] will be raised if `k` is not present. - Otherwise the found value will be returned. +ListKeys will return nil if `m` is nil, and an empty slice +if `m` is non-nil but empty. + +The ordering of `mapKeys` is unpredictable as maps are unsorted +and no explicit sorting is performed. +*/ +func ListKeys[Map ~map[K]V, K comparable, V any](m Map) (mapKeys []K) { + + var k K + + if m == nil { + return + } + mapKeys = make([]K, 0, len(m)) + + for k, _ = range m { + mapKeys = append(mapKeys, k) + } + + return +} + +/* +ListValues returns map `m`'s values as a slice (unlike [maps.Values], +which returns an iterator via [iter.Seq]). + +ListValues will return nil if `m` is nil, and an empty slice +if `m` is non-nil but empty. + +The ordering of `mapVals` is unpredictable as maps are unsorted +and no explicit sorting is performed. +*/ +func ListValues[Map ~map[K]V, K comparable, V any](m Map) (mapVals []V) { + + var v V + + if m == nil { + return + } + mapVals = make([]V, 0, len(m)) + + for _, v = range m { + mapVals = append(mapVals, v) + } + + return +} + +/* +Must, unlike [Get] or [GetOk], requires that `k` be in map `m`. + +A panic with error [ErrNotFound] will be raised if `k` is not present. +Otherwise the found value will be returned. */ func Must[Map ~map[K]V, K comparable, V any](m Map, k K) (val V) { diff --git a/tplx/sprigx/README.html b/tplx/sprigx/README.html index 82cfeee..20df275 100644 --- a/tplx/sprigx/README.html +++ b/tplx/sprigx/README.html @@ -636,7 +636,7 @@ pre.rouge .gs {
Brent Saner

-Last rendered 2026-07-14 03:02:23 -0400 +Last rendered 2026-07-15 19:04:01 -0400
Table of Contents
diff --git a/tplx/sprigx/README.md b/tplx/sprigx/README.md index dc1647c..26c26d2 100644 --- a/tplx/sprigx/README.md +++ b/tplx/sprigx/README.md @@ -6,7 +6,7 @@ Brent Saner -Last rendered 2026-07-14 03:02:27 -0400 +Last rendered 2026-07-15 19:04:05 -0400