While
? also works works
Needs@"GeneralUtilities`"
Module[{hold},
SetAttributes[hold, HoldAll];
oneTimeRules[rules_] :=
Normal@Merge[rules, ListIterator] /. Rule -> RuleDelayed /.
i_GeneralUtilities`Iterator :>
With[{r = Read[i]}, hold[r, r =!= IteratorExhausted]] /.
hold -> Condition;
];
and then
Replace[list, oneTimeRules@rules, Length@rules]
- Another fancy approach based on
Michael E2's answer
. This serves as a clarifying comment. Grab the relevant code that is needed
SetAttributes[useRepeated, Listable];
useRepeated[(Rule | RuleDelayed)[pat_, repl_], n_ : 1] :=
Module[{used = 0},
pat :> repl /; used++ < n
];
useOnce[r_] := useRepeated[r];
and then use
Replace[list, useOnce@rules, Length@rules]
or equivalently
ReplaceAll[list, useOnce@rules]
Note: if one uses Replace
and the default value of the command -which is set to 1
- the above does not work. So some minor caution is needed.