Skip to content

Instantly share code, notes, and snippets.

@willhbr
Created June 23, 2016 10:02
Show Gist options
  • Save willhbr/181ff764403245f4892db86797315297 to your computer and use it in GitHub Desktop.
Save willhbr/181ff764403245f4892db86797315297 to your computer and use it in GitHub Desktop.
defmodule RegexCond do
defmacro match(matchable, do: matches) do
IO.inspect matches
de_sigged = matches |> Enum.map(fn
{:"->", context, [[lhs], code]} ->
lhs = de_sig matchable, lhs
{:"->", context, [[lhs], code]}
end)
quote do
cond do
unquote de_sigged
end
end
end
defp de_sig(matchable, regex = {:sigil_r, _, _}) do
quote do
Regex.match?(unquote(regex), unquote(matchable))
end
end
defp de_sig(matchable, {:"->", context, [[lhs], code]}) do
de_sigged = de_sig matchable, lhs
{:"->", context, [[de_sigged], code]}
end
defp de_sig(matchable, {type, context, [lhs, rhs]}) do
lhs = de_sig matchable, lhs
rhs = de_sig matchable, rhs
{type, context, [lhs, rhs]}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment