Last active
October 27, 2024 08:35
-
-
Save internetimagery/edb5be0caabfc3ad31d6f141909d2c8c to your computer and use it in GitHub Desktop.
Apply functions happily
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 functools import partial | |
import builtins | |
class Emoji: | |
""" Execute functions with style """ | |
__empty = object() | |
def __init__(self, left=__empty, right=__empty): | |
self.__left = left | |
self.__right = right | |
def __run_left(self, func): | |
if self.__right is self.__empty: | |
return self.__class__(left=func) | |
return func(self.__right) | |
def __run_right(self, arg): | |
if self.__left is self.__empty: | |
return self.__class__(right=arg) | |
return self.__left(arg) | |
def __rxor__(self, other): | |
return self.__run_left(other) | |
def __xor__(self, other): | |
return self.__run_right(other) | |
def __sub__(self, other): | |
return self.__run_right(other) | |
def __rsub__(self, other): | |
return self.__run_left(other) | |
def __mul__(self, other): | |
return self.__run_right(other) | |
def __rmul__(self, other): | |
return self.__run_left(other) | |
def __gt__(self, other): | |
return self.__lt__(other) | |
def __lt__(self, other): | |
if self.__left is self.__empty and callable(other): | |
return self.__run_left(other) | |
elif self.__right is self.__empty: | |
return self.__run_right(other) | |
return self.__run_left(other) | |
builtins._ = Emoji() | |
b = 1 | |
d = bool | |
# Now have fun! | |
assert (d ^_^ b) is True | |
assert (d ^_- b) is True | |
assert (d -_* b) is True | |
assert (d -_< b) is True | |
#assert (d >_< b) is True | |
#assert (d >_> b) is True | |
assert (d >_- b) is True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment