Loading video player…

Replacing the Iterator With function*

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:

Language: JavaScript
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:

Language: Program Output
[ 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:

Language: JavaScript
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:

Language: Program Output
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:

Language: JavaScript
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:

Language: Program Output
[ 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().

00:00 In this lesson, you’ll 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.

00:10 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.

00:21 Python makes valid() a generator because a yield sits somewhere in its body. The JavaScript port so far holds the same readings.

00:30 So where does the generator go?

00:34 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.

00:44 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.

01:01 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.

01:16 Spread the generator, and the log shows the same five readings. As in Python, calling valid() runs none of its body yet. It hands back a generator object: an iterator that’s iterable too.

01:31 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, spread it twice, and the lengths come back five, then zero.

01:46 Same rule as Python: a stored generator stays used up, so call valid() again for a fresh pass.

01:55 Now for the Feed class. Its Symbol.iterator method still builds its iterator by hand, thirteen lines of counter and result objects.

02:05 In Python, __iter__ can be a generator instead: it loops over self.values and yields each one, and FeedIterator can go.

02:15 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.

02:28 Calling the method returns a generator, so spread lists all six readings, dropped read included.

02:37 Python shortens that loop to yield from. JavaScript’s spelling is yield*: the loop folds into one line. That’s a second star, with its own job: on yield, it means every value of an iterable.

02:52 The log doesn’t change. Leave the star off, and yield hands out the whole array as one value. Spread gets a single item: the array, inside another array.

03:04 Python’s plain yield does the same.

03:08 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.

03:21 Here, the log spreads feed twice and logs both lengths. Remember that temps ran dry. What does this line log? Your turn: pause the video and predict.

03:38 It logs six and six.

03:42 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.

03:53 Python’s __iter__ behaves the same way, and prints six and six too.

04:00 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.

04:13 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.

04:25 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().

Become a Member to join the conversation.