Andres

Andres

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.

Showing Posts 1 to 2

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 OP

Hello @LostKobrakai

Very useful information. Thanks so much for your answer.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews