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. 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
by: Dave Benjamin |
last post by:
It's nice to see you around here again!
Dave
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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,...
|
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...
|
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...
| |