Is it possible to achieve pattern test inside a pure function? I have tried
f = Function[{x_?NumericQ},
x
]
but the error Function::flpar occured.
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityYou may do the pattern test inside the function and eventually return immediately without further evaluation. E.g.
f = (If[! NumericQ[#], "Argument not numeric", #^2]) &;
f[a]
Argument nnot numeric
f[2]
4
f = Replace[x_?NumericQ :> x^2]
is the closest thing you get. $\endgroup$If[MatchQ[x, _?NumericQ], ...]
, the only difference is when it doesn't match, you'll get an unevaluated expression normally but withIf
you can control what to return. $\endgroup$