473,791 Members | 3,277 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reverse of map()?

Hello there,

I have a situation where a list of functions need to be called with a
single set of parameters and the result constructed into a tuple. I
know there's simple ways to do it via list comprehension:

Result = tuple( [ fn(* Args, ** Kwds) for fn in fn_list ] )

I'd hope there's a more efficient way to do this with a built-in
function, so that I could call:

Result = rmap( fn_list, * Args, ** Kwds )

and have it constructed as a tuple from the get-go.

Is there a built-in function that would allow me to do this, or do I
have to go with the list comprehension?

Feb 6 '06 #1
2 1696
Phillip Sitbon <ph************ @gmail.com> wrote:
Hello there,

I have a situation where a list of functions need to be called with a
single set of parameters and the result constructed into a tuple. I
know there's simple ways to do it via list comprehension:

Result = tuple( [ fn(* Args, ** Kwds) for fn in fn_list ] )

I'd hope there's a more efficient way to do this with a built-in
function, so that I could call:

Result = rmap( fn_list, * Args, ** Kwds )

and have it constructed as a tuple from the get-go.

Is there a built-in function that would allow me to do this, or do I
have to go with the list comprehension?


A genexp is probably going to be more efficient than the list
comprehension: just omit the brackets in your first snippet.

map(apply, fn_list, ...) may work, but I doubt it's going to be either
simple or speedy since the ... must be replaced with as many copies of
Args and Kwds as there are functions in fn_list, e.g.:

map(apply, fn_list, len(fn_list)*(A rgs,), len(fn_list)*(K wds))
There's no built-in that calls many functions with identical args and
kwds, since it's a rare need. Also, map returns a list, not a tuple, so
it's bordering on the absurd to think that your dreamed-for rmap would
be designed to return a tuple rather than a list -- you still have to
call tuple on the result, any way you build it.
Alex
Feb 6 '06 #2
[Alex Martelli]
map(apply, fn_list, ...) may work, but I doubt it's going to be either
simple or speedy since the ... must be replaced with as many copies of
Args and Kwds as there are functions in fn_list, e.g.:

map(apply, fn_list, len(fn_list)*(A rgs,), len(fn_list)*(K wds))
The repeat() function in the itertools module was designed to fulfill
this need and consume less memory in the process:

from itertools import repeat
n = len(fn_list)
map(apply, fn_list, repeat(Args, n), repeat(Kwds, n))
There's no built-in that calls many functions with identical args and
kwds, since it's a rare need.


Also, it is almost certain that the function calls and their execution
will dominate his runtime. Even if the fantasied rmap() function
existed, it would be unlikely to help.
Raymond

Feb 6 '06 #3

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

Similar topics

2
2111
by: microsoft.public.dotnet.languages.csharp | last post by:
I have Visio Professional that comes with the MSDN Universal subscription. I need to reverse engineer a database using Visio and create the DDL. I can only find information on creating the script by building the database within Visio. Our database is already created. I want to reverse engineer it. The main problem is that we don't know what you have to do to map the objects that Visio creates and assign them as the different database...
26
2729
by: jshanman | last post by:
I am writing a timeline that uses Google Maps. I have a function that converts a date time to latitude coords. This function is used to draw the markers on the timeline. I need a reverse function to convert a latitude coord back to an accurate date time. Then I could detect the day/hour in the viewport when the timeline is zoomed in or out, or if it is moved. I cannot use the javascript date functions because this timeline will...
11
16807
by: Noah | last post by:
I have a list of tuples I want to reverse the order of the elements inside the tuples. I know I could do this long-form: q = y = for i in y: t=list(t)
1
10760
by: sunnyluthra1 | last post by:
Hi, I was creating an Application in MS Access for Geocoding a particular Address from Google to get the Lat & Long. I successfully able to did that. Here is the code: **************************** On Error Resume Next 'if address not found, just move along Dim xml_document As DOMDocument Set xml_document = New DOMDocument Dim rootNode As IXMLDOMNode
4
14979
by: qlin88 | last post by:
Hi, In STL multi-map, the lower_bound, upper_bound,equal_range all return an iterator. Now ifone wants to iterate from an upper bound towards a lower bound, what would be the best way to do it? I tried to interate backwards with an iterator, but the begin() element was not properly included. Thanks for your help.
0
10428
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
10207
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...
0
9997
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...
0
9030
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7537
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
5435
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...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3718
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2916
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.