Skip to content

Commit 202ccd7

Browse files
author
Release Manager
committed
gh-35733: more uses of yield from <!-- Please provide a concise, informative and self-explanatory title. --> <!-- Don't put issue numbers in the title. Put it in the Description below. --> <!-- For example, instead of "Fixes #12345", use "Add a new method to multiply two integers" --> ### 📚 Description a few more use cases for the cool syntax `yield from` <!-- Describe your changes here in detail. --> <!-- Why is this change required? What problem does it solve? --> <!-- If this PR resolves an open issue, please link to it here. For example "Fixes #12345". --> <!-- If your change requires a documentation PR, please link it appropriately. --> ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. It should be `[x]` not `[x ]`. --> - [x] The title is concise, informative, and self-explanatory. - [x] The description explains in detail what this PR is about. - [ ] I have linked a relevant issue or discussion. - [ ] I have created tests covering the changes. - [ ] I have updated the documentation accordingly. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on - #12345: short description why this is a dependency - #34567: ... --> <!-- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> URL: #35733 Reported by: Frédéric Chapoton Reviewer(s): Kwankyu Lee
2 parents aba52d4 + 6b3eb6f commit 202ccd7

32 files changed

+67
-121
lines changed

src/sage/combinat/interval_posets.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2146,31 +2146,26 @@ def add_relations(poset, n, m):
21462146

21472147
if poset.le(n, m):
21482148
# there is already a link n->m, so we go to the next n
2149-
for pos in add_relations(poset, n - 1, m):
2150-
yield pos
2149+
yield from add_relations(poset, n - 1, m)
21512150
elif poset.le(m, n):
21522151
# there is an inverse link m->n, we know we won't be able
21532152
# to create a link i->m with i<=n, so we go to the next m
2154-
for pos in add_relations(poset, m, m + 1):
2155-
yield pos
2153+
yield from add_relations(poset, m, m + 1)
21562154
else:
21572155
# there is no link n->m
21582156
# first option : we don't create the link and go to the next m
21592157
# (since the lack of a link n->m forbids any links i->m
21602158
# with i<n)
2161-
for pos in add_relations(poset, m, m + 1):
2162-
yield pos
2159+
yield from add_relations(poset, m, m + 1)
21632160
# second option : we create the link
21642161
# (this is allowed because links i->m already exist for all
21652162
# n<i<m, or else we wouldn't be here)
21662163
poset = TamariIntervalPoset(poset.size(), poset._cover_relations + ((n, m),))
21672164
yield poset
21682165
# and then, we go to the next n
2169-
for pos in add_relations(poset, n - 1, m):
2170-
yield pos
2166+
yield from add_relations(poset, n - 1, m)
21712167

2172-
for inter in add_relations(self, 1, 2):
2173-
yield inter
2168+
yield from add_relations(self, 1, 2)
21742169

21752170
def interval_cardinality(self) -> Integer:
21762171
r"""

src/sage/combinat/necklace.py

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,7 @@ def _ffc(content, equality=False):
295295
if not e[0]: # == 0
296296
dll.hide(0)
297297

298-
for x in _fast_fixed_content(a, e, 2, 1, k, r, 2, dll, equality=equality):
299-
yield x
298+
yield from _fast_fixed_content(a, e, 2, 1, k, r, 2, dll, equality=equality)
300299

301300

302301
def _fast_fixed_content(a, content, t, p, k, r, s, dll, equality=False):
@@ -347,13 +346,13 @@ def _fast_fixed_content(a, content, t, p, k, r, s, dll, equality=False):
347346
sp = t + 1
348347

349348
if j == a[t - p - 1]:
350-
for x in _fast_fixed_content(a[:], content, t + 1, p,
351-
k, r, sp, dll, equality=equality):
352-
yield x
349+
yield from _fast_fixed_content(a[:], content, t + 1, p,
350+
k, r, sp, dll,
351+
equality=equality)
353352
else:
354-
for x in _fast_fixed_content(a[:], content, t + 1, t,
355-
k, r, sp, dll, equality=equality):
356-
yield x
353+
yield from _fast_fixed_content(a[:], content, t + 1, t,
354+
k, r, sp, dll,
355+
equality=equality)
357356

358357
if not content[j]: # == 0
359358
dll.unhide(j)
@@ -392,8 +391,7 @@ def _lfc(content, equality=False):
392391
if not content[0]: # == 0
393392
dll.hide(0)
394393

395-
for z in _list_fixed_content(a, content, 2, 1, k, dll, equality=equality):
396-
yield z
394+
yield from _list_fixed_content(a, content, 2, 1, k, dll, equality=equality)
397395

398396

399397
def _list_fixed_content(a, content, t, p, k, dll, equality=False):
@@ -434,13 +432,11 @@ def _list_fixed_content(a, content, t, p, k, dll, equality=False):
434432
dll.hide(j)
435433

436434
if j == a[t - p - 1]:
437-
for z in _list_fixed_content(a[:], content[:], t + 1, p,
438-
k, dll, equality=equality):
439-
yield z
435+
yield from _list_fixed_content(a[:], content[:], t + 1, p,
436+
k, dll, equality=equality)
440437
else:
441-
for z in _list_fixed_content(a[:], content[:], t + 1, t,
442-
k, dll, equality=equality):
443-
yield z
438+
yield from _list_fixed_content(a[:], content[:], t + 1, t,
439+
k, dll, equality=equality)
444440

445441
if not content[j]: # == 0
446442
dll.unhide(j)
@@ -519,13 +515,11 @@ def _simple_fixed_content(a, content, t, p, k, equality=False):
519515
a[t - 1] = j
520516
content[j] -= 1
521517
if j == a[t - p - 1]:
522-
for z in _simple_fixed_content(a[:], content, t + 1, p,
523-
k, equality=equality):
524-
yield z
518+
yield from _simple_fixed_content(a[:], content, t + 1, p,
519+
k, equality=equality)
525520
else:
526-
for z in _simple_fixed_content(a[:], content, t + 1, t,
527-
k, equality=equality):
528-
yield z
521+
yield from _simple_fixed_content(a[:], content, t + 1, t,
522+
k, equality=equality)
529523
content[j] += 1
530524

531525

src/sage/combinat/non_decreasing_parking_function.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,8 +419,7 @@ def __iter__(self):
419419
[[], [1], [1, 1], [1, 2], [1, 1, 1], [1, 1, 2], [1, 1, 3], [1, 2, 2]]
420420
"""
421421
for n in NN:
422-
for pf in NonDecreasingParkingFunctions_n(n):
423-
yield pf
422+
yield from NonDecreasingParkingFunctions_n(n)
424423

