Replacements acting on Associations confuse me. I have read Pattern matching to a function evaluation inside an Association and How to use ReplaceAll and RuleDelayed with an Association? and I understand how to use RuleCondition (or something equivalent) to insert an evaluated expression into the RHS of the held rule.
But this is not the only difficulty with replacement rules acting on Associations. Compare the following
replaceAllIncludingAssociations[expr_,
replacementRule_] := (expr /. assoc_Association :> assocHolder[Normal[assoc]]) /. replacementRule /. assocHolder -> Association (Here a RuleCondition on the RHS would not have helped.)
Applying a function by Key pattern, preserving Association order could probably be used to find a solution but it would require some adaption and learning a new syntax. I would prefer something that is close to the expected syntax of ReplaceAll
.
The following seems to work as I would expect ReplaceAll
to work. Of course performance wise it is probably terrible:
replaceAllIncludingAssociations[expr_,replacementRule_] := (expr //. assoc_Association :> assocHolder[Normal[assoc]]) /. replacementRule /. assocHolder -> Association