stevensonmt
Map.intersection/2?
Has anyone else ever wanted to merge two maps, but have the resulting map only include keys common to both maps? I think of it as analogous to MapSet.intersection but with a function for handling the values.
m1 = %{a: 1, b:2}
m2 = %{b: 3, c:4}
Map.intersection(m1, m2, fn _k, v1, v2 -> v1 * v2 end)
> %{b: 6}
This would replace
Map.merge(m1, m2, fn _k , v1, v2 -> v1 * v2 end)
|> Enum.filter(fn {k,_v} -> [m1, m2] |> Enum.all?(&Map.has_key?(&1, k)) end)
|> Map.new()
>%{b: 6}
Thoughts?
Most Liked
adamu
:maps.intersect(m1, m2)
%{b: 3}
:maps.intersect_with(fn _k , v1, v2 -> v1 * v2 end, m1, m2)
%{b: 6}
josevalim
Elixir main requires Erlang/OTP 24. Therefore if someone wants to submit a PR that adds intersect/2 and intersect/3, it will be welcome.
rvirding
There is only ONE type of map in the BEAM. The maps in Elixir are exactly the same maps as in Erlang. Seeing Erlang is the base language on the BEAM Elixir could not have had maps before the BEAM and Erlang implemented them.
iex(1)> m1 = %{a: 1, b: 1, c: 1}
%{a: 1, b: 1, c: 1}
iex(2)> m2 = %{a: 2 , c: 2, d: 2}
%{a: 2, c: 2, d: 2}
iex(3)> mi = :maps.intersect(m1, m2)
%{a: 2, c: 2}
iex(4)> :io.write(m1)
#{a => 1,b => 1,c => 1}:ok
iex(5)> :io.write(m2)
#{a => 2,c => 2,d => 2}:ok
iex(6)> :io.write(m1)
#{a => 1,b => 1,c => 1}:ok
The :io.write function is an output function in the Erlang io module and prints the maps which shows they are the same maps as in Erlang.
The :maps.intersect/2 function first came in OTP 24.0 so it may not be used yet in the Map module.
adamu
Just in case anybody has ideas:
Eiji
All you have to do is a simple reducer. ![]()
defmodule Example do
@spec sample(map(), map(), (Map.key(), Map.value(), Map.value() -> Map.value())) :: map()
def sample(left, right, func) when is_map(left) and is_map(right) and is_function(func, 3) do
Enum.reduce(left, %{}, fn
{key, value}, acc when is_map_key(right, key) ->
Map.put(acc, key, func.(key, value, right[key]))
_pair, acc ->
acc
end)
end
end
iex> Example.sample(%{a: 1, b: 2}, %{b: 3, c: 4}, fn _k, v1, v2 -> v1 * v2 end)
%{b: 6}
Helpful resources:







