goext/langext/maps.go

26 lines
492 B
Go
Raw Normal View History

2022-10-27 16:48:26 +02:00
package langext
func MapKeyArr[T comparable, V any](v map[T]V) []T {
result := make([]T, 0, len(v))
for k := range v {
result = append(result, k)
}
return result
}
2023-01-31 11:01:45 +01:00
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 {
result[keyfunc(v)] = v
}
return result
}
2023-04-23 19:31:48 +02:00
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 {
result[k] = v
}
return result
}