Moving the recipes from the README into here for now:
-
If you want composing zero functions to be the identity function:
from functools import partial
def identity(x):
return x
icompose = partial(compose, identity)
-
To compose arguments in reverse order:
def rcompose(*functions):
return compose(*reversed(functions))
-
When you need composition to produce a normal function, for example on Python implementations which don't implement the descriptor protocol:
def fcompose(*functions):
composed = compose(*functions)
return lambda *args, **kwargs: composed.__call__(*args, **kwargs)
(The explicit .__call__ makes this recipe work even on Python implementations that don't support overloading the function call operator.)
-
Composition as a decorator, so that you can use @composed_with(f) on top of def g(...) to get the same result as g = compose(f, g).
from functools import partial
composed_with = partial(partial, sacompose)
(Using sacompose allows this decorator to handle async functions too.)
Moving the recipes from the README into here for now:
If you want composing zero functions to be the identity function:
To compose arguments in reverse order:
When you need composition to produce a normal function, for example on Python implementations which don't implement the descriptor protocol:
(The explicit
.__call__makes this recipe work even on Python implementations that don't support overloading the function call operator.)Composition as a decorator, so that you can use
@composed_with(f)on top ofdef g(...)to get the same result asg = compose(f, g).(Using
sacomposeallows this decorator to handleasyncfunctions too.)