Last active
March 16, 2017 13:56
-
-
Save icio/02f394788b8914832cb8ef6eb193d043 to your computer and use it in GitHub Desktop.
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
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 |
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
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