Skip to content

Instantly share code, notes, and snippets.

@bofm

bofm/compose.py

Last active Jun 12, 2020
Embed
What would you like to do?
Python compose picklable
class compose:
__slots__ = ('f', 'fs')
def __init__(self, *fs):
self.f, *self.fs = reversed(fs)
def __call__(self, *args, **kwargs):
result = self.f(*args, **kwargs)
for f in self.fs:
result = f(result)
return result
def _compose(self, f):
return compose(self, f)
__mul__ = _compose
__lshift__ = __call__
def id_(x):
return x
C = compose(id_)
# >>> compose(str.upper, ','.join, str.split)(' ab cd xy z ')
# 'AB,CD,XY,Z'
# >>> C * str.upper * ','.join * str.split << ' ab cd xy z '
# 'AB,CD,XY,Z'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment