Group Abstract Group Abstract

Message Boards Message Boards

0
|
3.7K Views
|
3 Replies
|
0 Total Likes
View groups...
Share
Share this post:

How to turn a function off and on

POSTED BY: Henrick Jeanty
3 Replies

This reminds me of that old discussion from three years ago.

POSTED BY: Henrik Schachner
Posted 2 years ago

First off, x_ isn't "any pattern" (it doesn't match multiple arguments, for example). So, the evaluator must evaluate the arguments before it can determine a match. The pattern ___ would be closer to "any pattern", but I don't know if even that would preempt evaluation of the argument.

But I wouldn't recommend this approach at all. I'd recommend using built in functions:

POSTED BY: Eric Rimbey

Here is my attempt. The sample code with embedded Echo is this:

Module[{}, x = Sqrt[2]; If[myEchoQ, Echo[x]]; N[x]]

The flag is called myEchoQ. When myEchoQ is set to False, no echo is generated and no unnecessary evaluation of x is made, because If has the HoldRest attribute:

myEchoQ = False;
Module[{}, x = Sqrt[2]; If[myEchoQ, Echo[x]]; N[x]]

When myEchoQ is set to True, an echo is generated:

myEchoQ = True;
Module[{}, x = Sqrt[2]; If[myEchoQ, Echo[x]]; N[x]]
POSTED BY: Gianluca Gorni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard