468,110 Members | 1,821 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,110 developers. It's quick & easy.

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 1519
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)*(Args,), len(fn_list)*(Kwds))
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)*(Args,), len(fn_list)*(Kwds))
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by microsoft.public.dotnet.languages.csharp | last post: by
26 posts views Thread by jshanman | last post: by
11 posts views Thread by Noah | last post: by
4 posts views Thread by qlin88 | last post: by
1 post views Thread by Solo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.