Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Also known as "external" vs "internal" iterator.

Bottom line: external iteration is composable, internal iteration isn't. Try implementing zip or early stopping using internal iteration (hint: you can't).



Yup this is a great article about it that I coincidentally re-read yesterday:

https://journal.stuffwithstuff.com/2013/01/13/iteration-insi...

I find it easier to think of as "push-pull" though. In Python and Go, you "pull" from iterators. In Ruby, JavaScript, and Rust, you're "pushed" upon. You can do both styles in any language, but there's one way that's favored.

To break the dilemma, you need 2 call stacks, which means threads / coroutines / generators.

(There was a recent blog post about writing an iterator to do the powerset calculation, which is a bit tricky, and made me think of this post)


You can if your language supports first class continuations! (Which ruby has: yield work differently from python I think).

Then you can turn any internal iterator into an external one.




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

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

Search: