a small problem

Gerrit Holl gerrit.holl at pobox.com
Wed Jan 12 10:59:32 EST 2000


Thomas Wouters wrote on 947685067:
> On Tue, Jan 11, 2000 at 11:23:29PM -0500, Tim Peters wrote:
> 
> > Whereas people like Guido <wink> would not have considered writing anything
> > other than
> > 
> > >>> sum = 0
> > >>> for n, s in q:
> >         sum = sum + n
> 
> Aahh, yes. I dont know what I was thinking when I wrote it using map and
> reduce instead of a much more obvious for loop. My only excuse is that it
> was late yesterday night, and I'd just come back from an Amsterdam Perl
> Mongers meeting. Amusing evening, but it probably confused me into using the
> least obvious of all the ways to do it ;)

:-)

> > Seriously, I don't think I've ever seen a good use for reduce in Python --
> > although I've certainly used it <wink>.
> 
> People still care about speed.

People should hang around in comp.lang.c or even comp.lang.asm.i386 if
they care about speed.

> I know I sometimes do, even though I know
> intellectually that it isn't going to matter much in the end product. I
> still like to make it as speedy as possible, even if that entails using
> map/reduce or odd looking constructions like 'def spam(text, None=None)'. I
> suspect the use of map/reduce will drop when python is optimisable enough
> to make the for loop _feel_ faster than map(). Static typing and what not.

I never used reduce(), but I did use map(), filter() & lambda.

> On the other hand, oneliners will always feel or look faster to some people.

Sure:
l = filter(lambda x: x%3==0, range(100))
I _really_ read this one somewhere!

It's more complicated than range(0, 100, 3), but, er... ;-)

> I'm one of them, I must admit. Another thing is the geekiness and
> playfulness of oneliners. 'look, mom, only one line.' Maybe this is where
> list comprehensions come in ;)

Well, is most languages, *everything* is possible in one line. tr -d '\n'
does a lot in c...

> [ <snip> Guido saying lambda's suck (compared to the rest of python) ]

16:50:52:tty11:gerrit at stopcontact:~/cvs/ro/python/dist/src/Lib$ grep -w lambda *.py
Bastion.py:def Bastion(object, filter = lambda name: name[:1] != '_',
CGIHTTPServer.py:        nobody = 1 + max(map(lambda x: x[2], pwd.getpwall()))
asyncore.py:                    lambda x: string.join (x, '|'),
bdb.py:                 s = s + "<lambda>"
htmlentitydefs.py:    'Lambda': 'Λ',       # greek capital letter lambda, U+039B ISOgrk3
htmlentitydefs.py:    'lambda': 'λ',       # greek small letter lambda, U+03BB ISOgrk3
imaplib.py:             l = map(lambda x,j=j:'%s: "%s"' % (x[0], x[1][0] and j(x[1], '" "') or ''), l)
keyword.py:        'lambda',
mailcap.py:        entries = filter(lambda e, key=key: e.has_key(key), entries)
poplib.py:              digest = string.join(map(lambda x:'%02x'%ord(x), digest), '')
profile.py:                     t = reduce(lambda x,y: x+y, t, 0)
random.py:      # ('lambda' is a Python reserved word)
re.py:                repl = lambda m, repl=repl, expand=pcre_expand: expand(m, repl)
types.py:LambdaType = type(lambda: None)         # Same as FunctionType
xdrlib.py:        (up.unpack_uint,   (), lambda x: x == 9),
xdrlib.py:        (up.unpack_bool,   (), lambda x: not x),
xdrlib.py:        (up.unpack_bool,   (), lambda x: x),
xdrlib.py:        (up.unpack_uhyper, (), lambda x: x == 45L),
xdrlib.py:        (up.unpack_float,  (), lambda x: 1.89 < x < 1.91),
xdrlib.py:        (up.unpack_double, (), lambda x: 1.89 < x < 1.91),
xdrlib.py:        (up.unpack_string, (), lambda x: x == 'hello world'),
xdrlib.py:        (up.unpack_list,   (up.unpack_uint,), lambda x: x == range(5)),
xdrlib.py:         lambda x: x == ['what', 'is', 'hapnin', 'doctor']),

Only 15 real lambda's, in fact. It looks like Guido has written a very large
part of the library...


But suppose I have:
t = [('foo', 'bar'), ('foo_two', 'bar_two'), ('foo_three', 'bar_three')]
first_ones = map(lambda s: s[0], t)
last_ones = map(lambda s: s[1], t)

How would I do that without lambda's, if this code is in a method? Should
I nest two functions?

> > I suppose finally posting that will rekindle a few old flamewars!  Brrrr.
> 
> Weeellll, I never bothered much with flamewars (though I did see a spot of
> red when Tom Christiansen made his appearance last year) but I was always
> sceptical when I heard someone say such things. Surely lambda's are good in
> some cases ! But lately I've been looking at a lot of things differently and
> considering wether the good it brings in some cases justifies the pain it
> brings in other cases (this goes for much more than just lambda, python,
> computers or corporeality, though.)
> 
> So I hope potential flamers are willing to take a step back and do what I
> originally did. Start with thinking, "Guido Is Right", then consider, "Why
> is Guido right ? What is he saying, exactly ?" and ending up in argument
> with yourself. If you do end up thinking lambda's are guido's gift to sane
> coding, at least you'll be prepared for the flamewar, having heard all
> arguments.

lambda's are good, because otherwise, it wouldn't be possible to write
obfuscated code in Python... Can someone please rewrite the one-liner
mandelbrot without lambda's? And after that, without reduce()? And without
filter()? And map()? And apply()? And __import__()? And any builtins?
In c, you can write anything with only getchar() and putchar() ;-)

> It's important, though, once started, to finish this process. Dont get stuck
> at 'Guido Is Right', even if it's a very comfortable situation and nobody is
> going to notice in the end. The bureau of AFT might pull a Waco on Guido,
> after all :P

Guido Is Right. Linus Is Right. Eric Raymond Is Right.

Let's hope there'll never be a flamewar between Guido, Linus, Eric,
Bruce, ...

> Hi! I'm a .signature virus! copy me into your .signature file to help me spread!

Oh, help! My computer is infected! What should I do now?!

regards,
Gerrit.

-- 
Please correct any bad English you encounter in my mail!
-----BEGIN GEEK CODE BLOCK----- http://www.geekcode.com
Version: 3.12
GCS dpu s-:-- a14 C++++>$ UL++ P--- L+++ E--- W++ N o? K? w--- !O !M !V PS+ PE?
Y? PGP-- t- 5? X? R- tv- b+(++) DI D+ G++ !e !r !y
-----END GEEK CODE BLOCK-----




More information about the Python-list mailing list