Skip to content

Instantly share code, notes, and snippets.

@thiagoa
Created April 4, 2017 00:32
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 thiagoa/081fc6b14ef96bc6ff80811bdbd43118 to your computer and use it in GitHub Desktop.
Save thiagoa/081fc6b14ef96bc6ff80811bdbd43118 to your computer and use it in GitHub Desktop.
defmodule TermEvaluator do
def call(term, pattern: pattern) do
term = Macro.escape(term)
ast = quote do
case unquote(term) do
unquote(pattern) -> true
_ -> false
end
end
Code.eval_quoted(ast) |> elem(0)
end
end
pattern = quote(do: %{signal_strength: strength} when strength < 5)
term = %{signal_strength: 6}
IO.puts TermEvaluator.call(term, pattern: pattern)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment