Lazy List Generator Problem

M

Mark Hahnenberg

I'm trying to make a lazy, (theoretically) infinite prime number sieve
using generators. The code I have thus far is:

#!/usr/bin/env python

import itertools

def sieve():
nats = naturals(2)
while True:
elem = nats.next()
yield elem
nats = itertools.ifilterfalse(lambda x: x % elem == 0, nats)

def naturals(start=1):
curr = start
while True:
yield curr
curr += 1

primes = sieve()
i = 0
while i < 10:
print primes.next()
i += 1

When I execute this code, the numbers 2,3,4,...,11 are printed (i.e.
nothing gets filtered out). Could anyone explain why this is
happening? I generally understand generators, and my hypothesis is
that reassigning to nats the result of filtering nats could be
screwing things up somehow, but I've tried a variety of other methods,
from making copies of the old iterator to rolling my own filter
function and nothing has worked.

Thanks,
-MRH
 
A

alex23

When I execute this code, the numbers 2,3,4,...,11 are printed (i.e.
nothing gets filtered out).  Could anyone explain why this is
happening?  I generally understand generators, and my hypothesis is
that reassigning to nats the result of filtering nats could be
screwing things up somehow, but I've tried a variety of other methods,
from making copies of the old iterator to rolling my own filter
function and nothing has worked.

Hey Mark,

I think the issue isn't that 'nats' is changing, but that 'elem' is.
Try replacing this:
nats = itertools.ifilterfalse(lambda x: x % elem == 0, nats)

with this:

nats = itertools.ifilterfalse(lambda x, elem=elem: x % elem ==
0, nats)

Which works for me:

In [91]: s = sieve()
In [92]: primes_10 = [s.next() for x in xrange(10)]
In [93]: primes_10
Out[93]: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]


Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top