Skip to content

Instantly share code, notes, and snippets.

View zubairshokh's full-sized avatar

Zubair Nabi zubairshokh

View GitHub Profile
zubairshokh / map_to_keyword_list.exs
Created March 20, 2017 09:31
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:, fn {k,v} -> {String.to_atom(k),convert(v)} end)
def convert(v), do: v
# e.g. map= %{"a" => "aa", "b" => %{"c" => "cc", "d" => %{"ee" => "eee", "ff" => "fff"}}}
zubairshokh / string_to_atom.exs
Last active July 30, 2018 12:24
Converts map with keys as strings to keys as atoms.
defmodule Service.MiscScripts do
@doc """
Changes String Map to Map of Atoms e.g. %{"c"=> "d", "x" => %{"yy" => "zz"}} to
%{c: "d", x: %{yy: "zz"}}, i.e changes even the nested maps.
def convert_to_atom_map(map), do: to_atom_map(map)
defp to_atom_map(map) when is_map(map), do:, fn {k,v} -> {String.to_atom(k),to_atom_map(v)} end)
zubairshokh / nested.exs
Last active February 1, 2017 17:23
Extract key, value from nested maps or list of maps
defmodule Nested do
def get_inner_element(input) do,0)
|> __MODULE__.get_map(["accessConfigs"] )
|> __MODULE__.get_element_from_map("natIP")
def get_map(map,[head|tail]) do
map[head] |> get_map tail