feat<map> add ValuesByKeys for get values array in order [issue - 467]#494
feat<map> add ValuesByKeys for get values array in order [issue - 467]#494XiaKuan wants to merge 7 commits intosamber:masterfrom
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.
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.
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.
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.
That seems reasonable.
I have resubmitted the code
| Hi, can you review it @samber ? |
Co-authored-by: ccoVeille <3875889+ccoVeille@users.noreply.github.com>
Co-authored-by: ccoVeille <3875889+ccoVeille@users.noreply.github.com>
| Instead of returning error, I would return the list of missing keys. Also, i'm not even sure that returning a second argument is suitable. Because such helpers cannot be chained or used in argument to a function. |
solve
#467