Created
May 4, 2020 19:28
-
-
Save L3viathan/95a3e36628b1a8ab4bdf50a458e4c1ac to your computer and use it in GitHub Desktop.
Instead of break or continue, allow to do "redo". Kind-of.
This file contains hidden or 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
| 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 |
This file contains hidden or 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
| >>> 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