You may be remembering a way that I sometimes write function definitions or replacement rules using what I call vanishing patterns, using Alternatives
(short form |
). Some examples:
This technique can considerably condense some code, but at the expense of clarity for those not familiar with it, and often a slight decrease in performance. I do not see any (simple) way to apply it to the example in the question, and shoehorning the method into code where it does not naturally fit (for one accustomed to it) surely hurts clarity and should be avoided outside of "code golf" games.
To provide a simple example of the method where I believe it does fit consider:
f[a_Real, x_] := x*a
f[b_Integer, x_] := x^b
This could instead be written:
f[a_Real | b_Integer, x_] := (x*a)^b
Whichever pattern (a_Real
or b_Integer
) does not match is left out of the right-hand-side, and because of the single-argument behavior of Times
and Power
this is handled correctly.
Another place the method works very well is inserting elements into an expression at specific places based on pattern. For example, suppose you want an function than takes a list and an integer, and appends the integer if it is odd and "prepends" the integer if it is even. This can be written cleanly as:
g[{x___}, e_?EvenQ | o_?OddQ] := {e, x, o}
Test:
Fold[g, {}, Range@10]
{10, 8, 6, 4, 2, 1, 3, 5, 7, 9}
More simply you can use Alternatives
to match different input forms, e.g.:
Condition
? (look it up in the docs) $\endgroup$Alternatives
in the top-level function definitions is not allowed. $\endgroup$f[1, x_] | f[3, x_] := x^2
is not allowed, butf[1 | 3, x_] := x^2
is allowed. (It's not clear to me which the OP is trying to recall, if either.) $\endgroup$