Skip to content

Instantly share code, notes, and snippets.

@d0rc
Created April 24, 2014 02:33
Show Gist options
  • Select an option

  • Save d0rc/11239516 to your computer and use it in GitHub Desktop.

Select an option

Save d0rc/11239516 to your computer and use it in GitHub Desktop.
ListDict is dead, long live the ListDict
defmodule LAX do
defmodule Transformer do
def exec({ {:., _, [Kernel, :access]} , misc, [subj, [key]]}) do
quote do
LAX.getter(unquote(exec(subj)), unquote(exec(key)))
end
end
def exec(code), do: code
end
defp scan_proplist([], _), do: nil
defp scan_proplist([{key, value}|rest], key), do: value
defp scan_proplist([_|rest], key), do: scan_proplist(rest, key)
def getter(subject, key) when not(is_atom(key)) and is_list(subject) do
scan_proplist(subject, key)
end
def getter(subject, key) do
Dict.get(subject, key)
end
defmacro la(code) do
Transformer.exec(code)
end
end
require LAX
LAX.la ([a: 1, b: 2])[:a]
LAX.la ([{"hello", "world"}, {"goodbye", ListDict}])["goodbye"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment