Skip to content

Instantly share code, notes, and snippets.

@sirpengi
Created June 8, 2012 20:56
Show Gist options
  • Save sirpengi/2898083 to your computer and use it in GitHub Desktop.
Save sirpengi/2898083 to your computer and use it in GitHub Desktop.
blah
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