Stepping back for a moment, there are a lot of functions in the Enum module that apply to maps. Are we to implement all of them in the Map module just to avoid having to call Map.new after our various Enum calls?
The way things work at the moment is that when we need to take one item out at a time we convert the map to a list. This is not only semantically sensible, it's also efficient and fast. Once we have that list we leave it as a list so that we can easily compose these operations. If we no longer want a list, we can build a new map. This is also the fastest way to go about this. On Saturday, June 18, 2016 at 9:26:20 PM UTC-4, Allen Madsen wrote: > > I have used the pattern this code wraps often and would use this > feature if it existed. > Allen Madsen > http://www.allenmadsen.com > > > On Sat, Jun 18, 2016 at 8:39 PM, Ben Wilson <[email protected] > <javascript:>> wrote: > > Well I tried to reply but my email was deleted. > > > > Long story short, successive Map.put calls are actually inferior to > walking > > intermediary lists and building a new map at the end. You can read more > > about it here: https://github.com/elixir-lang/elixir/pull/4415 > > > > You'll note that Map.take is actually built in just that way: > > > https://github.com/elixir-lang/elixir/blob/master/lib/elixir/lib/map.ex#L330 > > > > On Saturday, June 18, 2016 at 4:49:04 PM UTC-4, Filip Haglund wrote: > >> > >> Here's another implementation, using Enum.reduce, without an > intermediate > >> list: > >> > >> def map_filter_values(map, func) do > >> Enum.reduce map, %{}, fn {key, val}, acc -> > >> if func.(val) do > >> Map.put(acc, key, val) > >> else > >> acc > >> end > >> end > >> end > >> > >> > >> On Saturday, June 18, 2016 at 6:27:07 PM UTC+2, Filip Haglund wrote: > >>> > >>> I wish there was a Map.filter_values function that would filter on > keys > >>> or values, but leave the other one intact. This seems like something > that > >>> should be in the standard library. > >>> > >>> Example implementation of a Map.filter_values that would filter a map > >>> based on its values, leaving the keys intact: > >>> > >>> @doc """ > >>> Filter map based on its values, leaving the corresponding keys intact. > >>> """ > >>> @spec map_filter_values(Map.t, (any -> boolean)) :: Map.t > >>> def map_filter_values(map, func) do > >>> map |> Enum.filter(fn {_, val} -> func.(val) end) |> Map.new > >>> end > >>> > >>> > >>> > >>> A corresponding Map.filter_keys would also be nice. > > > > -- > > 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 [email protected] <javascript:>. > > To view this discussion on the web visit > > > https://groups.google.com/d/msgid/elixir-lang-core/58c89485-0334-45ca-95f9-2a519060fc41%40googlegroups.com. > > > > > > For more options, visit https://groups.google.com/d/optout. > -- 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 [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/1a84ccf3-6565-4871-8eec-9f7b3e89be45%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
