Andres

Andres

Trying to understand the source code of the intersection function of the Mapset module

Hello.

I apologize if my questions are very basic.
I am trying to understand the source code of the intersection function of the Mapset module.
I have encountered many behaviors that I do not understand.

First I would like to understand the following behavior:

a = MapSet.new([1, 2, 3, 5])
b = MapSet.new([3, 4, 5])
c = %MapSet{map: a}

IO.inspect(c)
# #MapSet<[:__struct__, :map, :version]>

I searched in Google and in my Elixir books for that result and unfortunately I did not find any relevant information.

I can see that in #MapSet <[: __ struct__,: map,: version]> there is a :map atom key.
The intersection function makes use of that key.

If I try to change the :map key to another name I get a KeyError

d = %MapSet{other: a}
# ** (KeyError) key :other not found
#    (elixir) expanding struct: MapSet.__struct__/1
#    main.exs:34: (file)

Intersection function:

a = MapSet.new([1, 2, 3, 5])
b = MapSet.new([3, 4, 5])
defp order_by_size(map1, map2) when map_size(map1) > map_size(map2), do: {map2, map1}
defp order_by_size(map1, map2), do: {map1, map2}

# If I inspect the parameters of the intersection function 
# I get the following results:
def intersection(%MapSet{map: map1} = map_set, %MapSet{map: map2}) do
  IO.inspect(map1)
  # %{1 => [], 2 => [], 3 => [], 5 => []}
  # How is it possible that a Map was generated with the MapSet values as its keys?

  # #MapSet<[1, 2, 3, 5]>
  IO.inspect(map_set)

  # %{3 => [], 4 => [], 5 => []}
  IO.inspect(map2)

  # I understand this part of the code 。^‿^。
  {map1, map2} = order_by_size(map1, map2)

  # I would like to know why it is possible to interact with two different data structures:
  # #MapSet <[1, 2, 3, 5]> and % {1 => [], 2 => [], 3 => [], 5 = > []}
  # Why we surround map_set with a Map% {} struct and still return a MapSet?
  %{map_set | map: Map.take(map2, Map.keys(map1))}
end

In the end it seems that a Mapset is an undercover Map. :male_detective:
Thanks.

Most Liked

LostKobrakai

LostKobrakai

That’s exactly what it is, hence the name “MapSet”. But a map alone cannot guarantee that the constraints of being a set are abided for. There simply isn’t a native set datatype on the beam. If you look at MapSet.t the type for MapSets you’ll notice that it’s a “opaque” type. This means you’re not supposed to directly interact with any MapSet other than by using functions on the MapSet module. It alone can guarantee that you’re not breaking the data from being a set. Also in theory this allows MapSet to change the implementation at any time without breaking code using them.

Andres

Andres

Hello @LostKobrakai

Very useful information. Thanks so much for your answer.

Where Next?

Popular in Questions Top

nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
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
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
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
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New

Other popular topics Top

skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
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
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52673 488
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
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 127089 1222
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54120 245
New

We're in Beta

About us Mission Statement