"Collapsing" a list into a list of changes

Francis Girard francis.girard at free.fr
Mon Feb 7 13:07:10 EST 2005


Zut !

I'm very sorry that there is no good use case for the "reduce" function in 
Python, like Peter Otten pretends. That's an otherwise very useful tool for 
many use cases. At least on paper. 

Python documentation should say "There is no good use case for the reduce 
function in Python and we don't know why we bother you offering it."

Francis Girard

Le lundi 7 Février 2005 08:24, Peter Otten a écrit :
> Francis Girard wrote:
> > This is a prefect use case for the good old "reduce" function:
> >
> > --BEGIN SNAP
> >
> > a_lst = [None,0,0,1,1,1,2,2,3,3,3,2,2,2,4,4,4,5]
> >
> > def straightforward_collapse(lst):
> > return reduce(lambda v,e: v[-1]!=e and v+[e] or v, lst[1:], [lst[0]])
>
> reduce() magically increases the number of function calls by len(a_list).
> There will be no good use cases for reduce() until Python can inline
> functions.
>
> Apodictically Yours
> Peter




More information about the Python-list mailing list