Guest User

Untitled

a guest
Jan 17th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.51 KB | None | 0 0
  1. defmodule InlineLookup do
  2.  
  3. # Examples how to handle a static lookup table in Elixir.
  4.  
  5. def example1(x) do
  6. %{1 => "a", 2 => "b", 3 => "c"}[x]
  7. # Could also be a attribute
  8.  
  9. # Should be the same as:
  10. Map.get(%{1 => "a", 2 => "b", 3 => "c"}, x, nil)
  11. end
  12.  
  13. def example2(1), do: "a"
  14. def example2(2), do: "b"
  15. def example2(3), do: "c"
  16. def example2(_), do: nil
  17. # Functions calls, could be generated at compile time from attribute
  18. # Not using a map here, so could be optimied by compiler maybe.
  19.  
  20. end
Add Comment
Please, Sign In to add comment