# Collect the values of a Map into a MapSet

# Collect the values of a map into a mapset
def values(map) do
Enum.reduce(map, MapSet.new(),
fn ({_, value}, acc) ->
MapSet.put(acc, value)
end
)
end

values.(%{"a"=>1, "b"=>1, "c"=>2}) |>
IO.inspect


Is there a cleaner way to write this values function?

• Does the Map provide access to its values as a collection? In Java I would express this as new Set(map.values()), using the Set constructor that copies elements from another collection. – Roland Illig Apr 28 '19 at 7:53
• After reading the documentation, MapSet.new(map.values()) is probably all you need. – Roland Illig Apr 28 '19 at 8:00