473,394 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

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.
Jul 18 '05 #1
1 1264
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.
Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Mars | last post by:
I have looked long and hard at Mr. Martelli's Borg recipe: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66531 It is a very useful substitute for a Singleton, but I can't figure out...
23
by: Francis Avila | last post by:
Below is an implementation a 'flattening' recursive generator (take a nested iterator and remove all its nesting). Is this possibly general and useful enough to be included in itertools? (I know...
3
by: Dave Benjamin | last post by:
It's nice to see you around here again! Dave
6
by: Robert Brewer | last post by:
def warehouse(stock, factory=None): """warehouse(stock, factory=None) -> iavailable, iremainder. Iterate over stock, yielding each value. Once the 'stock' sequence is exhausted, the factory...
21
by: Steven Bethard | last post by:
Jack Diederich wrote: > > itertools to iter transition, huh? I slipped that one in, I mentioned > it to Raymond at PyCon and he didn't flinch. It would be nice not to > have to sprinkle 'import...
1
by: Steven Bethard | last post by:
Michael Spencer wrote: > While we're on the topic, what do you think of having unary, > non-summary builtins automatically map themselves when called with an > iterable that would otherwise be an...
3
by: Ron Guerin | last post by:
The New York Linux User's Group invites you to a special presentation by Alex Martelli of Google, on the Python Object Model. This presentation will be held at P.J. Clarke's Sidecar, rather than...
0
by: Ron Guerin | last post by:
(date and time inadvertently omitted last time. sorry!) The New York Linux User's Group invites you to a special presentation by Alex Martelli of Google, on the Python Object Model. This...
41
by: rurpy | last post by:
The code below should be pretty self-explanatory. I want to read two files in parallel, so that I can print corresponding lines from each, side by side. itertools.izip() seems the obvious way to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.