Reverse of map()?

P

Phillip Sitbon

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

Alex Martelli

Phillip Sitbon said:
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
 
R

Raymond Hettinger

[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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top