mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-13 17:22:27 +08:00
feat: add FindValuesBy
This commit is contained in:
@@ -78,7 +78,7 @@ import (
|
|||||||
- [GetOrSet](#GetOrSet)
|
- [GetOrSet](#GetOrSet)
|
||||||
- [SortByKey](#SortByKey)
|
- [SortByKey](#SortByKey)
|
||||||
- [GetOrDefault](#GetOrDefault)
|
- [GetOrDefault](#GetOrDefault)
|
||||||
|
- [FindValuesBy](#FindValuesBy)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -2308,3 +2308,42 @@ func main() {
|
|||||||
// default
|
// default
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="FindValuesBy">FindValuesBy</span>
|
||||||
|
|
||||||
|
<p>返回一个切片,包含满足给定谓词判断函数的map中的值。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func FindValuesBy[K comparable, V any](m map[K]V, predicate func(key K, value V) bool) []V
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:<span style="float:right;display:inline-block;">[运行](todo)</span></b>
|
||||||
|
|
||||||
|
```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]
|
||||||
|
}
|
||||||
|
```
|
||||||
@@ -79,6 +79,7 @@ import (
|
|||||||
- [GetOrSet](#GetOrSet)
|
- [GetOrSet](#GetOrSet)
|
||||||
- [SortByKey](#SortByKey)
|
- [SortByKey](#SortByKey)
|
||||||
- [GetOrDefault](#GetOrDefault)
|
- [GetOrDefault](#GetOrDefault)
|
||||||
|
- [FindValuesBy](#FindValuesBy)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -2276,8 +2277,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
result := maputil.SortByKey(m, func(a, b int) bool {
|
result := maputil.SortByKey(m, func(a, b int) bool {
|
||||||
return a < b
|
return a < b
|
||||||
})
|
})
|
||||||
|
|
||||||
fmt.Println(result)
|
fmt.Println(result)
|
||||||
|
|
||||||
@@ -2288,7 +2289,7 @@ func main() {
|
|||||||
|
|
||||||
### <span id="GetOrDefault">GetOrDefault</span>
|
### <span id="GetOrDefault">GetOrDefault</span>
|
||||||
|
|
||||||
<p>returns the value of the given key or a default value if the key is not present.</p>
|
<p>Returns the value of the given key or a default value if the key is not present.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
@@ -2296,7 +2297,7 @@ func main() {
|
|||||||
func GetOrDefault[K comparable, V any](m map[K]V, key K, defaultValue V) V
|
func GetOrDefault[K comparable, V any](m map[K]V, key K, defaultValue V) V
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/99QjSYSBdiM)</span></b>
|
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/99QjSYSBdiM)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -2325,3 +2326,42 @@ func main() {
|
|||||||
// default
|
// default
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="FindValuesBy">FindValuesBy</span>
|
||||||
|
|
||||||
|
<p>Returns a slice of values from the map that satisfy the given predicate function.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func FindValuesBy[K comparable, V any](m map[K]V, predicate func(key K, value V) bool) []V
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:<span style="float:right;display:inline-block;">[Run](todo)</span></b>
|
||||||
|
|
||||||
|
```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]
|
||||||
|
}
|
||||||
|
```
|
||||||
@@ -666,3 +666,17 @@ func GetOrDefault[K comparable, V any](m map[K]V, key K, defaultValue V) V {
|
|||||||
}
|
}
|
||||||
return defaultValue
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -829,3 +829,21 @@ func ExampleOrderedMap_UnmarshalJSON() {
|
|||||||
|
|
||||||
// fmt.Println(om.Elements())
|
// 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]
|
||||||
|
}
|
||||||
|
|||||||
@@ -888,3 +888,41 @@ func TestGetOrDefault(t *testing.T) {
|
|||||||
result2 := GetOrDefault(m1, 5, "123")
|
result2 := GetOrDefault(m1, 5, "123")
|
||||||
assert.Equal("123", result2)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user