From b0d3ce8c1c9329b2bf6c881f882ca5ddd94de69d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Wed, 24 May 2023 22:01:29 +0200 Subject: [PATCH] v0.0.122 --- langext/maps.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/langext/maps.go b/langext/maps.go index 4a5cd11..90bd322 100644 --- a/langext/maps.go +++ b/langext/maps.go @@ -1,5 +1,10 @@ package langext +type MapEntry[T comparable, V any] struct { + Key T + Value V +} + func MapKeyArr[T comparable, V any](v map[T]V) []T { result := make([]T, 0, len(v)) for k := range v { @@ -8,6 +13,14 @@ func MapKeyArr[T comparable, V any](v map[T]V) []T { return result } +func MapValueArr[T comparable, V any](v map[T]V) []V { + result := make([]V, 0, len(v)) + for _, mv := range v { + result = append(result, mv) + } + return result +} + func ArrToMap[T comparable, V any](a []V, keyfunc func(V) T) map[T]V { result := make(map[T]V, len(a)) for _, v := range a { @@ -16,6 +29,17 @@ func ArrToMap[T comparable, V any](a []V, keyfunc func(V) T) map[T]V { return result } +func MapToArr[T comparable, V any](v map[T]V) []MapEntry[T, V] { + result := make([]MapEntry[T, V], 0, len(v)) + for mk, mv := range v { + result = append(result, MapEntry[T, V]{ + Key: mk, + Value: mv, + }) + } + return result +} + func CopyMap[K comparable, V any](a map[K]V) map[K]V { result := make(map[K]V, len(a)) for k, v := range a {