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 5 1430
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***@dalkescie ntific.com
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
"Andrew Dalke" <ad****@mindspr ing.com> wrote in message
news:b6******** *******@newsrea d3.news.pas.ear thlink.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
"Alan G Isaac" <ai****@america n.edu> wrote in message
news:10******** *****@corp.supe rnews.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.
Alan G Isaac wrote: "Andrew Dalke" <ad****@mindspr ing.com> wrote in message news:b6******** *******@newsrea d3.news.pas.ear thlink.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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 and is supposed to provie one output.
When I call it in SQL Query Analyser all runs OK and I get a column result
(single result) titled "Output_Info" with a value of say 300051
|
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 method. But after
calling it in T-SQL, I became such a error message:
|
by: jygoh3 |
last post by:
ENCYCLOPEDIA OF MOBILE COMPUTING & COMMERCE
CALL FOR SHORT ARTICLES
Proposal Deadline: 15 Nov 2005 (Extended)
|
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 find a similar
process for C#. Does anyone know if such an animal exists?
I believe I could call the C# routines from within a C++ extended
stored procedure wrapper, but I stink as a C++ programmer (pointers?
what?) and don't want to learn C++...
|
by: Stelrad Doulton |
last post by:
Hi,
Does anyone know what number to use with GetEncoding for 8 bit ASCI?
Cheers
| |
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,
|
by: Baheri |
last post by:
Does any one have a sample on how can replay attacks be prevented in a
webservice?
|
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 ProcessCmdKey(ref Message msg, Keys keyData)
To map "Keys" objects to their string constant, and stored them on a string Queue as they were happening. Then (after resetting the form), I simulate replaying of these actions by flushing the queued...
|
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 server. Does anyone
know of such a tool from IBM or another vendor?
Thanks.
Lew
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
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...
|
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...
|
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...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |