I have a function, which I want to accept only a list of kind of elements belonging to a group. The task comes down to finding an elegant pattern suitable for easy to read function prototype.
As an example I have a pattern, which works:
pattern = {__?(Head[#] === foo || Head[#] === bar &)}
but I am sure, that it can be in a more elegant way.
Here are some cases were I expect the pattern to match the parameter:
MatchQ[{foo[a1], bar[b1, b2], foo[c1], bar[b1, b2]}, pattern]
MatchQ[{foo[a1]}, pattern]
And here I expect to get false:
MatchQ[{foo[a1], bar[b1, b2], cha[c1, c2]}, pattern]
MatchQ[{}, pattern]
MatchQ[{foo}, pattern]
MatchQ[{1}, pattern]
MatchQ[foo, pattern]
Weakly related to "Pattern matching to head with holdfirst".
x_Head
notation and dopattern = {__?(# /. y_foo | y_bar -> True &)}
. Is there any particular criterion you're interested in? $\endgroup$f[x : {__?(# /. y_foo | y_bar -> True &)}] := test[x]
, Hiding the pattern in some functionf[x : {__?patternTest}]
would not be much better, as then one needs to look uppattern
to see the format. $\endgroup$pattern = {(_foo|_bar)..}
$\endgroup$