Created
April 24, 2014 02:33
-
-
Save d0rc/11239516 to your computer and use it in GitHub Desktop.
ListDict is dead, long live the ListDict
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 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