Created
July 31, 2009 19:41
-
-
Save kergoth/159392 to your computer and use it in GitHub Desktop.
stackiter
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
#!/usr/bin/env python | |
from collections import deque | |
def stackiter(iterable): | |
iterator = iter(iterable) | |
stack = deque() | |
while True: | |
if stack: | |
val = stack.pop() | |
else: | |
val = iterator.next() | |
newval = yield val | |
if newval: | |
stack.append(newval) | |
stack.append(val) | |
if __name__ == "__main__": | |
# prints 10, 20, 30, 40, 50, 60 | |
testiter = stackiter([10,20,50,60]) | |
for item in testiter: | |
if item == 20: | |
testiter.send(40) | |
testiter.send(30) | |
print(item) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment