What is the best practice to match on a Map with keys defined as either atoms or strings?
%{"artist" => artist, "track" => track, "year" => year}
vs
%{artist: artist, track: track, year: year}
Function needs to match on both:
def duplicate_post(%{"artist" => artist, "track" => track, "year" => year}) do
...
end
The best way I can think of is to define the function twice, once for each kind of input, and call one of them from the other.
def duplicate_post(%{"artist" => artist, "track" => track, "year" => year}) do
duplicate_post(%{artist: artist, track: track, year: year})
end
def duplicate_post(%{artist: artist, track: track, year: year}) do
...
end
There is an Elixir library which nicely wraps maps, structs, lists and tuples to provide indifferent access:
https://github.com/vic/indifferent
https://hex.pm/packages/indifferent
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With