Guest User

Untitled

a guest
Jan 16th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. inactSymbols = <||>;
  2.  
  3. Attributes[myInactiveQ] = {HoldFirst};
  4. myInactiveQ[_] := False
  5.  
  6. Attributes[myInactive] = {HoldFirst};
  7. myInactive[h_] := myInactive[h] = With[
  8. {s = Unique["inact" <> SymbolName@Unevaluated@h]},
  9. Attributes[s] = Complement[Attributes[h], {Protected, Locked}];
  10. MakeBoxes[s[args___], frm_] ^:= With[
  11. {b = MakeBoxes[Inactive[h][args], frm]},
  12. InterpretationBox[b, s[args]]
  13. ];
  14. AppendTo[inactSymbols, s -> h];
  15. MakeBoxes[s, frm_] ^:= MakeBoxes[myInactive[h], frm];
  16. myInactiveQ[s] ^= True;
  17. s
  18. ]
  19. myInactive[s_?myInactiveQ] := s
  20. myInactive[s : List | Rule] := s
  21.  
  22. Attributes[myInactivate] = {HoldFirst};
  23. myInactivate[expr_] := Replace[
  24. Unevaluated@expr,
  25. {
  26. i : HoldPattern[myInactive[h_][args___]] :> i,
  27. h_Symbol[args___] :> With[
  28. {in = myInactive[h]},
  29. in[args] /; True
  30. ]
  31. },
  32. All
  33. ]
  34.  
  35. myActivate[expr_] := expr /. inactSymbols
  36.  
  37. a = 1;
  38. myInactivate[a = 2]
  39. (* myInactive[Set][a, 2] *)
  40.  
  41. myActivate[%]
  42. (* 2 *)
  43.  
  44. myInactivate[Sum[f[a = 2, z], {n, 1, 2}]]
  45.  
  46. myInactivate[Sum[f[a = 2, z], {n, 1, 2}]] //
  47. Cases[#, _?myInactiveQ, All, Heads -> True] &
  48. (* {myInactive[Sum], myInactive[f], myInactive[Set]} *)
  49.  
  50. myInactive[Set][a, 2]
Add Comment
Please, Sign In to add comment