Created
January 6, 2016 23:31
-
-
Save hamiltop/7d0067d58cc7b579074b 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 MacroFun do | |
defmacro a(str, do: block) do | |
res = [a: str] | |
{:__block__, _, calls} = block | |
res = Enum.reduce calls, res, fn | |
{f, c, a}, res when f in [:b, :c] -> {f, c, [res|a]} | |
end | |
res | |
end | |
def b(res, str) do | |
res ++ [b: str] | |
end | |
def c(res, str) do | |
res ++ [c: str] | |
end | |
end | |
defmodule Run do | |
import MacroFun | |
def run do | |
result = a("hello") do | |
b("bleh") | |
c("blah") | |
end | |
IO.inspect result | |
end | |
end | |
Run.run |
Author
hamiltop
commented
Jan 6, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment