summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2014-04-02 10:16:42 (GMT)
committerRaymond Hettinger <python@rcn.com>2014-04-02 10:16:42 (GMT)
commit31b26f637aa118c70157a3e11f58f12620345989 (patch)
treefd5e7e8d247c548bc2c170e819bbc0acd1084feb /Lib/test
parentf4284e45d037162835a5a5bc0bc23645b32da26c (diff)
downloadcpython-31b26f637aa118c70157a3e11f58f12620345989.zip
cpython-31b26f637aa118c70157a3e11f58f12620345989.tar.gz
cpython-31b26f637aa118c70157a3e11f58f12620345989.tar.bz2
Issue #18652: Add an itertools recipe for first_true()
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_itertools.py16
1 files changed, 16 insertions, 0 deletions
diff --git a/Lib/test/test_itertools.py b/Lib/test/test_itertools.py
index 21f1bde..3a580ad 100644
--- a/Lib/test/test_itertools.py
+++ b/Lib/test/test_itertools.py
@@ -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}