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.

1 Like

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.

3 Likes

Hello @LostKobrakai

Very useful information. Thanks so much for your answer.

1 Like