Skip to content
New issue

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

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

Already on GitHub? # to your account

Provide methods to convert Tuple2 to Entry and Map. #597

Open
loggerhead opened this issue Feb 22, 2025 · 2 comments
Open

Provide methods to convert Tuple2 to Entry and Map. #597

loggerhead opened this issue Feb 22, 2025 · 2 comments

Comments

@loggerhead
Copy link

Suppose there are two arrays, aa and bb. I want to convert them into a map in the form of {a: b}. Currently, I need to write the code like this:

aToB := lo.FromEntries(lo.Map(lo.Zip2(aa, bb), func(kv lo.Tuple2[int64, string], _ int) lo.Entry[int64, string] {
	return lo.Entry[int64, string]{
		Key:   kv.A,
		Value: kv.B,
	}
}))

It would be much more convenient if there were a method that could directly convert lo.Tuple2 to lo.Entry.

@samber
Copy link
Owner

samber commented Feb 22, 2025

First, i wonder if lo.Entry struct is still useful, since we now have lo.Tuple2.

As a soft breaking change, we could imagine the following helpers:

  • lo.ToPairs(a map) -> []tuple2
  • lo.FromPairs([]tuple2) -> a map

With such helpers, your example would be simply:

aToB := lo.FromPairs(lo.Zip2(aa, bb))

Until then, you can do something like:

lo.SliceToMap(lo.Zip2(aa, bb), func(t lo.Tuple2[K, V]) (K, V) {
  return t.A, t.B
})

[edit]: Sorry, lo.FromPairs and lo.ToPairs helpers already exist

@loggerhead
Copy link
Author

It's great to replace lo.Entry with lo.Tuple2. They are kind of repetitive.

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

2 participants