aaron-price
For ets/mnesia guards, is there an :in equivelant?
I am just starting to dabble with :mnesia.select/1 and for the most part it works fine, but how can I get the equivalent of Kernel.in/2 ?
For example this guard works and returns the correct results
[ {:==, :"$4", 10} ]
But this aborts with a :badarg message, because erlang has no in (or maybe it does? I don’t know erlang)
[ {:in, :"$4", [10] } ],
So how should I write such a guard clause?
In the erlang docs, I see that erlang DOES have a :lists.member function, but I cannot understand how to use that here. [ {:"lists.member", :"$4", [10]} ] does not appear to work, I’m not sure what syntax it is even expecting there.
According to the match spec docs, there is a set of built in functions, like ==, or map_get… But I see no way of asserting membership inside a list.
Marked As Solved
josevalim
You have to compile them to a comparison with every element of the list using =:= and then or all comparisons together, which is precisely what the in macro does anyway. ![]()
Also Liked
aaron-price
Thanks Jose! That works.
For posterity, my implementation:
def guard_in(value, li) do
clauses = Enum.map(li, &({:"=:=", value, &1}))
List.to_tuple([:or | clauses])
end
# ...
:mnesia.transaction(fn ->
:mnesia.select(table, [{
{table, :_, :"$1"},
[guard_in(:"$1", [10, 40])],
[:"$$"]
}])
end)








