It is possible to define function dependently to passed argument type or condition this argument fulfills (somewhat like overloading):
g[x_?EvenQ] := Print[ToString[x] <> " Is even"]
test1 = (TrueQ[# < 1]) &; test2 := (TrueQ[# >= 1]) &;
g[x_Real ? test1] := Print["function for real x of value < 1"]
g[x_Real? test2] :=Print["function for real x of value >= 1"]
Is it possible to use for this purpose lambda function (anonymous function / pure function) so I don't have to define test1 or test2 and simply use:
g[x_Real ?(TrueQ[# < 1]) &] := Print["function for real x of value < 1"]
g[x_Real ?(TrueQ[# >= 1]) &] :=Print["function for real x of value >= 1"]
instead? In first case g[0.25] returns function for real x of value < 1 in second case it remains unevaluated.
/;(Condition). – Spawn1701D May 21 '13 at 18:43?. Use extra brackets.g[x_Real ?(TrueQ[# < 1] &)]– Rojo May 21 '13 at 18:47g[x_Real ?((TrueQ[# < 1]) &)] := Print["function for real x of value < 1"]– Misery May 21 '13 at 18:49()and you don't needTrueQbecause<will always giveTrue/Falsefor real numerical values and that is guaranteed by your use of_Real. In other words,g[x_Real?(# < 1 &)] := ...is the same. – rm -rf May 21 '13 at 21:01