An "anonymous" function should have no name (symbol) attached. What result should you want from g[2.4] if the result should not contain the symbol g? I picked up my programming on the street, so take with a grain of salt: My claim is that is should not be g[2.4]; rather, if g = func[...], then it should be func[...][2.4], where func is a version of Function that conditionally evaluates its input. See an example below.
A so-called function like g[x_] := Sin[x] /; x < 2
defines a conditional pattern replacement in an expression with head g.
The pattern language is rather complex, even if we stick to down values.
Exactly what sort of pattern replacements do you want to turn into anonymous functions? Well, in the example below, I will keep it simple and implement a single conditional test equivalent to g[x_,...] := body /; test like in the OP. I'm not sure it's worth being able to turn g[Sin[Optional[v_?NumericQ]*u_]] := u + v /; v > 0 into an anonymous function.
Function[{x,...}, body][x1,...] makes a literal, unconditional replacement in the expression body, in which each literal occurrence of x is replaced by x1 and so on; this is then evaluated after Function returns. Viewed as a pattern replacement, it's very limited. It seems ill-suited to adapting to complicated patterns.
Examples.
First approach.
It's probably better to remove attr from the definition. See below.
ClearAll[fFunction];
SetAttributes[fFunction, HoldAll];
fFunction[v_, body_, attr_, test_][a___] :=
Function[v, body, attr][a] /; Function[v, test, attr][a];
Test case:
g = fFunction[u, Sin[u], {}, u < 2]
(* fFunction[u, Sin[u], {}, u < 2] *)
{g[0.3], g[2.4], g[x]}
% /. x -> 1.2
(*
{0.29552,
fFunction[u, Sin[u], {}, u < 2][2.4],
fFunction[u, Sin[u], {}, u < 2][x]}
{0.29552,
fFunction[u, Sin[u], {}, u < 2][2.4],
0.932039}
*)
Hey, do attributes really work?
HoldAll.
No, attributes don't really work.
Here's a way to fix it for HoldAll and similar.
It destroys somewhat the desired behavior when test is not True.
Since attributes are not applied to subvalues, fFunction[x, body, HoldAll, x > 10][a] won't hold a. We have to take advantage of the fact that heads are evaluated first to grab Hold of a before it evaluates.
ClearAll[fFunction];
SetAttributes[fFunction, HoldAll];
fFunction[v_, body_, attr_, test_] :=
Function[Null,
fFunction[v, body, attr, test, Hold][Hold[##]],
HoldAll];
fFunction[v_, body_, attr_, test_, Hold][Hold[a___]] :=
Function[v, body, attr][a] /; Function[v, test, attr][a];
Now what about Listable? Well, that's tricky because we have to thread the evaluation of test and use it to block or allow the evaluation of body -- that is, fFunction should be listable, in the case where Listable is in attr....Hmm, I'm reminded of something someone one wrote:
...once you run into a limitation like this, it is best to reconsider the design and find some other approach to the problem... -- Leonid Shifrin, How do you set attributes on SubValues?
Remarkably, there's an obvious alternative. Use the standard way of defining functions with patterns. :)