What is the "functional" way of doing this?
Paul Rubin
http
Mon Jul 30 22:19:21 EDT 2007
James Stroud <jstroud at mbi.ucla.edu> writes:
> def f(n):
> if n>0:
> yield n%26
> for i in f(n/26):
> yield i
Right, this mutates i though. Let's say we have a library function
itertools.iterate() and that we ignore (as we do with functions like
"map") that it uses mutation under the clothes:
def iterate(f, x):
# generate the infinite sequence x, f(x), f(f(x)), ...
while True:
yield x
x = f(x)
Then we could write:
from itertools import imap, takewhile
def snd((a,b)): return b
def f(n):
return takewhile(bool,
imap(snd,
iterate(lambda (a,b): divmod(a,26),
divmod(n,26))))
More information about the Python-list
mailing list