Is this a good use for lambda

Steven Bethard steven.bethard at gmail.com
Tue Dec 21 05:50:21 EST 2004


Nick Coghlan wrote:
> def compose(list_of_functions):
>   application_order = reversed(list_of_functions)
>   def composed(x):
>     for f in application_order:
>       x = f(x)
>     return x
>   return composed

reversed returns an iterator to the list in reverse order, not a copy of 
the list:

 >>> lst = range(10)
 >>> riter = reversed(lst)
 >>> list(riter)
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
 >>> list(riter)
[]

so you either need to call reversed each time in 'composed' or copy the 
list and call reverse.

Steve



More information about the Python-list mailing list