From 8d0cbed4f4738c4378a03e8cfd2e37813b9ae7a2 Mon Sep 17 00:00:00 2001 From: kercylan98 Date: Fri, 12 Jan 2024 17:14:01 +0800 Subject: [PATCH] =?UTF-8?q?other:=20=E4=BC=98=E5=8C=96=20collection.map=20?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E5=87=BD=E6=95=B0=E7=AD=BE=E5=90=8D=EF=BC=8C?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BD=BF=E7=94=A8=E4=BD=93=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/collection/map.go | 4 ++-- utils/collection/map_example_test.go | 11 +++++++---- utils/collection/random.go | 8 ++++---- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/utils/collection/map.go b/utils/collection/map.go index 10b3eeb..6a8a72b 100644 --- a/utils/collection/map.go +++ b/utils/collection/map.go @@ -1,7 +1,7 @@ package collection // MappingFromSlice 将切片中的元素进行转换 -func MappingFromSlice[S ~[]V, NS ~[]N, V, N any](slice S, handler func(value V) N) NS { +func MappingFromSlice[S ~[]V, NS []N, V, N any](slice S, handler func(value V) N) NS { if slice == nil { return nil } @@ -13,7 +13,7 @@ func MappingFromSlice[S ~[]V, NS ~[]N, V, N any](slice S, handler func(value V) } // MappingFromMap 将 map 中的元素进行转换 -func MappingFromMap[M ~map[K]V, NM ~map[K]N, K comparable, V, N any](m M, handler func(value V) N) NM { +func MappingFromMap[M ~map[K]V, NM map[K]N, K comparable, V, N any](m M, handler func(value V) N) NM { if m == nil { return nil } diff --git a/utils/collection/map_example_test.go b/utils/collection/map_example_test.go index e51b876..d26b712 100644 --- a/utils/collection/map_example_test.go +++ b/utils/collection/map_example_test.go @@ -1,9 +1,12 @@ -package collection +package collection_test -import "fmt" +import ( + "fmt" + "github.com/kercylan98/minotaur/utils/collection" +) func ExampleMappingFromSlice() { - result := MappingFromSlice[[]int, []int]([]int{1, 2, 3}, func(value int) int { + result := collection.MappingFromSlice([]int{1, 2, 3}, func(value int) int { return value + 1 }) fmt.Println(result) @@ -12,7 +15,7 @@ func ExampleMappingFromSlice() { } func ExampleMappingFromMap() { - result := MappingFromMap[map[int]int, map[int]int](map[int]int{1: 1, 2: 2, 3: 3}, func(value int) int { + result := collection.MappingFromMap(map[int]int{1: 1, 2: 2, 3: 3}, func(value int) int { return value + 1 }) fmt.Println(result) diff --git a/utils/collection/random.go b/utils/collection/random.go index cdb8816..03e43fe 100644 --- a/utils/collection/random.go +++ b/utils/collection/random.go @@ -101,7 +101,7 @@ func ChooseRandomMapKeyRepeatN[M ~map[K]V, K comparable, V any](m M, n int) (res return } -// ChooseRandomMapValueRepeatN 获取 map 中的 n 个随机 inputV,允许重复 +// ChooseRandomMapValueRepeatN 获取 map 中的 n 个随机 n,允许重复 // - 如果 n 大于 map 长度或小于 0 时将会发生 panic func ChooseRandomMapValueRepeatN[M ~map[K]V, K comparable, V any](m M, n int) (result []V) { if m == nil { @@ -150,7 +150,7 @@ func ChooseRandomMapKey[M ~map[K]V, K comparable, V any](m M) (k K) { return } -// ChooseRandomMapValue 获取 map 中的随机 inputV +// ChooseRandomMapValue 获取 map 中的随机 value func ChooseRandomMapValue[M ~map[K]V, K comparable, V any](m M) (v V) { if m == nil { return @@ -182,8 +182,8 @@ func ChooseRandomMapKeyN[M ~map[K]V, K comparable, V any](m M, n int) (result [] return } -// ChooseRandomMapValueN 获取 map 中的 inputN 个随机 inputV -// - 如果 inputN 大于 map 长度或小于 0 时将会发生 panic +// ChooseRandomMapValueN 获取 map 中的 n 个随机 value +// - 如果 n 大于 map 长度或小于 0 时将会发生 panic func ChooseRandomMapValueN[M ~map[K]V, K comparable, V any](m M, n int) (result []V) { if m == nil { return