-
-
Save sammko/e5eb59cee8b78a0a1f007010c231975d to your computer and use it in GitHub Desktop.
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
from itertools import product | |
def lazy_product(*iterables): | |
iters = [[i.__iter__(), [], False] for i in iterables] | |
def _p_next(i, il, _): | |
z = i.__next__() | |
il.append(z) | |
return z | |
s = True | |
while s: | |
s = False | |
for i in (x for x in iters if not x[2]): | |
try: | |
v = _p_next(*i) | |
yield from product(*(j[1] if j is not i else [v] for j in iters)) | |
s = True | |
except StopIteration: | |
i[2] = True | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment