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?

  • 2
    $\begingroup$ With[{rd = RealDigits[a]}, {a, Length@a, First@a}] $\endgroup$
    – Michael E2
    Commented Jun 2, 2020 at 17:28
  • 4
    $\begingroup$ Through[{Identity, Length, First}[RealDigits[a]]] $\endgroup$
    – LouisB
    Commented Jun 2, 2020 at 17:43
  • 2
    $\begingroup$ 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? $\endgroup$
    – MarcoB
    Commented Jun 2, 2020 at 18:42
  • $\begingroup$ 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. $\endgroup$
    – abcd
    Commented Jun 2, 2020 at 19:33

1 Answer 1


You can define the thing you want to use repeatedly using a variable (instead of a pure function):

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.