Created
March 20, 2017 09:31
-
-
Save zubairshokh/d66476fd7643dcb5178f133464d87f9b to your computer and use it in GitHub Desktop.
Converts map to keyword list recursively
This file contains hidden or 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
defmodule MapToKeywordList do | |
def map_to_keyword_list(map), do: convert(map) | |
def convert(map) when is_map(map), do: Enum.map(map, fn {k,v} -> {String.to_atom(k),convert(v)} end) | |
def convert(v), do: v | |
end | |
# e.g. map= %{"a" => "aa", "b" => %{"c" => "cc", "d" => %{"ee" => "eee", "ff" => "fff"}}} | |
# to [a: "aa", b: [c: "cc", d: [ee: "eee", ff: "fff"]]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment