List comprehension confusion...
Lulu of the Lotus-Eaters
mertz at gnosis.cx
Sun Feb 2 14:06:32 EST 2003
|> Peter Hansen <peter at engcorp.com> wrote previously:
|> I actually kinda agree. Most people dread lambdas because of what they
|> are (the semantics). Myself, I only have trouble with their spelling.
|Yeah, it's basically the syntax. I find the missing return statement
|bothersome, somewhere in the back of my mind, and also the parentheses
|that aren't there around the arguments...
Ah... I think you haven't quite "got" the gestalt switch yet, Peter.
The reason I use lambdas at all is BECAUSE of the lack of return
statment... that helps express a concept. Here's an explanation that I
hope might help:
A *pure* function is simply a type of mapping between a domain and a
range. In mathematics, it is no problem to express infinite domains and
ranges (or just large ones); but in programs it is often convenient to
express a calculable relationship algorithmically rather than
statically. The thing to understand is that these are not fundamentally
different creatures, just different conveniences. For example, compare:
_2xdict = {1:2, 2:4, 3:6}
_2xfunc = lambda i: i*2
print _2xdict[2]
print _2xfunc(2)
Obviously, the dict has a restricted domain compared to the function,
but within their common domain, they do exactly the same thing.
I will often explictly name a function using a lambda form when I want
to emphasize that it really is a pure function, acting as a mapping. A
mapping does not "return", it just has a correspondence between objects,
hence no 'return' statement.
Of course, it is easily possible to smuggle side-effects into a lambda
form in various ways. So it's not really true that every lambda is a
pure function. But as a convention, I find it helpful to use a lambda
as a way of indicating that I am eschewing side effects (or at least
making them "safe" ones).
Yours, Lulu...
--
mertz@ _/_/_/_/_/_/_/ THIS MESSAGE WAS BROUGHT TO YOU BY:_/_/_/_/ v i
gnosis _/_/ Postmodern Enterprises _/_/ s r
.cx _/_/ MAKERS OF CHAOS.... _/_/ i u
_/_/_/_/_/ LOOK FOR IT IN A NEIGHBORHOOD NEAR YOU_/_/_/_/_/ g s
More information about the Python-list
mailing list