
Ask HN: Python generator-alikes in other languages? - vram22
Generators in Python are versatile and powerful. What are some roughly equivalent language features in other languages? They could be called by the same name or different names, but have similar or the same functionality. Great if you can add some examples of use of such features.
======
dalke
[https://en.wikipedia.org/wiki/Generator_(computer_programmin...](https://en.wikipedia.org/wiki/Generator_\(computer_programming\))
has examples from 16 different languages.

~~~
vram22
Wow. Didn't know it was such a common construct, and should have googled.

~~~
dalke
The generator PEP,
[https://www.python.org/dev/peps/pep-0255/](https://www.python.org/dev/peps/pep-0255/)
, mentions influence from Sather ("inspired by iterators in CLU") and Icon.

The Sather paper linked-to therein references Marlin, C. D. (1980),
"Coroutines : a programming methodology, a language design, and an
implementation" as the source of the name "generators, semicoroutines or
hierarchical coroutines."

~~~
vram22
Cool, thanks again. Will check those links. I remember coroutines discussed
(and implementation shown in MIX) in Knuth's TAOCP Vol. 1, and now that I
think of it, I may have read about Icon's generators when trying Icon out
quite some years ago, but had forgotten about it. I do remember there were
some rather unusual features in Icon, as compared to common procedural or OO
languages. One was the idea of an operation failing, and being able to test
for that and continue, or something like that. I remember it being fun to try
out; I had downloaded Icon and run the software examples while reading the
Icon tutorial.

Also had seen somewhere that generators and coroutines are related, and at
least one can be implemented in terms of the other. The Wikipedia article you
linked to also says something like that - specifically, that generators can be
implemented in terms of coroutines or continuations. Interesting stuff ...

