A function that scans an iterator and returns the first value that matches the condition (Corresponding to Javascript or Ruby find)
See the official Python itertools recipe http://docs.python.jp/3/library/itertools.html
python
def first_true(iterable, default=False, pred=None):
"""Returns the first true value in the iterable.
If no true value is found, returns *default*
If *pred* is not None, returns the first item
for which pred(item) is true.
"""
# first_true([a,b,c], x) --> a or b or c or x
# first_true([a,b], x, f) --> a if f(a) else b if f(b) else x
return next(filter(pred, iterable), default)
Recommended Posts