Skip to content

Commit

Permalink
other: 优化 collection.map 相关函数签名,优化使用体验
Browse files Browse the repository at this point in the history
  • Loading branch information
kercylan98 committed Jan 12, 2024
1 parent af23744 commit 8d0cbed
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
4 changes: 2 additions & 2 deletions utils/collection/map.go
Original file line number Diff line number Diff line change
@@ -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
}
Expand All @@ -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
}
Expand Down
11 changes: 7 additions & 4 deletions utils/collection/map_example_test.go
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions utils/collection/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8d0cbed

Please sign in to comment.