Skip to content

Instantly share code, notes, and snippets.

@L3viathan
Created May 4, 2020 19:28
Show Gist options
  • Select an option

  • Save L3viathan/95a3e36628b1a8ab4bdf50a458e4c1ac to your computer and use it in GitHub Desktop.

Select an option

Save L3viathan/95a3e36628b1a8ab4bdf50a458e4c1ac to your computer and use it in GitHub Desktop.
Instead of break or continue, allow to do "redo". Kind-of.
def redoable(iterable):
do_redo = False
def redo():
nonlocal do_redo
do_redo = True
for item in iterable:
yield redo, item
while do_redo:
do_redo = False
yield redo, item
>>> import random
>>> for redo, i in redoable(range(10)):
... print(i)
... if random.random() > 0.8:
... redo()
...
0
1
2
3
4
5
5
5
6
6
7
8
9
>>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment