TransWikia.com

How to change behavior of Echo or turn it off without removing it from code?

Mathematica Asked on January 28, 2021

I wish to use Echo for output of intermediate results if an OptionValue I call verbose->True and neutralize it if verbose->False. I think this is useful when you need to know if some resources (packages. etc) were successfully loaded. I am thinking that it might work one of two ways…

  1. Spot lines of code with Echo but if verbose->False turn off Echo inside function then turn Echo back on when leaving function.
  2. Spot lines of code with a generic fn variable. When verbose->True activate it with fn=Echo but in its off state it should do something completely inert fn=do-something-that-does-not-effect-expression.

Please help me figure this out. Here is where I would first use it. Notice the If statements that really do nothing more than repeat an Echo wrapped expression. I wish to eliminate the If statements.

$resources=windows-path-to-alternate-directory-for-packages
packs={"package1`","package2`",...}
Options[loadPacks] = {path -> $resources, verbose -> True};
loadPacks[packs_, OptionsPattern[]] := Block[{pathslist},
   addpath[] := Once[PrependTo[$Path, OptionValue[path]]];
   If[OptionValue[verbose] === True, Echo[addpath[]], addpath[]];
   Map[Needs[#] &, packs];
   If[OptionValue[verbose] === True, 
    Echo[Column[contextSymbols[packs]]], Return[]];
   ];
contextSymbols[names__] := 
  Block[{list}, list = If[ListQ[names], names, {names}]; 
   Map[Names[# <> "*"] &, list]];

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP