itertools: followup to Alex Martelli problem ("number-in-base" ``oneliner'') and Bengt Richter solution
anton muhin
antonmuhin at rambler.ru
Mon Nov 1 07:47:55 EST 2004
Hello, everybody!
Trying to solve the problem in the subj, I found that I miss some
iterator-related tools. Mostly consequental application of the same
function to some argument (if I'm not missing something it has a name
y-combinator).
If we had one, generating the sequence of digits is easy:
iter(y(lambda (q, _): divmod(q, n), (x, 0)).next, (0, 0))
and if we have something like this in itertools
def y(f, x):
while True:
yield x
x = f(x)
it might be one of simplest solutions possible.
I tried to emulate it in the way Bengt wrote his solution:
def y(f, x):
return (h for t in [itertools.repeat(x)] for h, t in iter(lambda:
(t.next(), itertools.imap(f, t)), None))
but it, should I say, a little bit too complex
Therefore, a couple of questions:
1) is there easier way to write y with genexps?
2) don't we need y in itertools?
Partially answering the second question: of course, y is too abstract
and, at least IMHO, doesn't fit Python ideology. However, most of
itertools IMHO is rather abstract :)
With the best regards,
anton.
More information about the Python-list
mailing list