This commit is contained in:
xing 2023-02-07 23:46:34 +08:00
parent fd951b7ab8
commit f0028e72f4
2 changed files with 14 additions and 1 deletions

View File

@ -1,6 +1,8 @@
package maps package maps
import "encoding/json" import (
"encoding/json"
)
func StrAnyMapToStruct[T any, M any](m M) (r T, err error) { func StrAnyMapToStruct[T any, M any](m M) (r T, err error) {
str, err := json.Marshal(m) str, err := json.Marshal(m)
@ -54,6 +56,17 @@ func IsExists[K comparable, V any](m map[K]V, k K) bool {
return ok return ok
} }
func Keys[K comparable, V any](m map[K]V) []K {
return FilterToSlice(m, func(k K, v V) (K, bool) {
return k, true
})
}
func Values[K comparable, V any](m map[K]V) []V {
return FilterToSlice(m, func(k K, v V) (V, bool) {
return v, true
})
}
func Reduce[T, V any, K comparable](m map[K]V, fn func(K, V, T) T, r T) T { func Reduce[T, V any, K comparable](m map[K]V, fn func(K, V, T) T, r T) T {
for k, v := range m { for k, v := range m {
r = fn(k, v, r) r = fn(k, v, r)