Regarding equivalence of items in a list Elixir

Enum.uniq is a function which removes the duplicates and returns the unique elements in the list.

For simple data types like integer, float, character, string, I am able to reason out the logic.

But when I did some testing, the Enum.uniq worked for maps, even user defined structures etc.

How is this done? The code when traced leads to :sets.add of Erlang and not able to proceed after that

Sincerely,
Suds

Hi Suds, it will come down to equality tests for each data type. So for maps, for example,

%{a: 1, b: 2} == %{b: 2, a: 1} # returns true
%{a: 1, b: 2} == %{:b => 2, a: 1} # also true

structs are basically just maps, so the same applies.

I suspect it is doing s strict equality test behind the scenes. You can read about equality tests here: Kernel — Elixir v1.17.3

How this maps to the Erlang :set code I don’t know without digging, but I would guess the strict equality check is implemented in Erlang rather than Elixir.

The Enum functions work for any type that implements the Enumerable protocol. Which maps do implement.

1 Like