diff --git a/docs/api/packages/maputil.md b/docs/api/packages/maputil.md index 8173a0d..2f32e1d 100644 --- a/docs/api/packages/maputil.md +++ b/docs/api/packages/maputil.md @@ -78,7 +78,7 @@ import ( - [GetOrSet](#GetOrSet) - [SortByKey](#SortByKey) - [GetOrDefault](#GetOrDefault) - +- [FindValuesBy](#FindValuesBy)
@@ -2307,4 +2307,43 @@ func main() { // a // default } +``` + +### FindValuesBy + +

返回一个切片,包含满足给定谓词判断函数的map中的值。

+ +函数签名: + +```go +func FindValuesBy[K comparable, V any](m map[K]V, predicate func(key K, value V) bool) []V +``` + +示例:[运行](todo) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/maputil" +) + +func main() { + m := map[int]string{ + 1: "a", + 2: "b", + 3: "c", + 4: "d", + } + + result := maputil.FindValuesBy(m, func(k int, v string) bool { + return k%2 == 0 + }) + + fmt.Println(result) + + // Output: + // [b d] +} ``` \ No newline at end of file diff --git a/docs/en/api/packages/maputil.md b/docs/en/api/packages/maputil.md index ccaa86b..31d6983 100644 --- a/docs/en/api/packages/maputil.md +++ b/docs/en/api/packages/maputil.md @@ -79,6 +79,7 @@ import ( - [GetOrSet](#GetOrSet) - [SortByKey](#SortByKey) - [GetOrDefault](#GetOrDefault) +- [FindValuesBy](#FindValuesBy)
@@ -2276,8 +2277,8 @@ func main() { } result := maputil.SortByKey(m, func(a, b int) bool { - return a < b - }) + return a < b + }) fmt.Println(result) @@ -2288,7 +2289,7 @@ func main() { ### GetOrDefault -

returns the value of the given key or a default value if the key is not present.

+

Returns the value of the given key or a default value if the key is not present.

Signature: @@ -2296,7 +2297,7 @@ func main() { func GetOrDefault[K comparable, V any](m map[K]V, key K, defaultValue V) V ``` -Example:[运行](https://go.dev/play/p/99QjSYSBdiM) +Example:[Run](https://go.dev/play/p/99QjSYSBdiM) ```go package main @@ -2324,4 +2325,43 @@ func main() { // a // default } +``` + +### FindValuesBy + +

Returns a slice of values from the map that satisfy the given predicate function.

+ +Signature: + +```go +func FindValuesBy[K comparable, V any](m map[K]V, predicate func(key K, value V) bool) []V +``` + +Example:[Run](todo) + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/maputil" +) + +func main() { + m := map[int]string{ + 1: "a", + 2: "b", + 3: "c", + 4: "d", + } + + result := maputil.FindValuesBy(m, func(k int, v string) bool { + return k%2 == 0 + }) + + fmt.Println(result) + + // Output: + // [b d] +} ``` \ No newline at end of file diff --git a/maputil/map.go b/maputil/map.go index db35190..05629aa 100644 --- a/maputil/map.go +++ b/maputil/map.go @@ -666,3 +666,17 @@ func GetOrDefault[K comparable, V any](m map[K]V, key K, defaultValue V) V { } return defaultValue } + +// FindValuesBy returns a slice of values from the map that satisfy the given predicate function. +// Play: todo +func FindValuesBy[K comparable, V any](m map[K]V, predicate func(key K, value V) bool) []V { + result := make([]V, 0) + + for k, v := range m { + if predicate(k, v) { + result = append(result, v) + } + } + + return result +} diff --git a/maputil/map_example_test.go b/maputil/map_example_test.go index 72ef969..f18b00f 100644 --- a/maputil/map_example_test.go +++ b/maputil/map_example_test.go @@ -829,3 +829,21 @@ func ExampleOrderedMap_UnmarshalJSON() { // fmt.Println(om.Elements()) } + +func ExampleFindValuesBy() { + m := map[int]string{ + 1: "a", + 2: "b", + 3: "c", + 4: "d", + } + + result := FindValuesBy(m, func(k int, v string) bool { + return k%2 == 0 + }) + + fmt.Println(result) + + // Output: + // [b d] +} diff --git a/maputil/map_test.go b/maputil/map_test.go index 4a6c5f2..b85fc47 100644 --- a/maputil/map_test.go +++ b/maputil/map_test.go @@ -888,3 +888,41 @@ func TestGetOrDefault(t *testing.T) { result2 := GetOrDefault(m1, 5, "123") assert.Equal("123", result2) } + +func TestFindValuesBy(t *testing.T) { + t.Parallel() + + assert := internal.NewAssert(t, "TestFindValuesBy") + + tests := []struct { + name string + inputMap map[string]string + key string + expected []string + }{ + { + name: "Key exists", + inputMap: map[string]string{"a": "1", "b": "2", "c": "3"}, + key: "b", + expected: []string{"2"}, + }, + { + name: "Key does not exist", + inputMap: map[string]string{"a": "1", "b": "2", "c": "3"}, + key: "d", + expected: []string{}, + }, + { + name: "Empty map", + inputMap: map[string]string{}, + key: "a", + expected: []string{}, + }, + } + for _, tt := range tests { + result := FindValuesBy(tt.inputMap, func(key string, value string) bool { + return key == tt.key + }) + assert.Equal(tt.expected, result) + } +}