2 ms·
> If one ends up creating a large list of prime numbers with this generator But one does not always end up creating a large list of prime numbers, do they? >
by masklinn 3y ago
> If one ends up creating a large list of prime numbers with this generator
But one does not always end up creating a large list of prime numbers, do they?
> why not just return a large list of prime numbers?
Because that requires upfront knowledge of the filtering factor or absence thereof.
A generator means you don't care, you generate an infinite sequence and the consumer is free to do whatever they want.
> It is obvious that returning an array of prime numbers is what one would do in C/C++/Rust.
It's certainly not obvious for Rust.
- philipLutz 3y ago"I can't try to rewrite it in C++ or Rust for now, due to the lack of generator support; the yield statement is what makes this code so nice and elegant, and alternative idioms are much less convenient."
- masklinn 3y agoThat is the context of the first sentence in your comment, pretty much the only part I left alone. Everything I replied to is the opinion you personally expressed. And note that the author had already preempted your comment in the first place: > When we want a list of all the primes below some known limit, gen_primes_upto is great, and performs fairly well. There are two issues with it, though: > We have to know what the limit is ahead of time; this isn't always possible or convenient.