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 6 commits into
base: master
Choose a base branch
from

Conversation

XiaKuan
Copy link

@XiaKuan XiaKuan commented Jul 16, 2024

solve
#467

@XiaKuan
Copy link
Author

XiaKuan commented Jul 16, 2024

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.
Is it recommended to add this API?

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 {
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

@shivamrazorpay
Copy link
Contributor

Hi, can you review it @samber ?

README.md Outdated Show resolved Hide resolved
map.go Outdated Show resolved Hide resolved
XiaKuan and others added 3 commits July 20, 2024 10:32
Co-authored-by: ccoVeille <3875889+ccoVeille@users.noreply.github.com>
Co-authored-by: ccoVeille <3875889+ccoVeille@users.noreply.github.com>
Copy link
Contributor

@ccoVeille ccoVeille left a comment

Choose a reason for hiding this comment

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

LGTM 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants