# itertools: followup to Alex Martelli problem ("number-in-base" ``oneliner'')and Bengt Richter soluti

Discussion in 'Python' started by anton muhin, Nov 1, 2004.

1. ### anton muhinGuest

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.

anton muhin, Nov 1, 2004

2. ### anton muhinGuest

Re: itertools: followup to Alex Martelli problem ("number-in-base"``oneliner'') and Bengt Richter solution

Oops, it not y-combinator itself, but rather releated thing, sorry.

And as followup: iter(callable, sentinel) seems a little bit too
restricted for me. The reason is simple: no params are passed to
callable and, therefore, it is usually should return the same value. Of
course, there are a lot of callable things in Python that don't behave
this way, but shouldn't iter have interface like iter(callable, params)
that would generated iterator [x, f(x), f(f(x)), ...] that can be stoped
with itertools.takewhile if needed?

with the best regards,
anton.

anton muhin, Nov 1, 2004