Skip to content

Instantly share code, notes, and snippets.

@h4cc
Created January 9, 2018 09:08
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 h4cc/4381325743b6972e52026b02912baeb9 to your computer and use it in GitHub Desktop.
Save h4cc/4381325743b6972e52026b02912baeb9 to your computer and use it in GitHub Desktop.
defmodule InlineLookup do
# Examples how to handle a static lookup table in Elixir.
def example1(x) do
%{1 => "a", 2 => "b", 3 => "c"}[x]
# Could also be a attribute
# Should be the same as:
Map.get(%{1 => "a", 2 => "b", 3 => "c"}, x, nil)
end
def example2(1), do: "a"
def example2(2), do: "b"
def example2(3), do: "c"
def example2(_), do: nil
# Functions calls, could be generated at compile time from attribute
# Not using a map here, so could be optimied by compiler maybe.
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment