Let's say I have four functions: x, y, X`x, and X`y. Writing a pattern that matches x, but not X`x is easy
f[_x] := True; f[_] := False
{ f[x[]], f[X`x[]] }
(* {True, False} *)
and the reverse is also straightforward
g[_X`x] := True; g[_] := False
{ g[x[]], g[X`x[]] }
(* {False, True} *)
But, how would I match all members of X`?
Obviously, this works
h[_?(MemberQ[Names["X`*"], ToString@Head@#] &)] := True
h[_] := False
{ h[x[]], h[X`x[]], h[X`y[]] }
(* {False, True, True} *)
but it seems excessive, especially if the Context contains a large number of functions.
_?(Context @@ # == "X`" &)? – Mr.Wizard Jan 21 '13 at 15:41Contextexpects a symbol or string, soContext[X`x[]]generates an error. – rcollyer Jan 21 '13 at 15:44a_Symbol[___] /; Context[a] === "X`"may be better. Would you please try to explain in more detail what you're doing? – Mr.Wizard Jan 21 '13 at 15:50Headthat is a member of a specific context. To have a head, it must be a function, such asList. That is why my examples are littered withx[], notx. In the case of_?(Context @@ # == "X`" &),x[]is passed toPatternTest, notxasContextexpects. So, I have to strip off everything but theHead. – rcollyer Jan 21 '13 at 16:07