Last active
October 11, 2015 01:28
-
-
Save cblp/3781224 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
| class RorMethod1: | |
| """ | |
| RorMethod1,2 classes act as functools.partial with method-like ror application. | |
| """ | |
| def __init__(self, function): | |
| """ function may be print or sorted """ | |
| self.function = function | |
| def __ror__(self, value): | |
| return self.function(value) | |
| def __call__(self, *args): | |
| return self.function(*args) | |
| class RorMethod2: | |
| def __init__(self, function): | |
| """ function may be map, filter or reduce """ | |
| self.function = function | |
| def __ror__(self, value): | |
| return self.function(self.user_function, value) | |
| def __call__(self, *args): | |
| if len(args) == 1: | |
| self.user_function = args[0] | |
| return self | |
| else: | |
| return self.function(*args) | |
| sorted = RorMethod1(sorted) | |
| map = RorMethod2(map) | |
| filter = RorMethod2(filter) | |
| reduce = RorMethod2(reduce) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment