From 3f4f12e64e623b4076230dd0433e76d1f255ad1b Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Thu, 11 Jan 2024 13:38:51 +0100 Subject: [PATCH] fn: add Keys and Values helper functions for map access --- fn/func.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/fn/func.go b/fn/func.go index 5f0fc3c7b..4c97242db 100644 --- a/fn/func.go +++ b/fn/func.go @@ -30,6 +30,27 @@ func Map[I, O any, S []I](s S, f func(I) O) []O { return output } +// Keys returns a slice containing all the keys of the given map. +func Keys[T any, K comparable](s map[K]T) []K { + output := make([]K, 0, len(s)) + for key := range s { + key := key + output = append(output, key) + } + + return output +} + +// Values returns a slice containing all the values of the given map. +func Values[T any, K comparable](s map[K]T) []T { + output := make([]T, 0, len(s)) + for key := range s { + output = append(output, s[key]) + } + + return output +} + // Filter applies the given predicate function to each element of the given // slice and generates a new slice containing only the elements for which the // predicate returned true.