more helper functions

This commit is contained in:
Timon Ringwald
2022-09-06 01:58:19 +02:00
parent 4f3cf3f537
commit cdb1cf3e03
6 changed files with 87 additions and 18 deletions

16
of.go Normal file
View File

@ -0,0 +1,16 @@
package slices
func Of[T any](values ...T) []T {
return values
}
// OfMap returns a slice containing the return values of the unmapper function
// applied to any key-value pair in m
// The order is random
func OfMap[K comparable, V, T any](m map[K]V, unmapper func(K, V) T) []T {
out := make([]T, 0, len(m))
for k, v := range m {
out = append(out, unmapper(k, v))
}
return out
}