Skip to content

Commit

Permalink
add the new map generic functions
Browse files Browse the repository at this point in the history
  • Loading branch information
xgfone committed Mar 3, 2024
1 parent 2106dfe commit e3c6456
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 0 deletions.
54 changes: 54 additions & 0 deletions helper/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,57 @@ func MapKeys[M ~map[K]V, K comparable, V any](maps M) []K {
}
return keys
}

// MapKeysFunc returns all the keys of the map by the conversion function.
func MapKeysFunc[M ~map[K]V, T any, K comparable, V any](maps M, convert func(K) T) []T {
keys := make([]T, 0, len(maps))
for k := range maps {
keys = append(keys, convert(k))
}
return keys
}

// MapValues returns all the values of the map by the conversion function.
func MapValuesFunc[M ~map[K]V, T any, K comparable, V any](maps M, convert func(V) T) []T {
values := make([]T, 0, len(maps))
for _, v := range maps {
values = append(values, convert(v))
}
return values
}

// ToSetMap converts a slice s to a set map.
func ToSetMap[S ~[]T, T comparable](s S) map[T]struct{} {
m := make(map[T]struct{}, len(s))
for _, k := range s {
m[k] = struct{}{}
}
return m
}

// ToBoolMap converts a slice s to a bool map.
func ToBoolMap[S ~[]T, T comparable](s S) map[T]bool {
m := make(map[T]bool, len(s))
for _, k := range s {
m[k] = true
}
return m
}

// ToSetMapFunc converts a slice s to a set map by a conversion function.
func ToSetMapFunc[S ~[]T, K comparable, T any](s S, convert func(T) K) map[K]struct{} {
m := make(map[K]struct{}, len(s))
for _, k := range s {
m[convert(k)] = struct{}{}
}
return m
}

// ToBoolMapFunc converts a slice s to a bool map by a conversion function.
func ToBoolMapFunc[S ~[]T, K comparable, T any](s S, convert func(T) K) map[K]bool {
m := make(map[K]bool, len(s))
for _, k := range s {
m[convert(k)] = true
}
return m
}
82 changes: 82 additions & 0 deletions helper/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package helper

import (
"fmt"
"slices"
"sort"
"testing"
Expand Down Expand Up @@ -55,3 +56,84 @@ func TestMapValues(t *testing.T) {
t.Errorf("expect %v, but got %v", expectstrs, strs)
}
}

func ExampleMapKeysFunc() {
type Key struct {
K string
V int32
}
maps := map[Key]bool{
{K: "a", V: 1}: true,
{K: "b", V: 2}: true,
{K: "c", V: 3}: true,
}

keys := MapKeysFunc(maps, func(k Key) string { return k.K })
slices.Sort(keys)
fmt.Println(keys)

// Output:
// [a b c]
}

func ExampleMapValuesFunc() {
type Value struct {
V int
}
maps := map[string]Value{
"a": {V: 1},
"b": {V: 2},
"c": {V: 3},
}

values := MapValuesFunc(maps, func(v Value) int { return v.V })
slices.Sort(values)
fmt.Println(values)

// Output:
// [1 2 3]
}

func ExampleToSetMap() {
setmap := ToSetMap([]string{"a", "b", "c"})
fmt.Println(setmap)

// Output:
// map[a:{} b:{} c:{}]
}

func ExampleToBoolMap() {
boolmap := ToBoolMap([]string{"a", "b", "c"})
fmt.Println(boolmap)

// Output:
// map[a:true b:true c:true]
}

func ExampleToSetMapFunc() {
type S struct {
K string
V int32
}

values := []S{{K: "a", V: 1}, {K: "b", V: 2}, {K: "c", V: 3}}
setmap := ToSetMapFunc(values, func(s S) string { return s.K })
fmt.Println(setmap)

// Output:
// map[a:{} b:{} c:{}]
}

func ExampleToBoolMapFunc() {
type S struct {
K string
V int32
}

values := []S{{K: "a", V: 1}, {K: "b", V: 2}, {K: "c", V: 3}}
setmap := ToBoolMapFunc(values, func(s S) string { return s.K })
fmt.Println(setmap)

// Output:
// map[a:true b:true c:true]
}

0 comments on commit e3c6456

Please sign in to comment.