Created
August 2, 2017 13:04
-
-
Save igas/d612ceece6d2acf0b196a3af72646550 to your computer and use it in GitHub Desktop.
composition macro elixir.sydney
This file contains 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 Composition do | |
defmacro compose(name, list) do | |
arg = Enum.reduce(list, Macro.var(:x, __MODULE__), fn({mod, fun}, acc) -> | |
{{:., [], [{:__aliases__, [alias: false], [mod]}, fun]}, [], [acc]} | |
end) | |
quote do | |
def unquote(name)(x) do | |
unquote(arg) | |
end | |
end | |
end | |
end | |
defmodule Main do | |
require Composition | |
Composition.compose(:a, [{Enum, :sort}, {Enum, :uniq}, {Enum, :reverse}]) | |
def hello do | |
a([1,5,3,4,1,2,3]) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment