What technical advantages do generators offer that array methods do not? What experiences have others had using generators? Are they more a neat trick but not practical in production code? Or are they a miss-understood game changer?
Most documentation, scuttlebutt, and blogs only offer technical how to and lack any information on the why to use, when to use, and when not to use. How can I help articulate the good reasons for generators when there is very little blog posts and documentation about them?
Thanks in advance.
There are LOTS of advantages to generators, but it all depends on context. What are the generators doing? If they’re nothing more than lazy arrays, their only benefit is, well, their laziness - it’s a tradeoff in performance and space compared to eagerly computing the full array. But there are a few other ways they can be used. For example, you can have an infinite-length generator, capable of being iterated over until you find what you need, without worrying about pre-planning your input size. The behaviour of subsequent “array elements” can be changed by signalling the generator (since you can send a value into the generator as well as getting one out of it).
Generators (or similar concepts) exist in a number of languages. According to Generator (computer programming) - Wikipedia they have been around since 1975, although among languages that are still in use today, Python would be the oldest. They come by various names; personally, I’m tickled by Pike’s choice to call them “continue functions”, since they’re functions that… return… but still continue They’re incredibly useful in a small set of situations, and somewhat useful in a much broader set, so my personal recommendation is: Go ahead and keep using them. Haters gonna hate.
I wrote the PEP that introduced genrators to Python, so I may be too close to it to be objective.
I cut my teeth on generators in the Icon language, in which every expression “is a generator”. For example, the literal “2” is a generator with a result sequence of length 1, the single integer 2. The expression “x < y” is a generator that yields y if x is less than y, or fails (yields no result) if x >= y. So Python’s chained comparisons, like “x < y < z”, seem to work the same way in Icon, but for very different reasons.
Icon is typically used for non-numeric computation, and especially for combinatorial search algorithms (Icon was inspired by trying to expose the innards of SNOBOL4’s string pattern-matching machinery for general algorithmic use). Simple generators are just plain the bee’s knees for that, for reasons partly explained in the cited PEP.
I still use generators every day in Python. They’re a delight! Curiously. I never had much use for the fancier “async” extensions, and seemingly nobody had much use for Icon’s fancier full-blown coroutine extensions either (even among Icon’s developers, coroutines were viewed as an academic research project).
As to the JS community’s opinion, Matthew 7:6 comes to mind:
Give not that which is holy unto the dogs, neither cast ye your pearls before swine, lest they trample them under their feet, and turn again and rend you.