Created
December 29, 2017 22:39
-
-
Save anonymous/23d4b1862c455d0debdc8ba04a425683 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