Trott-Strzebonski method for In-Place Evaluation
One of the all-time cleverest idioms I've seen; this was discovered by Michael Trott and Adam Strzebonski a few years ago. The template is a rule of the following form:
lhs :> With[{eval = lhs}, eval /; True]
Suppose you want to replace a bunch of things inside a held expression, and have the replacements evaluate. You don't want to evaluate the whole, just the pieces that are targeted.
![[Graphics:../Images/index_gr_407.gif]](../Images/index_gr_407.gif)
![[Graphics:../Images/index_gr_408.gif]](../Images/index_gr_408.gif)
![[Graphics:../Images/index_gr_409.gif]](../Images/index_gr_409.gif)
![[Graphics:../Images/index_gr_410.gif]](../Images/index_gr_410.gif)
![[Graphics:../Images/index_gr_411.gif]](../Images/index_gr_411.gif)
![[Graphics:../Images/index_gr_412.gif]](../Images/index_gr_412.gif)
![[Graphics:../Images/index_gr_413.gif]](../Images/index_gr_413.gif)
![[Graphics:../Images/index_gr_414.gif]](../Images/index_gr_414.gif)
![[Graphics:../Images/index_gr_415.gif]](../Images/index_gr_415.gif)
You can also do this with ReplacePart, but it's harder to code, and has a worse time and space complexity for a large number of substitutions (and using Fold with ReplacePart would be O(n^2)).
|