Hacker News new | comments | show | ask | jobs | submit login

I'm not so sure. Generally implementing an iterator when the iteration doesn't represent a straightforward walk over the elements of a collection is much more error-prone. The implementation of the lambda version looks exactly the same as if you were just performing the iteration locally, while the iterator version requires saving and restoring a bunch of state and handling more edge cases. At call sites, the iterator version looks a little better, but not so much so that I'd consider the additional implementation complexity (and hence potential for bugs) worth it. That said, this is definitely a controversial opinion and there are arguments in the other direction as well.

Writing iterators is non trivial, but, lacking first class continuations, external iterators (a-la begin, end) are more powerful than internal iterators (a-la for_each), as they allow iterating multiple ranges at the same time and easily defining subranges.

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact