functional programming with map()

Quinn Dunkan quinn at vomit.ugcs.caltech.edu
Mon Feb 25 13:39:34 EST 2002


On Sun, 24 Feb 2002 19:34:34 -0800, David Eppstein <eppstein at ics.uci.edu> wrote:
>In article <a5cb54$27c2$1 at agate.berkeley.edu>,
> Daniel Yoo <dyoo at hkn.eecs.berkeley.edu> wrote:
>
>> : But what is the functional equvalent of:
>> 
>> : for x in items:
>> :     x.f()

There really is no functional equivalent of that, because it's not a functional
concept.  If your function doesn't have any side-effects, calling the function
only to throw away its value does nothing but suck up CPU time.

If you're calling the method for its side-effects, I'd write:

for x in items:
    x.f()

>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.



More information about the Python-list mailing list