Last active
July 24, 2018 15:07
-
-
Save Julien00859/5187072dbe907f80c6c1c67e016141e6 to your computer and use it in GitHub Desktop.
Python wrapper around builtin functions and itertools to chain functions from left to right
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
"""Wrapper around builtins and itertools to chain calls""" | |
from itertools import * | |
from functools import reduce | |
class Chain: | |
def __init__(self, data): | |
self._data = data | |
# Chain methods | |
def accumulate(self, func=None): | |
self._data = accumulate(self._data, func) | |
return self | |
def chain(self, *iterables): | |
self._data = chain(self._data, *iterables) | |
return self | |
def flatten(self): | |
self._data = chain(*self._data) | |
return self | |
def combination(self, r): | |
self._data = combination(self._data, r) | |
return self | |
def combination_with_replacement(self, r): | |
self._data = combination_with_replacement(self._data, r) | |
return self | |
def compress(self, selectors): | |
self._data = compress(self._data, selectors) | |
return self | |
def dropwhile(self, predicate): | |
self._data = dropwhile(predicate, self._data) | |
return self | |
def takewhile(self, predicate): | |
self._data = takewhile(predicate, self._data) | |
return self | |
def product(self, r=1): | |
self._data = product(self._data, r) | |
return self | |
def permutations(self, r=None): | |
self._data = permutations(self._data, r) | |
return self | |
def filter(self, func=None): | |
self._data = filter(func, self._data) | |
return self | |
def filterfalse(self, func=None): | |
self._data = filterfalse(func, self._data) | |
return self | |
def groupby(self, keyfunc=None): | |
self._data = groupby(self._data, keyfunc) | |
return self | |
def starmap(self, func): | |
self._data = starmap(func, self._data) | |
return self | |
def islice(self, *args): | |
self._data = islice(self._data, *args) | |
return self | |
def map(self, func, *args): | |
self._data = map(func, self._data, *args) | |
return self | |
def sort(self, key=None, reverse=False): | |
self._data = sorted(self._data, key=key, reverse=reverse) | |
return self | |
def reverse(self): | |
self._data = reversed(self.list()) | |
return self | |
def enumerate(self, start=0): | |
self._data = enumerate(self._data, start) | |
return self | |
def zip(self, *iterables): | |
self._data = zip(self._data, *iterables) | |
return self | |
def zipself(self): | |
self._data = zip(*self._data) | |
return self | |
# Output methods | |
def list(self): | |
return list(self._data) | |
def next(self): | |
return next(self._data) | |
def all(self): | |
return all(self._data) | |
def any(self): | |
return any(self._data) | |
def len(self): | |
return len(self._data) | |
def min(self, **kwargs): | |
return min(self._data, **kwargs) | |
def max(self, **kwargs): | |
return max(self._data, **kwargs) | |
def sum(self, start=0): | |
return sum(self._data, start) | |
def cycle(self): | |
return cycle(self._data) | |
def reduce(self, func, *args): | |
return reduce(func, self._data, *args) | |
def __iter__(self): | |
return self._data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment