# Progress Indicator for NestList

I have a time consuming function that is going to be iterated in a Nest or NestList and I would like to know if there is a good way to monitor the progress. I have found a partial work-around, but it requires an extra global variable (n).

fun[x_] := Module[{}, n++; Pause[1]];
ProgressIndicator[Dynamic[n/5]]
n = 0; NestList[fun, Null, 5]


Besides being poor coding practice, this is a problem because when I call the Nest from different places in the larger code (for example, make two copies of the above and execute both), all the progress indicators move synchronously, rather than being limited to the NestList that is actually executing.

• – Kuba Nov 25 '16 at 8:10

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.