summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRaymond Hettinger <rhettinger@users.noreply.github.com>2024-03-04 15:51:29 (GMT)
committerGitHub <noreply@github.com>2024-03-04 15:51:29 (GMT)
commit15dc2979bc1b24269177f0e150495abb7f3eb546 (patch)
treeafc151c4672139fb7b29154823c340cec6878545
parent0dfa7ce346ac003475aa45d25c76b13081b81217 (diff)
downloadcpython-15dc2979bc1b24269177f0e150495abb7f3eb546.zip
cpython-15dc2979bc1b24269177f0e150495abb7f3eb546.tar.gz
cpython-15dc2979bc1b24269177f0e150495abb7f3eb546.tar.bz2
Consistently spell out *predicate* instead of *pred*. (gh-116308)
-rw-r--r--Doc/library/itertools.rst39
-rw-r--r--Modules/itertoolsmodule.c6
2 files changed, 19 insertions, 26 deletions
diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst
index 072fe02..2ee39fd 100644
--- a/Doc/library/itertools.rst
+++ b/Doc/library/itertools.rst
@@ -56,13 +56,13 @@ Iterator Arguments Results
:func:`chain` p, q, ... p0, p1, ... plast, q0, q1, ... ``chain('ABC', 'DEF') --> A B C D E F``
:func:`chain.from_iterable` iterable p0, p1, ... plast, q0, q1, ... ``chain.from_iterable(['ABC', 'DEF']) --> A B C D E F``
:func:`compress` data, selectors (d[0] if s[0]), (d[1] if s[1]), ... ``compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F``
-:func:`dropwhile` pred, seq seq[n], seq[n+1], starting when pred fails ``dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1``
-:func:`filterfalse` pred, seq elements of seq where pred(elem) is false ``filterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8``
+:func:`dropwhile` predicate, seq seq[n], seq[n+1], starting when predicate fails ``dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1``
+:func:`filterfalse` predicate, seq elements of seq where predicate(elem) fails ``filterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8``
:func:`groupby` iterable[, key] sub-iterators grouped by value of key(v)
:func:`islice` seq, [start,] stop [, step] elements from seq[start:stop:step] ``islice('ABCDEFG', 2, None) --> C D E F G``
:func:`pairwise` iterable (p[0], p[1]), (p[1], p[2]) ``pairwise('ABCDEFG') --> AB BC CD DE EF FG``
:func:`starmap` func, seq func(\*seq[0]), func(\*seq[1]), ... ``starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000``
-:func:`takewhile` pred, seq seq[0], seq[1], until pred fails ``takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4``
+:func:`takewhile` predicate, seq seq[0], seq[1], until predicate fails ``takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4``
:func:`tee` it, n it1, it2, ... itn splits one iterator into n
:func:`zip_longest` p, q, ... (p[0], q[0]), (p[1], q[1]), ... ``zip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-``
============================ ============================ ================================================= =============================================================
@@ -90,7 +90,7 @@ Examples Results
.. _itertools-functions:
-Itertool functions
+Itertool Functions
------------------
The following module functions all construct and return iterators. Some provide
@@ -859,27 +859,20 @@ which incur interpreter overhead.
"Returns the nth item or a default value."
return next(islice(iterable, n, None), default)
- def quantify(iterable, pred=bool):
+ def quantify(iterable, predicate=bool):
"Given a predicate that returns True or False, count the True results."
- return sum(map(pred, iterable))
+ return sum(map(predicate, iterable))
+
+ def first_true(iterable, default=False, predicate=None):
+ "Returns the first true value or the *default* if there is no true value."
+ # 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(predicate, iterable), default)
def all_equal(iterable, key=None):
"Returns True if all the elements are equal to each other."
return len(take(2, groupby(iterable, key))) <= 1
- 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)
-
def unique_everseen(iterable, key=None):
"List unique elements, preserving order. Remember all elements ever seen."
# unique_everseen('AAAABBBCCDAABBB') --> A B C D
@@ -964,14 +957,14 @@ which incur interpreter overhead.
num_active -= 1
nexts = cycle(islice(nexts, num_active))
- def partition(pred, iterable):
+ def partition(predicate, iterable):
"""Partition entries into false entries and true entries.
- If *pred* is slow, consider wrapping it with functools.lru_cache().
+ If *predicate* is slow, consider wrapping it with functools.lru_cache().
"""
# partition(is_odd, range(10)) --> 0 2 4 6 8 and 1 3 5 7 9
t1, t2 = tee(iterable)
- return filterfalse(pred, t1), filter(pred, t2)
+ return filterfalse(predicate, t1), filter(predicate, t2)
def subslices(seq):
"Return all contiguous non-empty subslices of a sequence."
@@ -1214,7 +1207,7 @@ The following recipes have a more mathematical flavor:
>>> quantify([True, False, False, True, True])
3
- >>> quantify(range(12), pred=lambda x: x%2==1)
+ >>> quantify(range(12), predicate=lambda x: x%2==1)
6
>>> a = [[1, 2, 3], [4, 5, 6]]
diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c
index 1647414..d377c2c 100644
--- a/Modules/itertoolsmodule.c
+++ b/Modules/itertoolsmodule.c
@@ -4623,15 +4623,15 @@ batched(p, n) --> [p0, p1, ..., p_n-1], [p_n, p_n+1, ..., p_2n-1], ...\n\
chain(p, q, ...) --> p0, p1, ... plast, q0, q1, ...\n\
chain.from_iterable([p, q, ...]) --> p0, p1, ... plast, q0, q1, ...\n\
compress(data, selectors) --> (d[0] if s[0]), (d[1] if s[1]), ...\n\
-dropwhile(pred, seq) --> seq[n], seq[n+1], starting when pred fails\n\
+dropwhile(predicate, seq) --> seq[n], seq[n+1], starting when predicate fails\n\
groupby(iterable[, keyfunc]) --> sub-iterators grouped by value of keyfunc(v)\n\
-filterfalse(pred, seq) --> elements of seq where pred(elem) is False\n\
+filterfalse(predicate, seq) --> elements of seq where predicate(elem) is False\n\
islice(seq, [start,] stop [, step]) --> elements from\n\
seq[start:stop:step]\n\
pairwise(s) --> (s[0],s[1]), (s[1],s[2]), (s[2], s[3]), ...\n\
starmap(fun, seq) --> fun(*seq[0]), fun(*seq[1]), ...\n\
tee(it, n=2) --> (it1, it2 , ... itn) splits one iterator into n\n\
-takewhile(pred, seq) --> seq[0], seq[1], until pred fails\n\
+takewhile(predicate, seq) --> seq[0], seq[1], until predicate fails\n\
zip_longest(p, q, ...) --> (p[0], q[0]), (p[1], q[1]), ...\n\
\n\
Combinatoric generators:\n\