map/filter/reduce/lambda opinions and background unscientific mini-survey

Terry Hancock hancock at anansispaceworks.com
Fri Jul 1 19:06:39 EDT 2005


On Friday 01 July 2005 03:36 pm, Ron Adam wrote:
> I find map too limiting, so won't miss it.  I'm +0 on removing lambda 
> only because I'm unsure that there's always a better alternative.

Seems like some new idioms would have to be coined, like:

def my_function(a1, a2):
    def _(a,b): return a+b
    call_a_lib_w_callback(callback=_)

doesn't seem too bad, and defeats the "wasting time thinking of names"
argument.

> So what would be a good example of a lambda that couldn't be replaced?

I suspect the hardest would be building a list of functions. Something
like:

powers = [lambda a, i=i: a**i for i in range(10)]

which you might be able to make like this:

powers = []
for i in range(10):
    def _(a,i=i): return  a**i
    powers.append(_)

which works and is understandable, but a bit less concise.

The main obstacle to the lambda style here is that def statements
are not expressions.  I think that's been proposed as an alternative,
too -- make def return a value so you could say:

powers = [def _(a,i=i): return a**i for i in range(10)]

Personally, I think this is understandable, and given that lambda
is to be pulled, a nice substitute (I would say it is easier to read
than the current lambda syntax, and easier for a newbie to
understand).

But it would probably encourage some bad habits, such as:

myfunc = def _(a,b):
    print a,b
    return a+b

which looks too much like Javascript, to me, where there are
about three different common idioms for defining a
function (IIRC).  :-/

--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks  http://www.anansispaceworks.com




More information about the Python-list mailing list