ytsm/YtManagerApp/utils/iterutils.py

13 lines
407 B
Python
Raw Normal View History

2018-10-13 20:01:45 +00:00
def first_true(*args, 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, args), default)