Last active
May 14, 2018 14:20
-
-
Save nicktimko/a85ac7c7d634421aeec02a8bee48bdbd to your computer and use it in GitHub Desktop.
python arbitrary binary operators
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
import functools | |
class Operator: | |
def __init__(self, func): | |
self.func = func | |
def __rlshift__(self, other): | |
return self.__class__(functools.partial(self.func, other)) | |
def __rshift__(self, other): | |
return self.func(other) | |
@Operator | |
def xor(a, b): | |
return bool(a) ^ bool(b) | |
assert False <<xor>> False == False | |
assert False <<xor>> True == True | |
assert True <<xor>> False == True | |
assert True <<xor>> True == False | |
@Operator | |
def tripledouble(a, b): | |
return a*3 + b*2 | |
'a' <<tripledouble>> 'b' # aaabb |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment