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 muhin

    anton muhin Guest

    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
    #1
    1. Advertising

  2. anton muhin

    anton muhin Guest

    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
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bengt Richter
    Replies:
    0
    Views:
    256
    Bengt Richter
    Dec 22, 2003
  2. Dave Benjamin

    Alex Martelli: Welcome back!

    Dave Benjamin, Aug 27, 2004, in forum: Python
    Replies:
    3
    Views:
    375
    Alex Martelli
    Aug 27, 2004
  3. Batista, Facundo

    RE: Alex Martelli: Welcome back!

    Batista, Facundo, Aug 27, 2004, in forum: Python
    Replies:
    4
    Views:
    357
    Paul Rubin
    Aug 28, 2004
  4. Ron Guerin
    Replies:
    3
    Views:
    329
    Ron Guerin
    Oct 14, 2005
  5. Ron Guerin
    Replies:
    0
    Views:
    256
    Ron Guerin
    Oct 17, 2005
Loading...

Share This Page