Created
July 9, 2017 11:05
-
-
Save joeyates/56dbfbe9ca73466898600a066cfefb9f to your computer and use it in GitHub Desktop.
Merge Elixir keyword lists and maps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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