Created
January 9, 2018 09:08
-
-
Save h4cc/4381325743b6972e52026b02912baeb9 to your computer and use it in GitHub Desktop.
Inline lookup in Elixir. https://twitter.com/h4cc/status/950293145618706437
This file contains hidden or 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 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