in reply to complex iterator needed

It sounds exactly like you want coroutines or continuations. This might be a good opportunity to play around with Coro, which includes Coro::Cont, an implementation of continuations by way of coroutines.

Other approaches might try recursion or threads.

After Compline,
Zaxo