Skip to content

Instantly share code, notes, and snippets.

@cblp
Last active October 11, 2015 01:28
Show Gist options
  • Select an option

  • Save cblp/3781224 to your computer and use it in GitHub Desktop.

Select an option

Save cblp/3781224 to your computer and use it in GitHub Desktop.
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