Skip to main content
edited body
Source Link
Edmund
  • 43.2k
  • 3
  • 53
  • 148

You may Condition the function pattern such that it matches for a parameter of Head Function or on the DownValues of the symbol when they have the pattern of that matches a basic function define by SetDelayed.

As pointed out in the OP comments there are many forms that a function can take and the solution below will not cover them all. However, it may be enough to cover the cases within your particular project.

With

ClearAll[g]
g[foo_g[func_, a_, b_] /; 
  MatchQ[func_MatchQ[func, _Function] \[Or] 
   Length@Cases[DownValues[func][[All, 1]], \[FormalP] : HoldPattern[_[_Pattern .., ___]] :> 
       Hold@\[FormalP], 2] > 0 :=
 func[a + b]

and

f[x_] := x^2 - 1

Then

g[f, 1, 2]
8
g[# + 1 &, 1, 2]
4
g[y, 1, 2]
g[y, 1, 2]

Hope this helps.

You may Condition the function pattern such that it matches for a parameter of Head Function or on the DownValues of the symbol when they have the pattern of that matches a basic function define by SetDelayed.

As pointed out in the OP comments there are many forms that a function can take and the solution below will not cover them all. However, it may be enough to cover the cases within your particular project.

With

ClearAll[g]
g[foo_, a_, b_] /; 
  MatchQ[func_, _Function] \[Or] 
   Length@Cases[DownValues[func][[All, 1]], \[FormalP] : HoldPattern[_[_Pattern .., ___]] :> 
       Hold@\[FormalP], 2] > 0 :=
 func[a + b]

and

f[x_] := x^2 - 1

Then

g[f, 1, 2]
8
g[# + 1 &, 1, 2]
4
g[y, 1, 2]
g[y, 1, 2]

Hope this helps.

You may Condition the function pattern such that it matches for a parameter of Head Function or on the DownValues of the symbol when they have the pattern of that matches a basic function define by SetDelayed.

As pointed out in the OP comments there are many forms that a function can take and the solution below will not cover them all. However, it may be enough to cover the cases within your particular project.

With

ClearAll[g]
g[func_, a_, b_] /; 
  MatchQ[func, _Function] \[Or] 
   Length@Cases[DownValues[func][[All, 1]], \[FormalP] : HoldPattern[_[_Pattern .., ___]] :> 
       Hold@\[FormalP], 2] > 0 :=
 func[a + b]

and

f[x_] := x^2 - 1

Then

g[f, 1, 2]
8
g[# + 1 &, 1, 2]
4
g[y, 1, 2]
g[y, 1, 2]

Hope this helps.

Source Link
Edmund
  • 43.2k
  • 3
  • 53
  • 148

You may Condition the function pattern such that it matches for a parameter of Head Function or on the DownValues of the symbol when they have the pattern of that matches a basic function define by SetDelayed.

As pointed out in the OP comments there are many forms that a function can take and the solution below will not cover them all. However, it may be enough to cover the cases within your particular project.

With

ClearAll[g]
g[foo_, a_, b_] /; 
  MatchQ[func_, _Function] \[Or] 
   Length@Cases[DownValues[func][[All, 1]], \[FormalP] : HoldPattern[_[_Pattern .., ___]] :> 
       Hold@\[FormalP], 2] > 0 :=
 func[a + b]

and

f[x_] := x^2 - 1

Then

g[f, 1, 2]
8
g[# + 1 &, 1, 2]
4
g[y, 1, 2]
g[y, 1, 2]

Hope this helps.