Last active
April 18, 2017 02:51
-
-
Save tiagopog/57380d9b5e264c98d7e384659e684ac7 to your computer and use it in GitHub Desktop.
Elixir - Quoted expressions
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
# 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