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

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

anton muhin
Guest
Posts: n/a

 11-01-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.

anton muhin
Guest
Posts: n/a

 11-01-2004
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.

 Thread Tools

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Delaney, Timothy C (Timothy) Python 1 08-30-2004 08:07 AM Batista, Facundo Python 4 08-27-2004 11:38 PM Michael Sparks Python 1 08-27-2004 09:31 PM Dave Benjamin Python 3 08-27-2004 09:08 PM Bengt Richter Python 0 12-22-2003 02:37 AM

Advertisments