# Condition in a replacing function [duplicate]

Because of "Thou shalt not copypaste", I'd like to change my pair of replacements

F[foo_,foo_+1]:=bar;F[foo_,foo_-1]:=bar;


(it's MUCH longer, so it actually saves :-) to something like this:

If[Abs[foo_-fooo_]==1,F[foo_,fooo_]:=bar];


Alas, If doesn't work that way...
Bonus points if the Abs condition holds in a whole larger block because then I can reuse it for more F[] replacement rules. (I tried With, but that didn't work as expected either.)

• Look up Condition. Commented Jun 9, 2018 at 19:40
• As @Mathe172 mentioned, Condition is a quick way out: F[foo_, bar_] /; Abs[foo - bar] == 1 := replacement. I don't understand your bonus question though. Perhaps a less-minimal example is needed? Commented Jun 9, 2018 at 20:19
• I too think that you can solve your problem by carefully reading about Condition and I also don't understand your bonus-question. I'm going to close this as a duplicate to the post Marco mentioned. If you still have problems after reading the documentation, please feel free to edit your question and make clear what you try to achieve and why it doesn't work. Commented Jun 10, 2018 at 1:03
• Ah, THX, Condition didn't show up on the first suggestions list. (Since I can apply it only to one F[] statement at a time, no bonus though :-) Of course I can apply it everywhere I need it, so no harm except to my natural lazyness :-) Commented Jun 10, 2018 at 11:53