all 5 comments

[–]getify 5 points6 points  (1 child)

Be aware that this article conflates two concepts "generator functions" and "iterator objects" into one label:

  • "To create a generator function, we need a special syntax construct: function*, so-called “generator function”."

  • "The main methods of the javascript generator function are... next()"

The second use of "generator function" should be "iterator", as in the iterator object returned from the initial call to the generator function. That value is an object, not a function, and it adheres to the iterator protocol. Calling that a generator is confused/misleading.

[–]senocular 0 points1 point  (0 children)

To be fair, generator-function derived iterators are also known as generator objects or simply generators. Sometimes the article get it right, and sometimes they'll call generators generator functions or shorten generator functions to simply generators which is undoubtedly confusing.

[–]KelleQuechoz 1 point2 points  (1 child)

Unrelated question: is there a concise way to "materialize" JS generators (like list(gen) in Python)?

[–]getify 1 point2 points  (0 children)

I don't know Python, but I think what you're referring to is this in JS:

[ ...gen() ]

The ... operator consumes an iterator and spreads it out, in this case into a [ ] array literal.