Created
April 4, 2017 00:32
-
-
Save thiagoa/081fc6b14ef96bc6ff80811bdbd43118 to your computer and use it in GitHub Desktop.
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
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