Skip to content

Instantly share code, notes, and snippets.

@anthonylebrun
Last active May 30, 2016 23:45
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 anthonylebrun/271a94c04d2144f12d88b5f90e09157c to your computer and use it in GitHub Desktop.
Save anthonylebrun/271a94c04d2144f12d88b5f90e09157c to your computer and use it in GitHub Desktop.
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