Skip to content

Instantly share code, notes, and snippets.

@hamiltop
Created January 6, 2016 23:31
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 hamiltop/7d0067d58cc7b579074b to your computer and use it in GitHub Desktop.
Save hamiltop/7d0067d58cc7b579074b to your computer and use it in GitHub Desktop.
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
@hamiltop
Copy link
Author

hamiltop commented Jan 6, 2016

> elixir macro.ex 
[a: "hello", b: "bleh", c: "blah"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment