Hello everyone,
I'd like to propose `Enum.index_by/2` (or potentially /3, see further down).
Essentially, it is supposed to be same thing was `Enum.group_by/3` but without putting the values into a list - so one key, one item.
It is inspired by the Rails Enumerable method of the same name [1].
Now, why should we have this?
I believe the use case, while not super common, is common enough and valid. Most often I see it when you have 2 different collections of data and you want to stitch them together. For instance, you have accounts of your users on another platform and you want to associate your users to those accounts - so you fetch your users, index them by their email as a key and then you can go through the other list effectively fitting them together with your user. Or take payment transactions in an external payment system and payment transactions as tracked in your application.
I'm not aware of a "nice" way to do this in Elixir right now. The "best" would probably be `Map.new/2` although most often I see people fall back to `Enum.reduce/3` which I consider sub optimal for most cases (thankfully most common usages of `Enum.reduce/3` usually have a better equivalent in the language itself).
While `Enum.group_by/2` does most of what I want here, transforming all the one element lists into just the element to easily work with it is somewhat annoying.
Speaking of which, it might be fair to say that if we want to do this we'd want `Enum.index_by/3` to mirror `Enum.group_by/3` (with an optional value function) which sounds good to me.
Happy to tackle the implementation myself.
Thank you + cheers,
Tobi
PS: Have a bunny