[Tutor] using the reduce function

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Fri Jan 23 16:46:58 EST 2004



On Fri, 23 Jan 2004, Karl [iso-8859-1] Pfl=E4sterer wrote:

> On 23 Jan 2004, don arnold <- darnold02 at sprynet.com wrote:
>
> > I may be off-base here (since I've never used reduce() before), but can=
't
> > you just do this:
>
> >>>> reduce(lambda x,y: max(x,len(y)),['123456','23','1234567','1'],0)
>
> Sure you can.  There are multiple solutions possible; which one you
> choose may be a matter of taste and simply the fact that in that moment
> I didn't thouhgt of Python having `max'.  So I wrote it myself.


Hi Karl,


Not only can max take in two arguments, but it can also take in a list as
input:


###
>>> max([3, 5, 42, 19, 255, 91])
255
###


If we aren't forced to use reduce(), then Chris's max-string-length
problem ends up being a concise and pretty combination of the len(),
map(), and max() functions.  Chris, if you know already about map(), try
it out.


Functional programming is nice because of its emphasis on powerful
function composition.



Hope this helps!




More information about the Tutor mailing list