other: 优化 collection.map 相关函数签名,优化使用体验
This commit is contained in:
parent
af237448d7
commit
8d0cbed4f4
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue