Is there a function that applies list of functions to a value?
Josh English
Joshua.R.English at gmail.com
Wed Aug 28 14:50:53 EDT 2013
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
More information about the Python-list
mailing list