In this toy example,
lis = {C[1]*3, C[2]*9, 123};
I want to change, using Cases, the pattern C[x__]*y_
to C[x]*99*y
and the pattten y_Integer
to 5*y
Currently I do it using two separate calls and then use Join on the result. Like this
ClearAll["Global`*"]
lis = {C[1]*3, C[2]*9, 123};
c1 = Cases[lis, C[x__]*y_ :> C[x]*99*y]
c2 = Cases[lis, y_Integer :> 5*y]
Join[c1, c2]
Is it possible to do it using just one call to Cases?
Cases[lis, ??? ]
Not able to find what the syntax could be (if it is even possible).
Alternative
does not do what I want (at least I could not make it work).
Ofcourse this should extend to more than just two, it should work with 3 different patterns and 4 and as many as one wants.
Update
The above toy example may be was too toy. Here is another example
ClearAll["Global`*"]
lis = {Sin[x], Cos[x], 99, x};
c1 = Cases[lis, Sin[x_] :> 3 + Sin[2*x]]
c2 = Cases[lis, Cos[x_] :> Exp[x]*Tan[x]]
c3 = Cases[lis, x_Integer :> Integrate[x, z]]
Join[c1, c2, c3]
The idea to keep the same patterns used for each case, but combine them into one call to Cases. I do not want to change the logic or anything like that. In a new universe, I'd like to do for the above this
Cases[lis, Sin[x_] :> 3 + Sin[2*x] |
Cos[x_] :> Exp[x]*Tan[x]] |
x_Integer :> Integrate[x, z]
]
But the above ofcourse does not work.
Cases[lis, a_. * y_Integer :> a*99*y, 1]
$\endgroup$99
is not meant to be the same. It can be anything for each pattern. I will correct my example to make it general. The patterns have nothing in common in practice. $\endgroup$Join[Cases[lis, #][[1]] & /@ {Sin[x_] :> 3 + Sin[2*x], Cos[x_] :> Exp[x]*Tan[x], x_Integer :> Integrate[x, z]}]
$\endgroup$cases = {c1, c2, c3};
andNestList[Cases[lis, #] &, cases, Length@cases] // Flatten
? $\endgroup$Replace
with your list of patterns + a falling pattern to remove them at the end like_ -> Nothing
, Example 1:Replace[lis, {C[x__]*y_ :> C[x]*99*y, y_Integer :> 5*y, _ -> Nothing}, {1}]
, Example 2:Replace[lis, {Sin[x_] :> 3 + Sin[2*x], Cos[x_] :> Exp[x]*Tan[x], x_Integer :> Integrate[x, z], _ -> Nothing}, {1}]
$\endgroup$