Re: how to make a generator use the last yielded value when it regains control

2006-04-10 Thread Azolex
Lonnie Princehouse wrote: Here's my take on the thing. It only prints one term, though. http://www.magicpeacefarm.com/lonnie/code/morris.py.html (a bit too long to post) excerpt : def morris(seed, n): ... if n == 1: return seed else: return

Re: how to make a generator use the last yielded value when it regains control

2006-04-10 Thread Lonnie Princehouse
What's wrong with the following ? def morris(seed,n) : ... for k in xrange(n-1) : seed=length_encode(seed) return seed Nothing's wrong with it. I happen to think the recursive version is more elegant, but that's just me ;-) --

Re: how to make a generator use the last yielded value when it regains control

2006-04-10 Thread Felipe Almeida Lessa
Em Seg, 2006-04-10 às 10:05 -0700, Lonnie Princehouse escreveu: I happen to think the recursive version is more elegant, but that's just me ;-) It may be elegant, but it's not efficient when you talk about Python. Method calls are expensive: $ python2.4 -mtimeit 'pass' 1000 loops, best of

Re: how to make a generator use the last yielded value when it regains control

2006-04-10 Thread Lonnie Princehouse
In general, you're right - if speed is a concern, loops should be used instead of recursion in Python when possible. In this case, the recursive overhead is insignificant compared to the expense of iterating over the sequence at every iteration. By the time there are 70 stack frames of recursion

Re: how to make a generator use the last yielded value when it regains control

2006-04-08 Thread Michael Spencer
John Salerno wrote: Ben Cartwright wrote: Definitely go for (1). The Morris sequence is a great candidate to implement as a generator. As a generator, it will be more flexible and efficient than (2). Actually I was just thinking about this and it seems like, at least for my purpose

Re: how to make a generator use the last yielded value when it regains control

2006-04-08 Thread John Salerno
Michael Spencer wrote: itertools.groupby makes this very straightforward: I was considering this function, but then it seemed like it was only used for determing consecutive numbers like 1, 2, 3 -- not consecutive equivalent numbers like 1, 1, 1. But is that not right? --

Re: how to make a generator use the last yielded value when it regains control

2006-04-08 Thread Gerard Flanagan
John Salerno wrote: Michael Spencer wrote: itertools.groupby makes this very straightforward: I was considering this function, but then it seemed like it was only used for determing consecutive numbers like 1, 2, 3 -- not consecutive equivalent numbers like 1, 1, 1. But is that not right?

Re: how to make a generator use the last yielded value when it regains control

2006-04-08 Thread Michael Spencer
John Salerno wrote: Michael Spencer wrote: itertools.groupby makes this very straightforward: I was considering this function, but then it seemed like it was only used for determing consecutive numbers like 1, 2, 3 -- not consecutive equivalent numbers like 1, 1, 1. But is that not

Re: how to make a generator use the last yielded value when it regains control

2006-04-08 Thread John Salerno
Gerard Flanagan wrote: John Salerno wrote: Michael Spencer wrote: itertools.groupby makes this very straightforward: I was considering this function, but then it seemed like it was only used for determing consecutive numbers like 1, 2, 3 -- not consecutive equivalent numbers like 1, 1, 1.

Re: how to make a generator use the last yielded value when it regains control

2006-04-07 Thread Lonnie Princehouse
Here's my take on the thing. It only prints one term, though. http://www.magicpeacefarm.com/lonnie/code/morris.py.html (a bit too long to post) -- http://mail.python.org/mailman/listinfo/python-list

Re: how to make a generator use the last yielded value when it regains control

2006-04-07 Thread Azolex
just couldn't help taking the bait... def morris(seed) : m = morris('3447221') m.next() '1324172211' m.next() '1113121411172221' m.next() '31131112111431173211' assert isinstance(seed,basestring) and seed.isdigit(),bad seed def

Re: how to make a generator use the last yielded value when it regains control

2006-04-07 Thread John Salerno
Lonnie Princehouse wrote: Here's my take on the thing. It only prints one term, though. http://www.magicpeacefarm.com/lonnie/code/morris.py.html (a bit too long to post) yikes, scary! :) there was always the hint that using itertools might be helpful, as you guys are doing, but

how to make a generator use the last yielded value when it regains control

2006-04-06 Thread John Salerno
Ok, I wrote this all by myself, which explains why it doesn't work. It is meant to take a number and generate the next number that follows according to the Morris sequence. It works for a single number, but what I'd like it to do is either: 1. repeat indefinitely and have the number of times

Re: how to make a generator use the last yielded value when it regains control

2006-04-06 Thread John Salerno
John Salerno wrote: 1. repeat indefinitely and have the number of times controlled elsewhere in the program (e.g., use the morris() generator in a for loop and use that context to tell it when to stop) 2. just make it a function that takes a second argument, that being the number of

Re: how to make a generator use the last yielded value when it regains control

2006-04-06 Thread John Salerno
John Salerno wrote: 2. just make it a function that takes a second argument, that being the number of times you want it to repeat itself and create numbers in the sequence Here's what I've come up with so far. Probably not the most elegant solution because of the nested function, but it

Re: how to make a generator use the last yielded value when it regains control

2006-04-06 Thread Lonnie Princehouse
The generator in your original post /does/ rebind seed, but only on the last iteration of the loop. You'll need to wrap that loop in another loop if you want the generator to yield more than once. As for communicating with a generator --- e.g. telling it to stop --- this might be done by passing

Re: how to make a generator use the last yielded value when it regains control

2006-04-06 Thread Ben Cartwright
John Salerno wrote: It is meant to take a number and generate the next number that follows according to the Morris sequence. It works for a single number, but what I'd like it to do is either: 1. repeat indefinitely and have the number of times controlled elsewhere in the program (e.g., use

Re: how to make a generator use the last yielded value when it regains control

2006-04-06 Thread John Salerno
Ben Cartwright wrote: Definitely go for (1). The Morris sequence is a great candidate to implement as a generator. As a generator, it will be more flexible and efficient than (2). Actually I was just thinking about this and it seems like, at least for my purpose (to simply return a list of

Re: how to make a generator use the last yielded value when it regains control

2006-04-06 Thread Ben Cartwright
John Salerno wrote: Actually I was just thinking about this and it seems like, at least for my purpose (to simply return a list of numbers), I don't need a generator. Yes, if it's just a list of numbers you need, a generator is more flexibility than you need. A generator would only come in