Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat<map> add ValuesByKeys for get values array in order [issue - 467] #494

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ Supported helpers for maps:
- [PickBy](#pickby)
- [PickByKeys](#pickbykeys)
- [PickByValues](#pickbyvalues)
- [ValuesByKeys](#ValuesByKeys)
- [OmitBy](#omitby)
- [OmitByKeys](#omitbykeys)
- [OmitByValues](#omitbyvalues)
Expand Down Expand Up @@ -1120,6 +1121,18 @@ m := lo.PickByValues(map[string]int{"foo": 1, "bar": 2, "baz": 3}, []int{1, 3})

[[play](https://go.dev/play/p/1zdzSvbfsJc)]

### ValuesByKeys

Returns an array of values in the same order as the given keys.

```go

m := lo.ValuesByKeys(map[string]int{"foo": 1, "bar": 2, "baz": 3}, []string{"baz", "foo", "bar"})
XiaKuan marked this conversation as resolved.
Show resolved Hide resolved
// []int{3,1,2}
```

[[play](https://go.dev/play/p/PmmPDo1AqWl)]

### OmitBy

Returns same map type filtered by given predicate.
Expand Down
12 changes: 12 additions & 0 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ func PickByValues[K comparable, V comparable, Map ~map[K]V](in Map, values []V)
return r
}

// ValuesByKeys returns an array of values in the same order as the given keys.
// Play: https://go.dev/play/p/PmmPDo1AqWl
func ValuesByKeys[K comparable, V any](in map[K]V, keys []K) []V {
out := make([]V, 0, len(keys))
for i := range keys {
if v, ok := in[keys[i]]; ok {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a random doubt: If the key is not present in the map, then the order in the output slice will not be aligned to the input keys.
You can add that edge case in the example of Readme.
Your thoughts on it ??

Copy link
Author

@XiaKuan XiaKuan Jul 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your suggestion!
Another way would be to append nil or zero values to the slice,
This way, the order in the output slices is aligned with the input keys.
The focus of this method is on order information, and I think this is a better way to do it
WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @shivamrazorpay

It's the first thing I thought about

I would expect to return an error in such case

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems reasonable.

I have resubmitted the code

out = append(out, v)
}
}
return out
}

// OmitBy returns same map type filtered by given predicate.
// Play: https://go.dev/play/p/EtBsR43bdsd
func OmitBy[K comparable, V any, Map ~map[K]V](in Map, predicate func(key K, value V) bool) Map {
Expand Down
9 changes: 9 additions & 0 deletions map_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ func ExamplePickByValues() {
// Output: 2 1 3
}

func ExampleValuesByKeys() {
kv := map[string]int{"foo": 1, "bar": 2, "baz": 3}

result := ValuesByKeys(kv, []string{"baz", "foo", "bar"})

fmt.Printf("%v %v %v", len(result), result[0], result[1])
// Output: 3 3 1
}

func ExampleOmitBy() {
kv := map[string]int{"foo": 1, "bar": 2, "baz": 3}

Expand Down
13 changes: 13 additions & 0 deletions map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,19 @@ func TestPickByValues(t *testing.T) {
is.IsType(after, before, "type preserved")
}

func TestValuesByKeys(t *testing.T) {
t.Parallel()
is := assert.New(t)

result1 := ValuesByKeys(map[string]int{"foo": 1, "bar": 2, "baz": 3}, []string{"baz", "foo", "bar"})
result2 := ValuesByKeys(map[string]int{"": 0, "foobar": 6, "baz": 3}, []string{"baz", "foobar"})

is.Equal(len(result1), 3)
is.Equal(len(result2), 2)
is.ElementsMatch(result1, []int{3, 1, 2})
is.ElementsMatch(result2, []int{3, 6})
}

func TestOmitBy(t *testing.T) {
t.Parallel()
is := assert.New(t)
Expand Down