Priya’s feed now skips its dropped reads one value at a time. The dashboard also shows short previews, which the Python service builds with itertools. There, count() numbers the samples, generator expressions transform them, and islice() selects the part the dashboard needs.
Each step is lazy, so Python never builds a list it doesn’t need, even from an endless source. In this lesson, you’ll build the same lazy pipelines in JavaScript by chaining methods onto an iterator, starting with the first few samples.
Take the First Three Sample Numbers
The station numbers its samples from zero, forever, and the preview labels its first three rows with those numbers. In Python, count() produces them, and islice() stops after three:
from itertools import count, islice
print(list(islice(count(), 3)))
That prints [0, 1, 2]. JavaScript has no itertools module, so the port writes the counter as a generator function with function*. Inside it, while (true) is Python’s while True:, and n++ gives the current n and adds one to it, so yield n++ hands out 0, 1, 2, and so on. With no islice() to reach for, it’s fair to ask whether the .slice() you know from arrays also works on a generator:
function* counter() {
let n = 0;
while (true) yield n++;
}
console.log(counter().slice(0, 3));
Before you run it, decide what you expect: three numbers, a hang because the counter never ends, or something else. JavaScript stops at once. This shortened output omits the source context and stack trace:
...
TypeError: counter(...).slice is not a function
...