425424
def graded_component(self, n):
426425
"""

src/sage/combinat/posets/hasse_diagram.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -889,8 +889,7 @@ def upper_covers_iterator(self, element):
889889
sage: list(H.upper_covers_iterator(7))
890890
[]
891891
"""
892-
for x in self.neighbor_out_iterator(element):
893-
yield x
892+
yield from self.neighbor_out_iterator(element)
894893

895894
def lower_covers_iterator(self, element):
896895
r"""
@@ -905,8 +904,7 @@ def lower_covers_iterator(self, element):
905904
sage: list(H.lower_covers_iterator(4))
906905
[1, 2]
907906
"""
908-
for x in self.neighbor_in_iterator(element):
909-
yield x
907+
yield from self.neighbor_in_iterator(element)
910908

911909
def cardinality(self):
912910
r"""

src/sage/combinat/species/functorial_composition_species.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ def _structures(self, structure_class, s):
7171
{{1, 2}*{3}, {1, 3}*{2}, {2, 3}*{1}}]
7272
"""
7373
gs = self._G.structures(s).list()
74-
for f in self._F.structures(gs):
75-
yield f
74+
yield from self._F.structures(gs)
7675

7776
def _isotypes(self, structure_class, s):
7877
"""

src/sage/combinat/subset.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,8 +1130,7 @@ def __iter__(self):
11301130
[1, 2, 2, 3]]
11311131
"""
11321132
for k in range(len(self._l) + 1):
1133-
for s in SubMultiset_sk(self._l, k):
1134-
yield s
1133+
yield from SubMultiset_sk(self._l, k)
11351134

11361135
def __call__(self, el):
11371136
r"""

src/sage/combinat/tiling.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,8 +1220,7 @@ def isometric_copies(self, box, orientation_preserving=True,
12201220
all_distinct_cano = self.canonical_isometric_copies(orientation_preserving,
12211221
mod_box_isometries)
12221222
for cano in all_distinct_cano:
1223-
for t in cano.translated_copies(box=box):
1224-
yield t
1223+
yield from cano.translated_copies(box=box)
12251224

12261225
def isometric_copies_intersection(self, box, orientation_preserving=True):
12271226
r"""

src/sage/combinat/words/finite_word.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1731,8 +1731,7 @@ def left_special_factors_iterator(self, n=None):
17311731
"""
17321732
if n is None:
17331733
for i in range(self.length()):
1734-
for w in self.left_special_factors_iterator(i):
1735-
yield w
1734+
yield from self.left_special_factors_iterator(i)
17361735
else:
17371736
left_extensions = defaultdict(set)
17381737
for w in self.factor_iterator(n+1):
@@ -1799,8 +1798,7 @@ def right_special_factors_iterator(self, n=None):
17991798
"""
18001799
if n is None:
18011800
for i in range(self.length()):
1802-
for w in self.right_special_factors_iterator(i):
1803-
yield w
1801+
yield from self.right_special_factors_iterator(i)
18041802
else:
18051803
right_extensions = defaultdict(set)
18061804
for w in self.factor_iterator(n+1):
@@ -1890,8 +1888,7 @@ def bispecial_factors_iterator(self, n=None):
18901888
"""
18911889
if n is None:
18921890
for i in range(self.length()):
1893-
for w in self.bispecial_factors_iterator(i):
1894-
yield w
1891+
yield from self.bispecial_factors_iterator(i)
18951892
else:
18961893
left_extensions = defaultdict(set)
18971894
right_extensions = defaultdict(set)

src/sage/combinat/yang_baxter_graph.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -665,8 +665,7 @@ def __iter__(self):
665665
sage: list(Y.__iter__())
666666
[(1, 0, 2, 1, 0), (1, 2, 0, 1, 0), (1, 2, 1, 0, 0), (2, 1, 0, 1, 0), (2, 1, 1, 0, 0)]
667667
"""
668-
for v in self._vertex_ordering:
669-
yield v
668+
yield from self._vertex_ordering
670669

671670
def _swap_operator(self, operator, u):
672671
r"""

src/sage/databases/oeis.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,9 +1481,8 @@ def __iter__(self):
14811481
sage: s = oeis._imaginary_sequence(ident='A999991', keywords='sign,full')
14821482
sage: for i in s: pass
14831483
"""
1484-
for x in self.first_terms():
1485-
yield x
1486-
if not self.is_full() is True:
1484+
yield from self.first_terms()
1485+
if self.is_full() is not True:
14871486
raise LookupError("future values not provided by OEIS")
14881487

14891488
def references(self):

0 commit comments

Comments
 (0)