Thank you, I'm glad to see I'm not the only one finding myself needing this :)
> I think that we should keep the first value found for each key I really wasn't sure about which one would be more consistent with other Elixir APIs, I actually also have a branch keeping the first occurrence https://github.com/elixir-lang/elixir/compare/master...sabiwara:enum_key_by_first?expand=1 Le samedi 25 juillet 2020 11:47:55 UTC+9, Zachary Daniel a écrit : > > I like it, I write this function by hand all the time. I like the name, > and I think that we should keep the first value found for each key. > > On Friday, July 24, 2020 at 10:34:27 PM UTC-4 sabi...@gmail.com wrote: > >> 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/b113e3c5-eb81-4484-a499-60700fb5dacfo%40googlegroups.com.