how to make a generator use the last yielded value when it regains control
Azolex
cretin at des.alpes.ch
Mon Apr 10 07:26:23 EDT 2006
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 length_encode(morris(seed,n-1))
What's wrong with the following ?
def morris(seed,n) :
"""..."""
for k in xrange(n-1) :
seed=length_encode(seed)
return seed
or even
def morris(seed,n) :
return reduce(lambda x,y:y(x),n*[length_encode],seed)
I'd defend using recursion when it allows a more concise expression of
an algorithm, but not in other cases.
Mmmhhh, btw, strangely, it looks like a hole in the library that you
can't write eg
morris= lambda seed,n: reduce(operator.__rcall__,n*[length_encode],seed)
More information about the Python-list
mailing list