Skip to content

Instantly share code, notes, and snippets.

@joeyates
Created July 9, 2017 11:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joeyates/56dbfbe9ca73466898600a066cfefb9f to your computer and use it in GitHub Desktop.
Save joeyates/56dbfbe9ca73466898600a066cfefb9f to your computer and use it in GitHub Desktop.
Merge Elixir keyword lists and maps
defp combine(list1, list2) do
Keyword.merge(keyword_list(list1), keyword_list(list2), fn _k, _v1, v2 ->
v2
end)
end
def keyword_pair({key, value}) when is_atom(key) do
{key, value}
end
def keyword_pair({key, value}) do
{String.to_atom(key), value}
end
defp keyword_list(map) when is_map(map) do
Enum.map(map, &__MODULE__.keyword_pair/1)
end
defp keyword_list(list), do: list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment