-
-
Notifications
You must be signed in to change notification settings - Fork 823
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
base: master
Are you sure you want to change the base?
Conversation
This issue can also be resolved in this way func main() {
inputMap := map[string]int{"foo": 1, "bar": 2, "baz": 3}
inputSlice := []string{"foo", "baz"}
result := lo.Map(inputSlice, func(key string, _ int) int {
return inputMap[key]
})
} But I think this is used more. |
map.go
Outdated
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 { |
There was a problem hiding this comment.
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 ??
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
Hi, can you review it @samber ? |
Co-authored-by: ccoVeille <[email protected]>
Co-authored-by: ccoVeille <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍
solve
#467