[Tutor] Text processing and functional programming?

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Wed Aug 13 16:13:59 EDT 2003



On Tue, 12 Aug 2003, Clay Shirky wrote:

> My impression is that lambda and map/filter/reduce can often be replaced by
> list comprehensions. Is this correct?
>
> > ###
> > def indent(s):
> >   results = findall('(?m)^ *(?=\S)',s)
> >   minimum = len(results[0])
> >   for r in results:
> >       if len(r) < minimum:
> >           minimum = len(r)
> >   return minimum
> > ###
>
> This is *so* much more readable than the above.


Hi Clay,


Hmmm... Ok, fair enough.  How about this?

###
def indent(s):
    results = findall('(?m)^ *(?=\S)',s)
    lengths = map(len, results)
    return min(lengths)
###


I guess I'm trying to argue that we can be pragmatic about this and use
both imperative and functional techniques where they're appropriate.
Functional-style programming doesn't have to be ugly.


That being said, I agree that TPiP is a bit dense, perhaps needlessly so.
Perhaps we can send some feedback to David Mertz so that he can try
revising his code examples for readability's sake, without sacrificing too
much of its functional "purity".




More information about the Tutor mailing list