Hi! I would like to propose introducing an `Enum.key_by/3` function (name to be discussed) that would return a map where:
- each key is the result of the first callback applied to an item
- the value is the result of the second callback applied to the item (identity by default)
It would basically be a copy of Enum.group_by/3 keeping only the last (or first?) value for each key instead of building a list.
Rationale:
While it could be achieved easily enough using Enum.map/2 and Map.new/1, or Enum.into/3, having a function for it could help making the code more explicit about the intent and therefore more readable. Like Enum.group_by/3, I think it is a fairly common operation and it might be a natural candidate for the standard library?
Example use case: from a list of Ecto records, create a map of records keyed by `id` for efficient lookups in future code:
User |> Repo.all() |> Enum.key_by(fn user -> user.id end)
If we want to build a map to lookup user names by their ids:
User |> Repo.all() |> Enum.key_by(fn user -> user.id end, fn user -> user.name end)
Further considerations:
The typical use cases I have in mind would rely on some kind of unique key, so I'm not sure what would be the best API-wise when dealing with duplicate keys and have no real opinion:
- keep the last value found for each key
- keep the first value found for each key
This behaviour could always be changed using `Enum.reverse/1` if needed.
What do you think?