Skip to content

Instantly share code, notes, and snippets.

@cjbottaro
Created January 8, 2016 23:41
Show Gist options
  • Save cjbottaro/bde9471b122a9d9d4618 to your computer and use it in GitHub Desktop.
Save cjbottaro/bde9471b122a9d9d4618 to your computer and use it in GitHub Desktop.
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