2
$\begingroup$

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.