I'm trying to write a function whose first argument must be another function, defined either as a pure function or a function-style replacement rule. I don't know how to specify a condition on the argument to achieve that. I know I can use g[func_Function,…]
to require pure functions, but functions defined via replacement rules don't have a useful Head
.
Here's an example:
f[x_] := x^2 - 1;
g[func_?MatchQ[#, _[___]] &, a_, b_] := func[a + b];
g[f, 1, 2]
(* g[f, 1, 2] *)
Checking the Head
of f
doesn't seem helpful:
Head[f] (* ==> Symbol *)
Head[f[x]] (* ==> Plus *)
How can I constrain the arguments to g
such that it will accept either a pure function or a function defined with :=
?
System'Private'MightEvaluateWhenAppliedQ
in the page you linked to, but I could't get it to do anything. Every time I tried using it, I just got my expression back unevaluated. :( $\endgroup$System`Private`MightEvaluateWhenAppliedQ[f]
, thenf[x_] := x^2
, thenSystem`Private`MightEvaluateWhenAppliedQ[f]
again. $\endgroup$