# Write repeated expressions within a rule more simply

Is it possible to shorten the length of a (delayed) rule (i.e. lhs :> rhs) when there are expressions which repeat several times? Those expressions involve values of the patterns matched in lhs, so ideas that come to my mind, such as creating a pure function that takes the repeated elements as arguments, do not work.

A simple example of what I refer to can be:

Replace[ list , a_ :> {IntegerDigits[a],Length@IntegerDigits[a],IntegerDigits[a][[1]]} ]


Is there a way to write in such case IntegerDigits[a] only once instead of three in there?

• With[{rd = RealDigits[a]}, {a, Length@a, First@a}] Commented Jun 2, 2020 at 17:28
• Through[{Identity, Length, First}[RealDigits[a]]] Commented Jun 2, 2020 at 17:43
• Are you sure that's what you want though? Length@RealDigits[anything] will always be $2$, because RealDigits always returns a list of two elements. Was that supposed to be Length@First@RealDigits[a] instead? Commented Jun 2, 2020 at 18:42
• I've edited it and changed the function for IntegerDigits. The function doesn't directly relate to my problem, it was just a simple example to clarify a bit what I meant.
– abcd
Commented Jun 2, 2020 at 19:33

Replace[list, a_ :> {b = IntegerDigits[a], Length@b, b[[1]]}]