3
$\begingroup$

Trying to Monitor a process using the postfix notation, as in

Table[Pause[n/10], {n, 5}] // Monitor[#, n] &

fails: the argument Table[Pause[n/10], {n, 5}] is evaluated before being passed to the pure function Monitor[#,n]& because that function does not have the attribute HoldAll like Monitor itself.

An option is to Hold explicitly and release the hold inside the pure function:

Table[Pause[n/10], {n, 5}] // Hold // Monitor[ReleaseHold[#], n] &

Is there a simpler/cleaner/shorter way? Obviously Monitor[Table[...],n] works, but I'd rather separate the code from the monitoring.

$\endgroup$
  • 1
    $\begingroup$ I tend to use an infix version Table[...] ~Monitor~ n. $\endgroup$ – wxffles Apr 18 '16 at 21:47
  • $\begingroup$ Ah, I was wrong to insist on postfix. The infix notation is great, I'll use that. $\endgroup$ – Bruno Le Floch Apr 19 '16 at 20:37
2
$\begingroup$

You can use Unevaluated

Unevaluated@Table[Pause[n/10], {n, 5}] // Monitor[#, n] &

The neater way will be to use the infix version as suggested by wxffles in the comments, but your question specifically asked for postfix so... For completeness, here it is

Table[Pause[n/10], {n, 5}] ~Monitor~ n
$\endgroup$
2
$\begingroup$

Wrap your Monitor call in a Function object endowed with a holding attribute:

Table[Pause[n/10], {n, 10}] // Function[{input}, Monitor[input, n], HoldAll]

This will work as though you had wrapped Monitor around your Table.

enter image description here

$\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.