Skip to content

Instantly share code, notes, and snippets.

@icio icio/select.py
Last active Mar 16, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.