Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions py/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,9 @@ func (l *List) M__mul__(other Object) (Object, error) {
if b, ok := convertToInt(other); ok {
m := len(l.Items)
n := int(b) * m
if n < 0 {
n = 0
}
newList := NewListSized(n)
for i := 0; i < n; i += m {
copy(newList.Items[i:i+m], l.Items)
Expand Down
6 changes: 6 additions & 0 deletions py/tests/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,10 @@
assert repr(a) == "['a', 'b', 'c', 'd', 'e', 'f']"
assertRaises(TypeError, lambda: [].append())

doc="mul"
a = [1, 2, 3]
assert a * 2 == [1, 2, 3, 1, 2, 3]
assert a * 0 == []
assert a * -1 == []

doc="finished"
6 changes: 6 additions & 0 deletions py/tests/tuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@
assert repr((1,(2,3),4)) == "(1, (2, 3), 4)"
assert repr(("1",(2.5,17,()))) == "('1', (2.5, 17, ()))"

doc="mul"
a = (1, 2, 3)
assert a * 2 == (1, 2, 3, 1, 2, 3)
assert a * 0 == ()
assert a * -1 == ()

doc="finished"
4 changes: 4 additions & 0 deletions py/tuple.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func (a Tuple) M__add__(other Object) (Object, error) {
copy(newTuple[len(b):], b)
return newTuple, nil
}

return NotImplemented, nil
}

Expand All @@ -131,6 +132,9 @@ func (l Tuple) M__mul__(other Object) (Object, error) {
if b, ok := convertToInt(other); ok {
m := len(l)
n := int(b) * m
if n < 0 {
n = 0
}
newTuple := make(Tuple, n)
for i := 0; i < n; i += m {
copy(newTuple[i:i+m], l)
Expand Down