In my journey to learning Elixir, I gave myself the task of implementing an
algorithm that obtains the intersection of two MapSets without using the
a = MapSet.new([3, 3, 3, 2, 2, 1]) b = MapSet.new([3, 4, 5]) # => Expected result #MapSet<> def intersection_a(a, b) do Enum.reduce(b, MapSet.new(), fn x, acc -> if MapSet.member?(a, x) do MapSet.put(acc, x) else acc end end) end def intersection_b(a, b) do # Returns a List instead of #MapSet<> Enum.filter(b, fn x -> MapSet.member?(a, x) end) end
intersection_b function returns a List instead of a MapSet.
Why does this happen?
Any suggestions to improve the algorithm is welcome.