Skip to content

Commit d665b16

Browse files
committed
Modify
1 parent fa7d947 commit d665b16

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/sage/interfaces/maxima_abstract.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1689,6 +1689,17 @@ def __getitem__(self, i):
16891689
# If you change the i+1 to i below, better change __iter__ as well.
16901690
return InterfaceElement.__getitem__(self, i+1)
16911691

1692+
def __iter_without_mutation(self):
1693+
"""
1694+
Returns an iterator for ``self``. It is assumed that ``self`` won't be
1695+
mutated during the iteration.
1696+
"""
1697+
N = len(self)
1698+
z = self
1699+
for _ in range(N):
1700+
yield z.first()
1701+
z = z.rest()
1702+
16921703
def __iter__(self):
16931704
"""
16941705
Return an iterator for ``self``.
@@ -1702,12 +1713,8 @@ def __iter__(self):
17021713
sage: [e._sage_() for e in L]
17031714
[0, x, 2*x^2, 3*x^3, 4*x^4, 5*x^5]
17041715
"""
1705-
# Maxima lists are linked lists with O(n) random access time
1706-
# hence we create a copy and pop elements from the front instead
1707-
# so that __iter__ has complexity as O(n) and not O(n^2)
1708-
copied_list = self.copylist()
1709-
for i in range(len(copied_list)):
1710-
yield copied_list.pop()
1716+
# NOTE: `self` should not be mutated during iteration
1717+
return self.__iter_without_mutation()
17111718

17121719
def subst(self, val):
17131720
"""

0 commit comments

Comments
 (0)