Functional Python
Composing Functions
def compose2(g, f):
def h(*a, **kw):
result = wrap(g, *a, **kw)
return wrap(f, result)
h.__name__ = "%s -> %s" % (g.__name__, f.__name__)
h.__doc__ = f.__doc__
h.__parent__ = f
f.__parent__ = g
return h
def compose(*fs):
"""
Create a function from a set of functions, each output is the
input of the succeeding function.
Each function is submitted to wrap, which logs, caches, and handles errors.
The functions are called in the order they are listed, for example:
d = compose(a, b, c)
Is similar to:
c(b(a()))
This can be called with parameters to d:
d(42)
Which is similar to:
c(b(a(42)))
"""
return reduce(compose2, fs)Last updated