Created
November 13, 2016 22:02
-
-
Save fredcallaway/d8cd8192464d7ea839832e1a86c3f3f3 to your computer and use it in GitHub Desktop.
This file contains 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
from toolz import pipe | |
class Blank(object): | |
"""Forgive me Lord, for I have sinned. | |
Inspired by implicit partial application in Coconut: | |
http://coconut.readthedocs.io/en/master/DOCS.html#implicit-partial-application | |
""" | |
def __getattr__(self, attr): | |
def voodoo(*args): | |
if len(args) == 1 and hasattr(args[0], attr): | |
return getattr(args[0], attr) | |
else: | |
return lambda *more_args: voodoo(*more_args)(*args) | |
return voodoo | |
class Object(object): | |
"""Just an Object.""" | |
def __init__(self, attr): | |
super(Object, self).__init__() | |
self.attr = attr | |
def attr_plus(self, val): | |
return self.attr + val | |
obj = Object(Object(1)) | |
_ = Blank() | |
print( | |
# These are equivalent. | |
# Object(Object(1)) -> Object(1) -> 11 | |
pipe(obj, lambda x: x.attr, lambda x: x.attr_plus(10)), | |
pipe(obj, _.attr, _.attr_plus(10)), | |
sep='\n' | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment