Last active
May 30, 2016 23:45
-
-
Save anthonylebrun/271a94c04d2144f12d88b5f90e09157c to your computer and use it in GitHub Desktop.
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 MapToFunction do | |
defmacro functionalize(map) do | |
map |> Enum.map(fn {key, val} -> | |
quote do: def unquote(key)(), do: unquote(val) | |
end) | |
end | |
end | |
defmodule Digits do | |
import MapToFunction | |
functionalize [ | |
one: 1, two: 2, three: 3, | |
four: 4, five: 5, six: 6, | |
seven: 7, eight: 8, nine: 9 | |
] | |
end | |
Digits.five # => 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment