Created
January 8, 2016 23:41
-
-
Save cjbottaro/bde9471b122a9d9d4618 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 | |
quote do | |
{ :ok, var!(buffer, MacroFun) } = Agent.start_link fn -> [a: unquote(str)] end | |
unquote(block) | |
res = Agent.get var!(buffer, MacroFun), &(&1) | |
Agent.stop var!(buffer, MacroFun) | |
Enum.reverse res | |
end | |
end | |
defmacro b(str) do | |
quote do | |
Agent.update var!(buffer, MacroFun), &( [ {:b, unquote(str)} | &1 ] ) | |
end | |
end | |
defmacro c(str) do | |
quote do | |
Agent.update var!(buffer, MacroFun), &( [ {:c, unquote(str)} | &1 ] ) | |
end | |
end | |
end | |
defmodule Run do | |
import MacroFun | |
def run do | |
res = a "hello" do | |
b "blah" | |
c "bleh" | |
end | |
IO.inspect res | |
end | |
end | |
Run.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment