Skip to content

Instantly share code, notes, and snippets.

@zubairshokh
Created March 20, 2017 09:31
Show Gist options
  • Save zubairshokh/d66476fd7643dcb5178f133464d87f9b to your computer and use it in GitHub Desktop.
Save zubairshokh/d66476fd7643dcb5178f133464d87f9b to your computer and use it in GitHub Desktop.
Converts map to keyword list recursively
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