macpie.itertools.first_true#

macpie.itertools.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.

Parameters:
iterable
defaultobject, Default is False

Default value if no true value found.

predBoolean-valued function, Default is None

Notes

Taken from Itertools Recipes from https://docs.python.org/3/library/itertools.html

Examples

>>> mp.itertools.first_true([None, 0, 1])
1
>>> mp.itertools.first_true([None, 0, 1], pred=lambda x: x is not None)
0
>>> mp.itertools.first_true([None, 0, 1], pred=lambda x: x == 2)
False
>>> mp.itertools.first_true([None, 0, 1], pred=lambda x: x == 2, default='albert')
'albert'