Clear questions and runnable code get the best and fastest answer 

PerlMonks 
comment on 
( #3333=superdoc: print w/replies, xml )  Need Help?? 
Quite an interesting technique, and as a disclaimer I really do like it. That being said, this technique will not always get you the fastest iterator. The holy grail of iterators for combinatorial structures is a constant amortized time (CAT) algorithm, meaning that any N successive calls to the iterator takes O(N) time. This is as good as it gets.
Your technique is great as the iterator algorithm mirrors the easytounderstand recursive algorithm. And kudos for noting (and fixing) the doublerecursion. However, your algorithm's running time for iterating through all (N choose K) combinations is proportional to (N+2 choose K+1), so it is not CAT. For a detailed analysis of this and many other interesting iteration algorithms, see section 4.3 of the book Combinatorial Generation by Frank Ruskey. The book does give a CAT iterator for generating combinations, and I believe your general technique of mirroring the recursive algorithm inherently cannot achieve CAT running time (unless you memoize, which defeats the whole purpose of iterating to save memory). I guess it's a matter of what you want to trade  (sometimes small) factors of running time, or readability of the algorithm. Although for iteration problems like this, I usually just refer to Ruskey's book and find a prepackaged CAT algorithm ;) blokhead In reply to Re: Recursivelygenerated Iterators
by blokhead

