Skip to content

Instantly share code, notes, and snippets.

@sielicki
Created March 30, 2021 02:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sielicki/3f57ec402d6c523a85ad710394318aeb to your computer and use it in GitHub Desktop.
Save sielicki/3f57ec402d6c523a85ad710394318aeb to your computer and use it in GitHub Desktop.
import collections
def lazy_categorize(iterable, predicate):
a, b = collections.deque(), collections.deque()
empty = False
def _gen_generator(t=True):
d = a if t else b
d_inv = b if t else a
def g():
nonlocal empty
while not empty:
if len(d) > 0:
yield d.popleft()
else:
try:
e = next(iterable)
p = predicate(e)
if not t ^ p:
yield e
else:
d_inv.append(e)
except StopIteration:
empty = True
return g()
return (_gen_generator(True), _gen_generator(False))
if __name__ == '__main__':
r = iter(range(10000))
t, f = lazy_categorize(r, lambda x: x % 2 == 0)
for _ in range(25):
print("evens ", next(t))
for _ in range(100):
print("odds ", next(f))
for _ in range(100):
print("range at ", next(r))
for _ in range(25):
print("evens ", next(t))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment