Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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)
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.
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.