I'm building a filtering API. Simplifying things, I have something that ressembles a function that takes in a list of structs and a map of filters. My issue is that pairing Enum.filter with &match? is not working when passing in the function parameter:
def filter_structs(structs, filters) do
# this always returns []
structs |> Enum.filter(&match?(^filters, &1))
end
But if instead I hardcode the filters, things work fine:
def filter_structs(structs, _filters) do
structs |> Enum.filter(&match?(%{some_field: true}, &1))
end
I have this workaround working, but it's not very pretty... Is there no better solution?
def filter_structs(structs, filters) do
structs
|> Enum.filter(fn s ->
Map.equal?(filters, s |> Map.take(filters |> Map.keys))
end)
end