ValuesF helper renamed to PickByKeys

This commit is contained in:
Anatoly Ibragimov
2022-04-13 21:44:04 +02:00
parent 279689f73f
commit 0ed7583ceb
4 changed files with 8 additions and 6 deletions

2
.gitignore vendored
View File

@@ -34,3 +34,5 @@ go.work
cover.out cover.out
cover.html cover.html
.vscode .vscode
.idea/

View File

@@ -540,12 +540,12 @@ values := lo.Values[string, int](map[string]int{"foo": 1, "bar": 2})
// []int{1, 2} // []int{1, 2}
``` ```
### ValuesF ### PickByKeys
same as Values, but additionally filters map elements by provided keys. same as Values, but additionally filters map elements by provided keys.
```go ```go
valuesF := ValuesF[string, int](map[string]int{"foo": 1, "bar": 2, "baz": 3}, []string{"foo", "baz"}) pickByKeys := lo.PickByKeys[string, int](map[string]int{"foo": 1, "bar": 2, "baz": 3}, []string{"foo", "baz"})
// []int{1, 3} // []int{1, 3}
``` ```

4
map.go
View File

@@ -22,8 +22,8 @@ func Values[K comparable, V any](in map[K]V) []V {
return result return result
} }
// ValuesF same as Values, but additionally filters map elements by provided keys. // PickByKeys same as Values, but additionally filters map elements by provided keys.
func ValuesF[K comparable, V any](in map[K]V, keys []K) []V { func PickByKeys[K comparable, V any](in map[K]V, keys []K) []V {
r := make([]V, 0, len(in)) r := make([]V, 0, len(in))
for k, v := range in { for k, v := range in {
if Contains(keys, k) { if Contains(keys, k) {

View File

@@ -26,10 +26,10 @@ func TestValues(t *testing.T) {
is.Equal(r1, []int{1, 2}) is.Equal(r1, []int{1, 2})
} }
func TestValuesF(t *testing.T) { func TestPickByKeys(t *testing.T) {
is := assert.New(t) is := assert.New(t)
r1 := ValuesF[string, int](map[string]int{"foo": 1, "bar": 2, "baz": 3}, []string{"foo", "baz"}) r1 := PickByKeys[string, int](map[string]int{"foo": 1, "bar": 2, "baz": 3}, []string{"foo", "baz"})
sort.Ints(r1) sort.Ints(r1)
is.Equal(r1, []int{1, 3}) is.Equal(r1, []int{1, 3})