Skip to content

Instantly share code, notes, and snippets.

@okeyokoro
Forked from kyle-eshares/conduit.py
Created February 7, 2020 01:30
Show Gist options
  • Save okeyokoro/a0d2b2aa4eaf816a8bae13d31ce672e5 to your computer and use it in GitHub Desktop.
Save okeyokoro/a0d2b2aa4eaf816a8bae13d31ce672e5 to your computer and use it in GitHub Desktop.
class conduit(object):
def __init__(self, iterator):
self.iterator = iterator
def filter(self, predicate):
return conduit(itertools.ifilter(predicate, self.iterator))
def map(self, func):
return conduit(itertools.imap(func, self.iterator))
def sort(self, key):
# sort has to evaluate the iterator
return conduit(sorted(self.iterator, key=key))
def __iter__(self):
return iter(self.iterator)
def to_list(self):
return list(self)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment