mirror of
https://github.com/python/cpython.git
synced 2025-11-03 03:22:27 +00:00
Issue #18652: Add an itertools recipe for first_true()
This commit is contained in:
parent
f4284e45d0
commit
31b26f637a
2 changed files with 29 additions and 0 deletions
|
|
@ -1998,6 +1998,19 @@ Samuele
|
|||
... # unique_justseen('ABBCcAD', str.lower) --> A B C A D
|
||||
... return map(next, map(itemgetter(1), groupby(iterable, key)))
|
||||
|
||||
>>> 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)
|
||||
|
||||
This is not part of the examples but it tests to make sure the definitions
|
||||
perform as purported.
|
||||
|
||||
|
|
@ -2075,6 +2088,9 @@ True
|
|||
>>> list(unique_justseen('ABBCcAD', str.lower))
|
||||
['A', 'B', 'C', 'A', 'D']
|
||||
|
||||
>>> first_true('ABC0DEF1', '9', str.isdigit)
|
||||
'0'
|
||||
|
||||
"""
|
||||
|
||||
__test__ = {'libreftest' : libreftest}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue