Skip to content

Instantly share code, notes, and snippets.

@kergoth
Created July 31, 2009 19:41
Show Gist options
  • Save kergoth/159392 to your computer and use it in GitHub Desktop.
Save kergoth/159392 to your computer and use it in GitHub Desktop.
stackiter
#!/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