Expat
Expat is a macro for very simple pattern matching on quoted elixir expressions.
Installation
Available in Hex, the package can be installed as:
- Add
expatto your list of dependencies inmix.exs:
```elixir
def deps do
[{:expat, "~> 0.1.1"}]
end
```
Usage
A typical pattern match on a quoted expression looks like this:
assert {:foo, _ctx, _args} = quote(do: foo)
However as the complex expression, the complex the AST, and sometimes you just want to pattern match on it
iex> import Expat
...> expr = quote do
...> fn a, b, c -> a + b + c end
...> end
...> case expr do
...> expat(fn _, b, _ -> _ end) -> :b_is_second_arg
...> _ -> :dunno
...> end
:b_is_second_arg
The variable pin lets you check on existing variables as in normal patterns. The following example checks that we are adding the number 22
iex> import Expat
...> expr = quote do
...> fn a -> a + 22 end
...> end
...> x = 22
...> case expr do
...> expat(fn _ -> _ + ^x end) -> :good
...> _ -> :dunno
...> end
:good
You can use the double pin operator to assign into a variable, for example, to get the name of the third argument bellow:
iex> import Expat
...> expr = quote do
...> fn a, b, c -> a + b + c end
...> end
...> case expr do
...> expat(fn _, _, ^^x -> _ end) ->
...> with({name, _, _} <- x, do: name)
...> _ -> :dunno
...> end
:c