Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Reverse of map()?

Reply
Thread Tools

Reverse of map()?

 
 
Phillip Sitbon
Guest
Posts: n/a
 
      02-06-2006
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?

 
Reply With Quote
 
 
 
 
Alex Martelli
Guest
Posts: n/a
 
      02-06-2006
Phillip Sitbon <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Raymond Hettinger
Guest
Posts: n/a
 
      02-06-2006
[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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
cisco/DNP/SCADA and reverse telnet A. Andrews Cisco 0 01-08-2004 06:32 PM
Reverse engineering an EDIF file? Rastislav Struharik VHDL 8 01-02-2004 01:57 PM
Question on Cisco reverse Subnets James Roper Cisco 2 12-16-2003 10:37 PM
Stacks Queues Reverse Reverse Polish dogbite C++ 4 10-10-2003 05:06 AM
Re: AS5350 reverse telnet Aaron Leonard Cisco 0 07-09-2003 11:49 PM



Advertisments