Sieve of Eratosthenes [was Re: A Pipe to the Future]

Simon Guest simon.guest at tesujimath.org
Thu Sep 6 11:57:46 UTC 2007


At Thu, 6 Sep 2007 06:52:59 +0200,
Jason Johnson wrote:
> 
> "Sieve Of Eratosthenes"
> 
> SomeClass>>sieve: aLazyList
>   ^ LazyList
>       cons: aLazyList first
>       with: (LazyList delay:
>          [ SomeClass sieve: aLazyList allButFirst
>              select [:e|  (e \\ aLazyList first = 0) not ] ])
> 
> allPrimes := SomeClass sieve: (LazyList enummerateFrom: 2 with: [:e| e + 1])
> "NOTE: This returns immediately.  But just don't call: allPrimes at:
> 10000000 on an x486"

I had a play with this; it's pretty cool.  Note that LazyList delay: 
should be LazyValue delay: (oh, and enumerateFrom with one m).

Being able to type
(allPrimes take: 100) asOrderedCollection
is rather good.

cheers,
Simon



More information about the Squeak-dev mailing list