3 ms·
> while(true) You're a braver man than I.
by ramesh31 3y ago
> while(true)
You're a braver man than I.
- AprilArcus 3y agoThis is a common idiom when working with generators, since iteration is controlled on the consuming side. Consider e.g. a trivial generator that counts up from one: function * makeSequence() { let i = 1; while (true) yield i++; } This function returns an iterator: const ints = makeSequence(); And now the caller is control of iteration, not the callee: function sumTo(cutoff) { const ints = makeSequence(); let sum = 0; for (let i = 0; i < cutoff; i++) { sum += ints.next().value; } return sum; } In this case there is no risk of an infinite loop.
- ramesh31 3y ago>This is a common idiom when working with generators, since iteration is controlled on the consuming side. I realize that, and it's why I (and most) have avoided the syntax entirely. Generators today feel more like a vestigial appendix to the language; an aborted branch of development, than anything I'd ever use or suggest.
- elwell 3y agoMore brave would be: while (0.1 + 0.2 !== 0.3)
- everforward 3y agoThings like that tend to be relatively easy to unit test as long as you have sane (read: few) exit conditions.