Sub topic 10
Taming Infinite Sequences
This chapter assumes you know generator functions, how take limits output, and how consuming methods such as toArray and find pull values through a pipeline. Here we put those pieces together to work safely with sequences that never end.
Why infinite sequences are possible at all
A generator with while (true) never finishes by itself. That is harmless, because a generator does nothing until something calls next(). Iterator helpers keep this property. Each helper pulls one value at a time from the stage before it. No stage ever asks for "everything." Only the consumer at the end decides how much work happens.
function* fibonacci() {
let [a, b] = [0, 1];
while (true) {
yield a;
[a, b] = [b, a + b];
}
}
console.log(fibonacci().take(8).toArray());
// [0, 1, 1, 2, 3, 5, 8, 13]toArray keeps pulling until its source reports done: true. On its own, fibonacci() never does that. Adding take(8) makes the sequence finite, so toArray can return.
Consumers that stop on their own
Some consuming methods stop early, so they work on infinite sequences without take:
function* naturals() {
let n = 0;
while (true) yield n++;
}
console.log(fibonacci().find((n) => n > 1000)); // 1597
console.log(naturals().some((n) => n * n > 50)); // true (stops at 8)
console.log(naturals().every((n) => n < 5)); // false (stops at 5)find and some stop at the first match. every stops at the first failure. Each one returns as soon as it knows the answer.
| Consumer | Safe on an infinite source? |
|---|---|
| `toArray`, `reduce`, `forEach` | Never. They always read to the end |
| `find`, `some` | Only if some value eventually matches |
| `every` | Only if some value eventually fails |
The filter trap
take only counts values that reach it. If filter sits before take and rejects every value, take never receives anything to count, and the loop never ends:
// Hangs: no natural number is negative, so take(1) waits forever.
// naturals().filter((n) => n < 0).take(1).toArray();The same thing happens with a filter that matches rarely and less often over time, such as "primes with a gap of a million." take(n) only bounds the work when the filter keeps letting values through.
const evensInFirst100 = naturals()
.take(100)
.filter((n) => n % 2 === 0)
.toArray();
console.log(evensInFirst100.length); // 50drop is also safe on an infinite source. It discards a fixed number of values and then passes the rest through, so it never waits forever.
Exercise
Which of these terminate? (1) naturals().map((n) => n * 2).toArray() (2) naturals().find((n) => n > 10) (3) naturals().filter((n) => n > 10).take(3).toArray() (4) naturals().every((n) => n >= 0)
Show answer
(2) returns 11, and (3) returns [11, 12, 13]. (1) never ends, because toArray has no limit. (4) never ends, because every natural number is >= 0, so every never finds a value that fails.
Exercise
Write an expression that returns the first five Fibonacci numbers greater than 100, using fibonacci() from this chapter.
Show answer
fibonacci().filter((n) => n > 100).take(5).toArray(), which gives [144, 233, 377, 610, 987]. It terminates because infinitely many Fibonacci numbers exceed 100.