match?/2
turns a match result into a predicate
TIL about Elixir’s Kernel.match?/2
.
iex> list = [a: 1, b: 2, a: 3]
iex> Enum.filter(list, &match?({:a, _}, &1))
[a: 1, a: 3]
So many times I’ve hacked around not knowing about this. 🤯
Where this gets really useful is in tests where you want to assert part of the shape of a map. For instance:
assert Enum.count(
results,
&match?(%{id: ^my_id, inserted_at: ^time}, &1)
) == 1