Progress Indicator for NestList

fun should not know about n:

nestListWithMonitor[f_, init_, n_] := Module[{it = 0},
  PrintTemporary[ProgressIndicator[Dynamic[it/n]]];
  NestList[(it++; f[#]) &, init, n]
]

it is highlighted Red but it doesn't matter if its parent Dynamic is not meant to survive across sessions.

Now you can use it with whatever fun you want.

fun[x_] := (Pause[1]; x + 1);

nestListWithMonitor[fun, 1, 5]

Inspired by this older post by Andrew Moylan, I tried combining Monitor with your ProgressIndicator, then wrapping the whole thing in a DynamicModule to limit the scope the n variable:

Clear[fun]

DynamicModule[{n},
 n = 0;
 fun[x_] := Module[{}, n++; Pause[1]];

 Monitor[
  NestList[fun, Null, 5],
  ProgressIndicator[Dynamic[n/5]]
 ]
]

This may become cumbersome for larger code, though. Alternatively, I was wondering if you would consider assigning an explicit context to the global n counter, with different contexts used for different copies of the Nest code, effectively separating them. Still rather cumbersome though.

Tags:

Dynamic