Skip to content

Instantly share code, notes, and snippets.

@alex-lx
Last active January 20, 2019 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alex-lx/49dae4185720bbf8c4121515875e1baf to your computer and use it in GitHub Desktop.
Save alex-lx/49dae4185720bbf8c4121515875e1baf to your computer and use it in GitHub Desktop.
# helper class
class C:
def __init__(self, val):
self.val = val
def pipe(self, func):
return C(func(self.val))
def result(self):
return self.val
__getitem__ = pipe
__call__ = result
# usage
# demo functions
def a(x): return x * 2
def b(x): return x + 1
def c(x): return x * x
# same as c(b(a(4)))
C(4).pipe(a).pipe(b).pipe(c).result()
# if you like operators
C(4)[a][b][c]()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment