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? I have a working branch for this here, happy to open a PR if you are interested: https://github.com/elixir-lang/elixir/compare/master...sabiwara:enum_key_by?expand=1 -- You received this message because you are subscribed to the Google Groups "elixir-lang-core" group. To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/2a5608f1-2772-467e-a989-06c2c95d0633o%40googlegroups.com.