Skip to content

Commit

Permalink
fn: add Keys and Values helper functions for map access
Browse files Browse the repository at this point in the history
  • Loading branch information
guggero committed Jan 11, 2024
1 parent ed7409c commit 3f4f12e
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions fn/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 3f4f12e

Please sign in to comment.