Replacing the Iterator With function*
In this lesson, you port Priya’s valid() generator to JavaScript, then use a generator to replace your
Feed iterator: thirteen lines of counters and result objects become three. In Python, valid() loops
over the readings and yields each one that isn’t None, so list() gets five readings, and the dropped
read is gone. Python makes valid() a generator because a yield sits somewhere in its body.
JavaScript wants a generator marked up front, with a star right after the keyword function. The star
goes on function. Each yield inside stays plain. Watch out: arrow functions have no generator form, so
a generator is always a function* or a generator method.
The body reads like Python’s: a for...of loop over the values, a check that the value isn’t null, and
yield value:
const readings = [12.5, 13.1, null, 14.0, 13.6, 15.2];
function* valid(values) {
for (const value of values) {
if (value !== null) {
yield value;
}
}
}
console.log([...valid(readings)]);
Spread the generator, and the log shows the same five readings. JavaScript numbers don’t split into int
and float, so 14.0 logs as 14:
[ 12.5, 13.1, 14, 13.6, 15.2 ]
Watch out: yield belongs to the function it’s written in. In Python, a yield inside a callback quietly makes that callback a generator;
in JavaScript, it’s a syntax error, and the file doesn’t run at all.
As in Python, calling valid() runs none of its body yet. It hands back a generator object: an iterator that’s iterable
too. Each time spread asks for a value, the body steps on to the next yield, and pauses there.
So one generator object gives you one pass. Store it as temps and spread it twice:
const readings = [12.5, 13.1, null, 14.0, 13.6, 15.2];
function* valid(values) {
for (const value of values) {
if (value !== null) {
yield value;
}
}
}
const temps = valid(readings);
console.log([...temps].length, [...temps].length);
The lengths come back five, then zero, and Python’s list() gets five, then zero, too:
5 0
Same rule as Python: a stored generator stays used up, so call valid() again for a fresh pass.
The Feed class’s Symbol.iterator method still builds its iterator by hand, thirteen lines of counter and
result objects. In Python, __iter__ can be a generator instead: it loops over self.values and yields
each one, and FeedIterator can go.
JavaScript does the same with a generator method: put a star in front of the method’s name, and the hand-written iterator gives way to a three-line loop. The body is the
method’s own, so this is the feed, like self. Calling the method returns a generator, so spread lists all six readings, dropped read included.
Python shortens that loop to yield from. JavaScript’s spelling is yield*, a second star with its own
job: on yield, it means every value of an iterable. The loop folds into one line that hands out every
value of this.values, one at a time:
const readings = [12.5, 13.1, null, 14.0, 13.6, 15.2];
class Feed {
constructor(values) {
this.values = values;
}
*[Symbol.iterator]() {
yield* this.values;
}
}
const feed = new Feed(readings);
console.log([...feed]);
console.log([...feed].length, [...feed].length);
The first log lists all six readings. The second spreads feed twice, and logs six and six:
[ 12.5, 13.1, null, 14, 13.6, 15.2 ]
6 6
Leave the star off, and yield hands out the whole array as one value: spread gets a single item, the
array inside another array, [ [ 12.5, 13.1, null, 14, 13.6, 15.2 ] ]. Python’s plain yield does the
same. yield* takes any iterable: an array, a string, or another generator. Watch out: yield of a
generator call hands out the generator object, not its values.
Unlike temps, the feed object isn’t a generator. It’s a Feed with a generator method. Each spread
calls that method again, and each call starts a new generator at the first reading. Python’s __iter__
behaves the same way, and prints six and six too.
So a generator function is a function with a star: function*, or a star before a method’s name. Calling it
gives a generator object that yields values lazily, one pass each. A generator Symbol.iterator method gives
every loop a fresh generator, because each loop calls that method again. And yield* hands out every value of another iterable.
Next up is the text lesson Sending Values Into a Generator. There, you’ll pass a value into a paused
generator with next(), the way Python’s send() does, and end a generator early with return().