
Guido on the fate of Lambda, map() and filter(), and reduce() in Python 3000 - smanek
http://www.artima.com/weblogs/viewpost.jsp?thread=98196
======
pg
"filter(P, S) is almost always written clearer as [x for x in S if P(x)]"

Is this widely believed by Python hackers?

~~~
etal
If P is a built-in function taking a single argument, or one that's already
been defined, no. That's often not the case, though, and using a simple lambda
is annoying and usually less efficient the rest of the time.

For example,

    
    
        filter(isfile, os.listdir(some_dir))
    

is cleaner than:

    
    
        (f for f in os.listdir(some_dir) if isfile(f))
    

But

    
    
        filter(lambda f: f.endswith('.py'), os.listdir(some_dir))
    

is worse than:

    
    
        (f for f in os.listdir(some_dir) if f.endswith('.py'))
    

Ruby's blocks are something to be jealous of, in this case. List
comprehensions and generator expressions have other merits, though.

~~~
eru
I wish there was an easy syntax (in Python) for not only map and filter - but
for something like reduce/foldr/foldl as well. You still have to write loops
to do this (or use reduce.)

~~~
etal
Me too. I think the introduction of the functional module will actually be
good, because there's less resistance to adding clever features for higher-
order functions in the standard library than there is in the core language (in
Python, at least, since it aims to be small and beginner-friendly). Look at
the itertools module to see how that turned out -- lots of useful tricks
borrowed from the ML family, and you can "buy it by the yard".

------
riobard
I think the problem of list comprehension is that it is not as flexible as
combination of map() and filter(). For example,

    
    
      [F(x) for x if P(x)] 
    

is equivalent to

    
    
      map(F, filter(P, S))
    

but what about the equivalence of

    
    
      filter(P, map(F, S))? 
    

Without using any map() and filter(), this is the solution came up to my mind

    
    
      [x for x in [F(y) for y in S] if P(x)]
    

Pretty ugly, huh?

~~~
bamsjakalaka
[F(x) for x in S if P(F(x))]

~~~
riobard
That means you have to do F(x) TWICE, which is sometimes ineffective (when
F(x) costs too much)

------
apathy
wait a minute, you can't just expect python primitives to vectorize? And
people live without any(), all(), etc?

Its been a long time since I primarily wrote python, and I guess R has made me
lazy, but seriously: WTF?

Maybe hold off on Py3K until this is done?

~~~
ivank
any() and all() are available in Python 2.5.

~~~
apathy
cool, I can't believe I haven't had occasion to use them yet.

thank you

