Is there a way to use a match specification to select between different clauses of a function? I’ve seen match specifications used only for either tracing or matching entries in ets tables.
Example of what I want to do:
In user supplied file:
Module(m1),
Function(f1),
Guard([ %% list of match specifications follows:
%% First (and only in this case) match spec:
{ [{score, '$1', '$2', '$3'}, '$4'],
[{is_atom, '$1'}, {is_pid, '$2'}, {is_atom, '$3'}],
[true] }
]).
From this file I want to generate code. What’s important for me is to be able to use the match specifications in the Guard to be able to filter out the clauses of f1 such that I can know when the first argument of f1 was a tuple of the form {score, First, Second, Third} and is_atom(First), is_pid(Second), is_atom(Third).
Is there a way for me to generate code like this:
case some_unknown_function(MatchSpec, F1Args) of
true ->
%% f1's clause matches the MatchSpec
;
false ->
%% f1's clause does not match the MatchSpec
end.
Any help would be much appreciated. Thanks.
Take a look at
ets:match_spec_compile/1andets:match_spec_run/2, they do exactly what you want.