first_true¶
-
sknano.core.itertools.
first_true
(iterable, default=False, pred=None)[source] [edit on github][source]¶ 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.
Parameters: Examples
>>> from sknano.core import first_true >>> first_true([(), None, '', 0, 1]) 1 >>> first_true([(), None, '', 0], 'donkey') 'donkey' >>> is_odd = lambda x: x % 2 != 0 >>> first_true([2, 4, 6, 8, 11], default='donkey', pred=is_odd) 11 >>> first_true([2, 4, 6, 8], default='donkey', pred=is_odd) 'donkey'