Re: [elixir-core:5920] Re: Proposal: Map.filter_values and Map.filter_keys

2016-06-18 Thread Ben Wilson
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 t

Re: [elixir-core:5918] Re: Proposal: Map.filter_values and Map.filter_keys

2016-06-18 Thread Allen Madsen
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 wrote: > Well I tried to reply but my email was deleted. > > Long story short, successive Map.put calls are actually inferio

[elixir-core:5918] Re: Proposal: Map.filter_values and Map.filter_keys

2016-06-18 Thread Ben Wilson
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 actuall

[elixir-core:5917] Re: Proposal: Map.filter_values and Map.filter_keys

2016-06-18 Thread Filip Haglund
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+

[elixir-core:5916] Re: Proposal: Map.filter_values and Map.filter_keys

2016-06-18 Thread Filip Haglund
Sorry, I was unclear. The example implementation was an example of how the function would work, not how it would be implemented. I was imagining an implementation that didn't build an intermediate list. Is building a new map each step too expensive for this to be considered composable? There's

[elixir-core:5915] Re: Proposal: Map.filter_values and Map.filter_keys

2016-06-18 Thread Ben Wilson
Your example implementation shows why this isn't really necessary. Moreover it doesn't compose well. Consider: map |> Enum.filter(fn {k, v} -> key_filter(k) && value_filter(v) end) |> Map.new vs map |> Map.filter_keys(key_filter) |> Map.filter_values(value_filter) In the latter it starts as a

[elixir-core:5914] Proposal: Map.filter_values and Map.filter_keys

2016-06-18 Thread Filip Haglund
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