Created
June 8, 2012 20:56
-
-
Save sirpengi/2898083 to your computer and use it in GitHub Desktop.
blah
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
def one(iterable, condition=None): | |
"""True if only one value in the iterable evaluates to True.""" | |
if condition is None: | |
condition = bool | |
gen = (i for i in iterable if condition(i)) | |
try: | |
gen.next() | |
except StopIteration: | |
return False | |
try: | |
gen.next() | |
return False | |
except StopIteration: | |
return True | |
def first(iterable, condition=None, sentinel=None): | |
gen = (i for i in iterable if (condition is None or condition(i))) | |
try: | |
return gen.next() | |
except StopIteration: | |
return sentinel |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment