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

A

anton muhin

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.
 
A

anton muhin

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top