Skip to content

Commit bc543aa

Browse files
committed
more uses of yield from
1 parent 2f426a1 commit bc543aa

File tree

25 files changed

+60
-107
lines changed

25 files changed

+60
-107
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/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/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):

src/sage/games/sudoku.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -719,8 +719,7 @@ def backtrack(self):
719719
"""
720720
from .sudoku_backtrack import backtrack_all
721721
solutions = backtrack_all(self.n, self.puzzle)
722-
for soln in solutions:
723-
yield soln
722+
yield from solutions
724723

725724
def dlx(self, count_only=False):
726725
r"""

src/sage/graphs/bipartite_graph.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1701,8 +1701,7 @@ def rec(G):
17011701

17021702
# For each unlabeled matching, we yield all its possible labelings
17031703
for m in rec(G):
1704-
for pm in itertools.product(*[edges[frozenset(e)] for e in m]):
1705-
yield pm
1704+
yield from itertools.product(*[edges[frozenset(e)] for e in m])
17061705

17071706
def load_afile(self, fname):
17081707
r"""

src/sage/graphs/pq_trees.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -365,8 +365,7 @@ def __iter__(self):
365365
{2, 3}
366366
('P', [{2, 4}, {8, 2}, {9, 2}])
367367
"""
368-
for i in self._children:
369-
yield i
368+
yield from self._children
370369

371370
def number_of_children(self):
372371
r"""
@@ -795,13 +794,11 @@ def orderings(self):
795794
({2, 4}, {0, 8}, {1, 2}, {0, 5})
796795
({2, 4}, {0, 8}, {0, 5}, {1, 2})
797796
...
798-
799797
"""
800798
from itertools import permutations, product
801799
for p in permutations(self._children):
802-
for o in product(*[x.orderings() if isinstance(x, PQ) else [x]
803-
for x in p]):
804-
yield o
800+
yield from product(*[x.orderings() if isinstance(x, PQ) else [x]
801+
for x in p])
805802

806803

807804
class Q(PQ):
@@ -1124,8 +1121,7 @@ def orderings(self):
11241121
"""
11251122
if len(self._children) == 1:
11261123
c = self._children[0]
1127-
for o in (c.orderings() if isinstance(c, PQ) else [c]):
1128-
yield o
1124+
yield from (c.orderings() if isinstance(c, PQ) else [c])
11291125
else:
11301126
from itertools import product
11311127
for o in product(*[x.orderings() if isinstance(x, PQ) else [x]

0 commit comments

Comments
 (0)