Skip to content

Instantly share code, notes, and snippets.

@keichan34
Last active September 17, 2016 09:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keichan34/ce01e7f34d141d069e02e808cc3f3025 to your computer and use it in GitHub Desktop.
Save keichan34/ce01e7f34d141d069e02e808cc3f3025 to your computer and use it in GitHub Desktop.
compile-time list
defmodule Hi do
keys = ~w(hello there)a
for {key, idx} <- Enum.with_index(keys) do
def index_of_key(unquote(key)) do
{:ok, unquote(idx)}
end
def key_at_index(unquote(idx)) do
{:ok, unquote(key)}
end
end
def index_of_key(_), do: :error
def key_at_index(_), do: :error
end
IO.inspect Hi.index_of_key(:hello)
IO.inspect Hi.index_of_key(:there)
IO.inspect Hi.index_of_key(:hallo?)
IO.inspect Hi.key_at_index(0)
IO.inspect Hi.key_at_index(1)
IO.inspect Hi.key_at_index(2)
{:ok, 0}
{:ok, 1}
:error
{:ok, :hello}
{:ok, :there}
:error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment