473,395 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

replay 'apply' with extended call

Consider an example due to Mertz (in Text Processing in Python):
apply_each = lambda fns, args=[]: map(apply, fns, [args]*len(fns))
This allows one to supply a list of functions and a tuple of arguments
to produce a list evaluating each function with those arguments.

But 'apply' is deprecated in favor of extended call syntax.
What is the equivalent with extended call syntax?

Thanks,
Alan Isaac
Jul 18 '05 #1
5 1414
Alan G Isaac wrote:
apply_each = lambda fns, args=[]: map(apply, fns, [args]*len(fns)) What is the equivalent with extended call syntax?


How about a solution which replaces the 'map' with a
list comprehension?

def apply_each(fns, args = []):
return [fn(*args) for fn in fns]

Conversion to lambda form is trivial for this case but I
figured if you're going to name it, why use a lambda?
Andrew
da***@dalkescientific.com
Jul 18 '05 #2
Alan G Isaac wrote:
Consider an example due to Mertz (in Text Processing in Python):
apply_each = lambda fns, args=[]: map(apply, fns, [args]*len(fns))
This allows one to supply a list of functions and a tuple of arguments
to produce a list evaluating each function with those arguments.

But 'apply' is deprecated in favor of extended call syntax.
What is the equivalent with extended call syntax?


The literal translation would be:
def apply_each(fns, *args, **kw): .... return [fn(*args, **kw) for fn in fns]
.... times2 = 2 .__mul__
times4 = 4 .__mul__
apply_each([times2, times4], 3) [6, 12]

But nobody would bother defining such an apply_each() function anymore.
Instead you can use a list comprehension directly:
[f(3) for f in [times2, times4]]

[6, 12]
Peter

Jul 18 '05 #3

"Andrew Dalke" <ad****@mindspring.com> wrote in message
news:b6***************@newsread3.news.pas.earthlin k.net...
How about a solution which replaces the 'map' with a
list comprehension?
def apply_each(fns, args = []):
return [fn(*args) for fn in fns]
Conversion to lambda form is trivial for this case but I
figured if you're going to name it, why use a lambda?

This raises another (newbie) question that I had.
Take a trivial example:

from operator import truth
bool1 = lambda lst: map(truth, lst)
def bool2(lst): return map(truth,lst)
def bool3(lst): return [truth(_) for _ in lst]

To my eyes, the most natural is bool2.
I would never have considered bool1 if
I had not come across it in the Merz book,
but it is both shortest and clear.
I include bool3 just for comparison: I think
the way in which it is harder to read illustrates
the usefulness of 'map'.

So, are there any obvious considerations when
making a choice among these. In particular,
why might someone prefer the style in bool1?

Thanks,
Alan Isaac
Jul 18 '05 #4
"Alan G Isaac" <ai****@american.edu> wrote in message
news:10*************@corp.supernews.com...

This raises another (newbie) question that I had.
Take a trivial example:

from operator import truth
bool1 = lambda lst: map(truth, lst)
def bool2(lst): return map(truth,lst)
def bool3(lst): return [truth(_) for _ in lst]

To my eyes, the most natural is bool2.
I would never have considered bool1 if
I had not come across it in the Merz book,
but it is both shortest and clear.
I include bool3 just for comparison: I think
the way in which it is harder to read illustrates
the usefulness of 'map'.


You find bool3 *harder* to read? I guess that just illustrates what a
wonderfully diverse world we live in.

--
I don't actually read my hotmail account, but you can replace hotmail with
excite if you really want to reach me.
Jul 18 '05 #5
Alan G Isaac wrote:

"Andrew Dalke" <ad****@mindspring.com> wrote in message
news:b6***************@newsread3.news.pas.earthlin k.net...
How about a solution which replaces the 'map' with a
list comprehension?
def apply_each(fns, args = []):
return [fn(*args) for fn in fns]
Conversion to lambda form is trivial for this case but I
figured if you're going to name it, why use a lambda?

This raises another (newbie) question that I had.
Take a trivial example:

from operator import truth
bool1 = lambda lst: map(truth, lst)
def bool2(lst): return map(truth,lst)
def bool3(lst): return [truth(_) for _ in lst]

To my eyes, the most natural is bool2.
I would never have considered bool1 if
I had not come across it in the Merz book,
but it is both shortest and clear.
I include bool3 just for comparison: I think
the way in which it is harder to read illustrates
the usefulness of 'map'.

So, are there any obvious considerations when
making a choice among these. In particular,
why might someone prefer the style in bool1?


I agree with you that using lambda is bad style for defining a named
function. I also prefer map() over list comprehensions if the expression is
a simple call to a predefined function, but a list comprehension is much
more flexible as it allows for expressions instead of function calls and
covers the functionality of filter() and map() in one pass. Personally, I
would just use the inline version of bool2, i. e. write
map(bool, ["", [], 0, 0.0])

[False, False, False, False]

directly without bothering to define a function first. I didn't know about
truth(), but it seems to be equivalent to bool() - so why bother with the
import?

Peter
Jul 18 '05 #6

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

Similar topics

1
by: Craig Hoskin | last post by:
Hi all Ive got a problem I was hoping someone may be able to help with. Im calling an extended stored procedure provided by a third party (master..xp_OrderHeader). This xp requires 3 inputs...
3
by: Matik | last post by:
Hi, I alredy tried to search this problem in last posts, but I couldn't find the answer. I try to access via Extended SP the method in a dll. I registered the dll as a ExSP, with a name of...
0
by: jygoh3 | last post by:
ENCYCLOPEDIA OF MOBILE COMPUTING & COMMERCE CALL FOR SHORT ARTICLES Proposal Deadline: 15 Nov 2005 (Extended)
4
by: icebrrrg | last post by:
Hi. I have some functionality in a C# DLL that I would like to be able to call from inside SQLServer. I know this was possible in C++, using an Extended Stored Procedure, but I haven't been able to...
5
by: Stelrad Doulton | last post by:
Hi, Does anyone know what number to use with GetEncoding for 8 bit ASCI? Cheers
1
by: newcomer | last post by:
I have traced action in XML. Now I would like to replay that. How could I do that based on this XML trace? I am using soap:tcp between client and server. thanks,
3
by: Baheri | last post by:
Does any one have a sample on how can replay attacks be prevented in a webservice?
0
by: neonspark | last post by:
I'm buidling some simple macro functionality for my app so the users can record a sequence of keyboard inputs and replay them reliably via some menu. Originally, I used: protected override bool...
7
by: Lew | last post by:
Hi, I'm looking for a tool that can capture all the sql transactions for a period of time (24 hours or so ) from our production server and replay it exactly as entered on our performance tuning...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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...

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.