Suppose I have the function
heavyFunction[x_]:=Module[{n},Do[Pause[0.1],{n,100}]]
representing some complicated and time-demanding computation. I want in some circumstances to Monitor
the progress of this function. Other times I don't, for example because heavyFunction
is used by other computationally-heavy functions and I'd rather monitor the progress of those instead.
I'm also not looking for a solution like the simple
Monitor[heavyFunction[whatever],n]
because the quantity I'd use to monitor the progress can be not as simple as the loop index, and because the computation of such a quantity may depend on local variables defined in a Module
inside the function.
The naive solution I'd have liked to use is something like
Options[heavyFunction]={monitor->True};
heavyFunction[x_,OptionsPattern[]]:=Module[{n},
If[OptionValue@monitor,Monitor[#,n],Identity]&@
Do[Pause[0.1],{n,10}]
]
but it doesn't show the progress.
How can this be achieved?