5

Suppose I have a code block

EchoOff[CompoundExpression[
    ...,
    Echo[...,"echo 1",f],
    Echo[...,"echo 2",g],
    ...
]]

Is there a function EchoOff that can be wrapped around an expression to turn off the output of all Echo encountered?

Moreover is there something like turning selective echos off? For example,

EchoOff[expr,{1,4}] turns off the first and fourth Echo encountered in the expression.

Or simply if the Echo can be marked as 1,2,... and then EchoOff can target them with their mark?

user13892
  • 9,375
  • 1
  • 13
  • 41

2 Answers2

7

You can use GeneralUtilities`DisableEcho:

expr1; Echo[x = 1 + 1]; 
 GeneralUtilities`DisableEcho[ Echo[y = 1 + 2] ; Echo[z = 1 + 3]];Echo[w = 1 + 4]; expr2

enter image description here

z + y

7

kglr
  • 394,356
  • 18
  • 477
  • 896
5
ClearAll[EchoOff];
SetAttributes[EchoOff, HoldFirst];
EchoOff[code_] := Block[{Echo},
   Echo[c_, bla___] := c;
   code
   ];

Then:

EchoOff[
 CompoundExpression[1, Echo[1, "echo 1", f], Echo[2, "echo 2", g], 2]
 ]

2

You can do that selectively by label with

EchoOff[code_, blacklist_] := Unevaluated[code] /. Table[
    With[{label = b},
     HoldPattern[Echo[c_, label, bla___]] :> Unevaluated[c]
     ],
    {b, blacklist}
    ];

EchoOff[
 CompoundExpression[1, Echo[1, "echo 1", f], Echo[2, "echo 2", g], 
  2],
 {"echo 1"}
 ]

echo 2 g[2]

2

Henrik Schumacher
  • 106,770
  • 7
  • 179
  • 309