5
$\begingroup$

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?

$\endgroup$
7
$\begingroup$

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

$\endgroup$
5
$\begingroup$
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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.