使用定义为原子或字符串的键在Map上匹配的最佳做法是什么?
%{"artist" => artist, "track" => track, "year" => year}
VS
%{artist: artist, track: track, year: year}
功能需要在两者上匹配:
def duplicate_post(%{"artist" => artist, "track" => track, "year" => year}) do
...
end
答案 0 :(得分:6)
我能想到的最好方法是两次定义函数,每种输入一次,然后从另一种调用其中一种。
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
答案 1 :(得分:1)
有一个Elixir库可以很好地包装地图,结构,列表和元组,以提供无关紧要的访问: