473,809 Members | 2,731 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

itertools: followup to Alex Martelli problem ("number-in-base" ``oneliner'')an d 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.repea t(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 1281
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.takew hile 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
2654
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 how it works. _shared_state is never assigned any value, only self.__dict__ is assigend self._shared_sate's value - or rather (self.)_shared_state must be assigned a value at some point, otherwise the code wouldn't work(right ?). I have gone...
23
3742
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 *I* wanted something like it...) Very basic examples: >>> rl = , '678', 9]] >>> list(flatten(rl)) >>> notstring = lambda obj: not isinstance(obj, type(''))
3
1474
by: Dave Benjamin | last post by:
It's nice to see you around here again! Dave
6
1745
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 function (or any callable, such as a class) is called to produce a new valid object upon each subsequent call to next().
21
2191
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 itertools as it' in code. iter could also > become a type wrapper instead of a function, so an iter instance could > be a wrapper that figures out whether to call .next or __getitem__ > depending on it's argument. > for item in...
1
1306
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 illegal argument: > > e.g., > int(iterable) -> (int(i) for i in iterable) > ord(iterable) -> (ord(i) for i in iterable) > >
3
1739
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 our usual location, and Google is picking up the tab for an hour and a half of open bar and food. Additionally, if you're looking for a job as a Python developer, bring your resume. Please RSVP at http://rsvp.nylug.org to attend, as seating is...
0
1392
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 presentation will be held at P.J. Clarke's Sidecar, rather than our usual location, and Google is picking up the tab for an hour and a half of open bar and food. Additionally, if you're looking for a job as a Python developer, bring your resume. ...
41
2687
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 do this. izip() will stop interating when it reaches the end of the shortest file. I don't know how to tell which file was exhausted so I just try printing them both. The exhausted one will generate a
0
9601
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10635
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10378
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10115
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7653
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.