From 0d2775ecd7611b2f338abf8391938443a25263a3 Mon Sep 17 00:00:00 2001 From: dxyinme Date: Sun, 31 Mar 2024 21:37:02 +0800 Subject: [PATCH] update --- slice/map.go | 26 ++++++++++---------- slice/map_test.go | 62 +++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 65 insertions(+), 23 deletions(-) diff --git a/slice/map.go b/slice/map.go index ec3c5f7..1e33061 100644 --- a/slice/map.go +++ b/slice/map.go @@ -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 diff --git a/slice/map_test.go b/slice/map_test.go index dea97e4..1af4058 100644 --- a/slice/map_test.go +++ b/slice/map_test.go @@ -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 }) @@ -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{ @@ -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{ @@ -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 }) @@ -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 }) @@ -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{ @@ -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{ @@ -296,7 +296,7 @@ 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 }) @@ -304,3 +304,45 @@ func TestMapWithE2KFunc(t *testing.T) { 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}] +}