stevensonmt

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

adamu

:maps.intersect(m1, m2)
%{b: 3}

:maps.intersect_with(fn _k , v1, v2 -> v1 * v2 end, m1, m2)
%{b: 6}
josevalim

josevalim

Creator of Elixir

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

rvirding

Creator of Erlang

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

adamu

Just in case anybody has ideas:

Eiji

Eiji

All you have to do is a simple reducer. :smiling_imp:

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:

  1. Guides |> Typespecs @ Elixir documentation
  2. Enum.reduce/3
  3. Kernel.is_function/2
  4. Kernel.is_map/1
  5. Kernel.is_map_key/2
  6. Map.put/3

Where Next?

Popular in Discussions Top

New
rump13
Hi everyone, I’ve been following Elixir since around 2016 and tinkering with it on and off, but I haven’t had the opportunity to use it ...
New
bartblast
StackOverflow Survey results for 2025 have been published: Gleam - 2nd most admired language Elixir - 3rd most admired language Phoenix...
New
bglusman
I’m curious how others in the community deal with first class enums… especially crossing boundaries like database, code logic, and absin...
New
SyntaxSorcerer
This is the start of my Elixir learning journal/journey. I plan to share my journey in this thread and am open to all feedback, construct...
New
NKTgLaw
Hello fellow BEAM enthusiasts, I’d like to introduce a conceptual idea I’ve been developing, which I call NKTg Law. It’s a physics-inspi...
New
AstonJ
@Garrison’s comment in another thread reminded me of this post by Joe: With the big five exerting more control than ever, new (AI) play...
New
sym_num
I created a Forth processor in Elixir. This is my hobby project. https://github.com/sasagawa888/Forth
New
bartblast
It’s time to implement JavaScript interop for Hologram! Eventually, there will be Hologram wrappers for most APIs, but sometimes we’ll n...
New
kusokuzeshiki
I tried to generate pages with claude code and fluxon ui. I ask to claude code to generate real world page samples, the results are thes...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement