Skip to content

Instantly share code, notes, and snippets.

@piroyoung
Last active October 27, 2017 00:33
Show Gist options
  • Save piroyoung/97a713f487ef308587789acc5db8b388 to your computer and use it in GitHub Desktop.
Save piroyoung/97a713f487ef308587789acc5db8b388 to your computer and use it in GitHub Desktop.
from typing import List, Callable, TypeVar
from functools import reduce, lru_cache
T = TypeVar('T')
class MyCollection(List[T]):
def reduce(self, f: Callable[[T, T], T]):
return reduce(f, self)
# example usage
obj = MyCollection(range(5))
obj.reduce(lambda x, y: x + y)
# example usage
obj = MyCollection('hello')
obj.reduce(lambda x, y: x + '-' + y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment