Skip to content

Instantly share code, notes, and snippets.

@icio
Last active March 16, 2017 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save icio/02f394788b8914832cb8ef6eb193d043 to your computer and use it in GitHub Desktop.
Save icio/02f394788b8914832cb8ef6eb193d043 to your computer and use it in GitHub Desktop.
from Queue import Queue
def select(source, selector, unselected, sentinel):
for item in source:
if selector(item):
yield item
else:
unselected.put(item)
unselected.put(sentinel)
unselected, sentinel = Queue(), None
selected = select(
source=(hex(n) for n in xrange(40)),
selector=lambda h: 'a' not in h,
unselected=unselected,
sentinel=sentinel,
)
for h in selected:
print 'Selected:', h
for h in iter(unselected.get, sentinel):
print 'Unselected:', h
from Queue import Queue
unselected = Queue()
selected = (
item
for item in (hex(n) for n in xrange(40))
if ('a' not in item) or unselected.put(item)
)
for h in selected:
print 'Selected:', h
sentinel = None
unselected.put(sentinel)
for h in iter(unselected.get, sentinel):
print 'Unselected:', h
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment