map/filter/reduce/lambda opinions and background unscientificmini-survey

Carl Banks invalidemail at aerojockey.com
Mon Jul 4 05:23:51 EDT 2005


Christopher Subich wrote:
> Carl Banks wrote:
> >
> > Christopher Subich wrote:
> >>I've heard this said a couple times now -- how can listcomps not
> >>completely replace map and filter?
> > If you're doing heavy functional programming, listcomps are
> > tremendously unwieldy compared to map et al.
>
> Interesting; could you post an example of this?  Whenever I try to think
> of that, I come up with unwieldly syntax for the functional case.  In
> purely functional code the results of map/filter/etc would probably be
> directly used as arguments to other functions, which might make the
> calls longer than I'd consider pretty.  This is especially true with
> lots of lambda-ing to declare temporary expressions.

I suspect you're misunderstanding what I mean by heavily functional.

You appear to see maps and listcomps merely as a shortcut for a for
loop.  You're comparing the map shortcut and the listcomp shortcut and
seeing which one's less verbose.  In a mostly procedural program which
uses functional constructs in isolation, listcomps are going to win
most of those battles.

Heavily functional programming is a different mindset altogether.  In
heavily functional programming, things like maps and filters and
function applications are actually what you're thinking about.  map
isn't an indirect way to do a for loop; it's a direct way to do a map.

When your mind is focused on "applying a function to each member of
this list and returning a list of the results" as opposed to
"convenient shortcut to a for loop", map is going to be far superior to
a listcomp.  And when you're doing dozens and dozens of maps over a
large purely functional program, you don't want to write out a listcomp
every single time you want to do it.


-- 
CARL BANKS




More information about the Python-list mailing list