Last active
January 20, 2019 10:29
-
-
Save alex-lx/49dae4185720bbf8c4121515875e1baf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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