# Fast way to check if an expression matches any of the rules in a list?

I have a function defined roughly like so. This is a minimum recreation.

foo[expr_, rules: {__Rule}] := "some fancy result" /; AnyRuleMatchesQ[expr, rules];


What can I use in place of AnyRuleMatchesQ (which doesn't exist) to quickly check if any of the rules has a pattern matching expr?

I don't want to do anything that actually applies the rule as part of the test, because that seems inefficient. I could use Part to pull out the patterns from the rules, and then map over them to check them, but that feels clunky and slow.

This is a check that Mathematica is presumably doing internally all over the matching engine, so I expected to find this easily exposed as a function somewhere. But alas, I can't see anything that does this.

• I would use patterns = rules[[All, 1]]; MatchQ[expr, Alternatives @@ patterns] – C. E. Dec 30 '18 at 1:10