replay 'apply' with extended call

A

Alan G Isaac

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
 
A

Andrew Dalke

Alan said:
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
(e-mail address removed)
 
P

Peter Otten

Alan said:
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:
.... 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
 
A

Alan G Isaac

Andrew Dalke said:
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
 
R

Russell Blau

Alan G Isaac said:
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.
 
P

Peter Otten

Alan said:
Andrew Dalke said:
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
[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
 

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

Members online

No members online now.

Forum statistics

Threads
474,209
Messages
2,571,088
Members
47,687
Latest member
IngridXxj

Latest Threads

Top