Skip to content

Instantly share code, notes, and snippets.

@vanstee
Created June 25, 2013 01:55
Show Gist options
  • Select an option

  • Save vanstee/5855317 to your computer and use it in GitHub Desktop.

Select an option

Save vanstee/5855317 to your computer and use it in GitHub Desktop.
defmodule Test do
def falsify_var(contents) do
IO.puts(inspect(contents))
case contents do
{ :^, _, [_] } ->
contents
{ var, meta, scope } when is_atom(var) and is_atom(scope) ->
{ var, meta, false }
{ left, meta, right } ->
{ falsify_var(left), meta, falsify_var(right) }
{ left, right } ->
{ falsify_var(left), falsify_var(right) }
list when is_list(list) ->
lc i inlist list, do: falsify_var(i)
other ->
other
end
end
defmacro new_match?(left, right) do
left = falsify_var(left)
quote do
case unquote(right) do
unquote(left) ->
true
_ ->
false
end
end
end
def test(_a, b, c) do
finder = new_match?({ ^b, _, d } when length(d) == c, &1)
list = [{ 1, 2, [3] }, { 1, 2, [3] }]
Enum.find(list, finder)
new_match?(e, 1)
Kernel.binding
end
end
IO.puts(inspect(Test.test(1, 2, 3)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment