# Condition block scoping

I have a list of functional rules of the type F[x_]:=y;cond[x] with always the same condition. The list is long and rule 1 of programming says: "Do not copy!" I surely can somehow "extract" my condition?!

fromherecondmusthold statement
F[x_]:=y; (*if cond[x]*)
G[x_]:=z; (*if cond[x]*)
fromherecondmustnolongerhold statement


As you see, a slight snag would be that F[u_]:=v would not match the pattern, although an underscored variable is a bound one that could have any other name.

As an example we choose a condition x>0:

ClearAll[f1, f2];
With[{y = x_ /; x > 0},
f1[y] = x;
f2[y] = 2 x;
]


Now to test:

f1[-1]
(*f1[-1]*)
f1[1]
(*1*)
f2[-1]
(*f2[-1]*)
f2[1]
(*2*)

• I desperately tried to include 2 variables. Say, f computes y-x but only if y>x. I tried out about any syntax...except the correct :-/ Can you help again? Nov 2, 2021 at 10:45
• Try: Clear[f1, cond]; cond[x_, y_] := (x > y); f1[x_, y_] := x + y /; cond[x, y]; Nov 2, 2021 at 11:24
• That works for one function, but not for many. I tried the following: With[{assigns}/;cond,f1=...;f2=...;] is wrong syntax, With wants a list. Even a nested With doesn't scope where it should: With[{v = b_}, With[{u = a_ /; u > v}, g[u, v] := a/b]];. It also doesn't matter if u>v is a>b or any mix - the outer assign doesn't make it into the inner bracket. Nov 3, 2021 at 9:35
• No, the idea was: Clear[f1, cond]; cond[x_, y_] := (x > y); f1[x_, y_] := x + y /; cond[x, y];f2[x_, y_] := x +2 y /; cond[x, y];... repeating cond[...] Nov 3, 2021 at 9:55