Skip to content

Instantly share code, notes, and snippets.

@keichan34 keichan34/hi.exs
Last active Sep 17, 2016

Embed
What would you like to do?
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
You can’t perform that action at this time.