Last active
September 17, 2016 09:57
-
-
Save keichan34/ce01e7f34d141d069e02e808cc3f3025 to your computer and use it in GitHub Desktop.
compile-time list
This file contains 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 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) |
This file contains 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
{: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