1
\$\begingroup\$
# 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?

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Roland Illig Apr 28 at 7:53
  • 2
    \$\begingroup\$ After reading the documentation, MapSet.new(map.values()) is probably all you need. \$\endgroup\$ – Roland Illig Apr 28 at 8:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.