Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
dxyinme committed Mar 31, 2024
1 parent d98fcb5 commit 0d2775e
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 23 deletions.
26 changes: 13 additions & 13 deletions slice/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,54 +37,54 @@ func Map[Src any, Dst any](src []Src, m func(idx int, src Src) Dst) []Dst {
}

// 将[]Ele映射到map[Key]Ele
// 从Ele中提取Key的函数E2KFunc由使用者提供
// 从Ele中提取Key的函数fn由使用者提供
//
// 注意:
// 如果出现 i < j
// 设:
//
// key_i := E2KFunc(elements[i])
// key_j := E2KFunc(elements[j])
// key_i := fn(elements[i])
// key_j := fn(elements[j])
//
// 满足key_i == key_j 的情况,则在返回结果的resultMap中
// resultMap[key_i] = val_j
//
// 即使传入的字符串为nil,也保证返回的map是一个空map而不是nil
func MapWithE2KFunc[Ele any, Key comparable](
func ToMap[Ele any, Key comparable](
elements []Ele,
E2KFunc func(element Ele) Key,
fn func(element Ele) Key,
) map[Key]Ele {
return MapWithE2KVFunc(
return ToMapV(
elements,
func(element Ele) (Key, Ele) {
return E2KFunc(element), element
return fn(element), element
})
}

// 将[]Ele映射到map[Key]Val
// 从Ele中提取Key和Val的函数E2KVFunc由使用者提供
// 从Ele中提取Key和Val的函数fn由使用者提供
//
// 注意:
// 如果出现 i < j
// 设:
//
// key_i, val_i := E2KVFunc(elements[i])
// key_j, val_j := E2KVFunc(elements[j])
// key_i, val_i := fn(elements[i])
// key_j, val_j := fn(elements[j])
//
// 满足key_i == key_j 的情况,则在返回结果的resultMap中
// resultMap[key_i] = val_j
//
// 即使传入的字符串为nil,也保证返回的map是一个空map而不是nil
func MapWithE2KVFunc[Ele any, Key comparable, Val any](
func ToMapV[Ele any, Key comparable, Val any](
elements []Ele,
E2KVFunc func(element Ele) (Key, Val),
fn func(element Ele) (Key, Val),
) (resultMap map[Key]Val) {
resultMap = make(map[Key]Val)
if elements == nil {
return
}
for _, element := range elements {
k, v := E2KVFunc(element)
k, v := fn(element)
resultMap[k] = v
}
return
Expand Down
62 changes: 52 additions & 10 deletions slice/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ func ExampleFilterMap() {
// Output: [1 3]
}

func TestMapWithE2KVFunc(t *testing.T) {
func TestToMapV(t *testing.T) {
t.Run("integer-string to map[int]int", func(t *testing.T) {
elements := []string{"1", "2", "3", "4", "5"}
resMap := MapWithE2KVFunc(elements, func(str string) (int, int) {
resMap := ToMapV(elements, func(str string) (int, int) {
num, _ := strconv.Atoi(str)
return num, num
})
Expand Down Expand Up @@ -136,7 +136,7 @@ func TestMapWithE2KVFunc(t *testing.T) {
C: 2,
},
}
resMap := MapWithE2KVFunc(elements, func(ele eleType) (string, eleType) {
resMap := ToMapV(elements, func(ele eleType) (string, eleType) {
return ele.A, ele
})
epectedMap := map[string]eleType{
Expand Down Expand Up @@ -177,7 +177,7 @@ func TestMapWithE2KVFunc(t *testing.T) {
C: 3,
},
}
resMap := MapWithE2KVFunc(elements, func(ele eleType) (string, eleType) {
resMap := ToMapV(elements, func(ele eleType) (string, eleType) {
return ele.A, ele
})
epectedMap := map[string]eleType{
Expand All @@ -197,7 +197,7 @@ func TestMapWithE2KVFunc(t *testing.T) {

t.Run("传入nil slice,返回空map", func(t *testing.T) {
var elements []string = nil
resMap := MapWithE2KVFunc(elements, func(str string) (int, int) {
resMap := ToMapV(elements, func(str string) (int, int) {
num, _ := strconv.Atoi(str)
return num, num
})
Expand All @@ -206,10 +206,10 @@ func TestMapWithE2KVFunc(t *testing.T) {
})
}

func TestMapWithE2KFunc(t *testing.T) {
func TestToMap(t *testing.T) {
t.Run("integer-string to map[int]string", func(t *testing.T) {
elements := []string{"1", "2", "3", "4", "5"}
resMap := MapWithE2KFunc(elements, func(str string) int {
resMap := ToMap(elements, func(str string) int {
num, _ := strconv.Atoi(str)
return num
})
Expand Down Expand Up @@ -240,7 +240,7 @@ func TestMapWithE2KFunc(t *testing.T) {
C: 2,
},
}
resMap := MapWithE2KFunc(elements, func(ele eleType) string {
resMap := ToMap(elements, func(ele eleType) string {
return ele.A
})
epectedMap := map[string]eleType{
Expand Down Expand Up @@ -276,7 +276,7 @@ func TestMapWithE2KFunc(t *testing.T) {
C: 2,
},
}
resMap := MapWithE2KFunc(elements, func(ele eleType) string {
resMap := ToMap(elements, func(ele eleType) string {
return ele.A
})
epectedMap := map[string]eleType{
Expand All @@ -296,11 +296,53 @@ func TestMapWithE2KFunc(t *testing.T) {

t.Run("传入nil slice,返回空map", func(t *testing.T) {
var elements []string = nil
resMap := MapWithE2KFunc(elements, func(str string) int {
resMap := ToMap(elements, func(str string) int {
num, _ := strconv.Atoi(str)
return num
})
epectedMap := make(map[int]string)
assert.Equal(t, epectedMap, resMap)
})
}

func ExampleToMap() {
elements := []string{"1", "2", "3", "4", "5"}
resMap := ToMap(elements, func(str string) int {
num, _ := strconv.Atoi(str)
return num
})
fmt.Println(resMap)
// Output: map[1:1 2:2 3:3 4:4 5:5]
}

func ExampleToMapV() {
type eleType struct {
A string
B string
C int
}
type eleTypeOut struct {
A string
B string
}
elements := []eleType{
{
A: "a",
B: "b",
C: 1,
},
{
A: "c",
B: "d",
C: 2,
},
}
resMap := ToMapV(elements, func(ele eleType) (string, eleTypeOut) {
return ele.A, eleTypeOut{
A: ele.A,
B: ele.B,
}
})
fmt.Println(resMap)
// Output: map[a:{a b} c:{c d}]
}

0 comments on commit 0d2775e

Please sign in to comment.