Skip to content

Instantly share code, notes, and snippets.

@tiagopog
Last active April 18, 2017 02:51
Show Gist options
  • Save tiagopog/57380d9b5e264c98d7e384659e684ac7 to your computer and use it in GitHub Desktop.
Save tiagopog/57380d9b5e264c98d7e384659e684ac7 to your computer and use it in GitHub Desktop.
Elixir - Quoted expressions
# Quote an expression (turn it into an AST)
foobar = quote do
_ * _ = 42
end
IO.inspect(foobar)
# Define the function to be used when traversing the AST
answers = [2, 21]
put_awnsers = fn
{:_, _meta, _args}, [head|tail] -> IO.inspect(head); {head, tail}
node, acc -> IO.inspect(node); {node, acc}
end
IO.puts "---"
{generated_ast, acc} = Macro.prewalk(foobar, answers, put_awnsers)
IO.puts "---"
IO.inspect(generated_ast)
IO.puts "---"
IO.inspect(acc)
IO.puts "---"
{result, _bindings} = Code.eval_quoted(generated_ast)
IO.inspect(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment