functional programming with map()

David Eppstein eppstein at ics.uci.edu
Mon Feb 25 14:13:01 EST 2002


In article <slrna7l176.3al.quinn at vomit.ugcs.caltech.edu>,
 quinn at vomit.ugcs.caltech.edu (Quinn Dunkan) wrote:

> >I'd prefer
> >[x.f() for x in items]
> >
> >It's not functional syntax, but so what?
> 
> Sure it is.  It's an expression, works best when x.f() has no 
> side-effects, and provides nicer syntax for mapping and filtering, two 
> popular functional concepts.  It's also borrowed from haskell, which, 
> although functional, has a lot of syntax sugar that turns into function 
> application.

To me it's like the difference between using coordinates or avoiding them 
in linear algebra.  A true functional syntax, to me, would avoid referring 
to the individual items in the list and just combine the list with a 
function, so "map(f, items)" is purer to me than "[f(x) for x in items]", 
because it avoids using any x's.  Which in turn is purer than
    L = []
    for x in items:
        L.append(f(x))

But, as I said, for the x.f() case, I prefer the list-comprehension syntax, 
because simplicity and ease of understanding are more important than purity.
-- 
David Eppstein       UC Irvine Dept. of Information & Computer Science
eppstein at ics.uci.edu http://www.ics.uci.edu/~eppstein/



More information about the Python-list mailing list