Reduce tricks are nice, but I prefer clarity sometimes:
def double(x):
return x*2
def add3(x):
return x+3
def compose(*funcs):
for func in funcs:
if not callable(func):
raise ValueError('Must pass callable functions')
def inner(value):
for func in funcs:
value = func(value)
return value
return inner
add_then_double = compose(add3, double)
double_then_add = compose(double, add3)
print add_then_double(1) # prints 8
print double_then_add(1) # prints 5
--
http://mail.python.org/mailman/listinfo/python-list