Allow "*" on lists and strings to repeat them.
This is not implemented on Sequence because, at least for lists and strings, I think users expect an eager result. Multiplying a string should give you back a string, not a lazy sequence of repeated characters. This also mirrors "+" on strings and lists, which is eager. I like the idea of having a general guideline that operators are eager. Repetition is useful for arbitrary sequences, but for that maybe we should add a "repeat()" method.
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
System.print([1, 2, 3] * 0) // expect: []
|
||||
System.print([1, 2, 3] * 1) // expect: [1, 2, 3]
|
||||
System.print([1, 2, 3] * 4) // expect: [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
|
||||
|
||||
// Doesn't modify original list.
|
||||
var a = [1, 2, 3]
|
||||
a * 5
|
||||
System.print(a) // expect: [1, 2, 3]
|
||||
@@ -0,0 +1 @@
|
||||
[1, 2, 3] * -3 // expect runtime error: Count must be a non-negative integer.
|
||||
@@ -0,0 +1 @@
|
||||
[1, 2, 3] * 1.2 // expect runtime error: Count must be a non-negative integer.
|
||||
@@ -0,0 +1 @@
|
||||
[1, 2, 3] * "not num" // expect runtime error: Count must be a non-negative integer.
|
||||
@@ -1,18 +1,11 @@
|
||||
var a = [1, 2, 3]
|
||||
var b = [4, 5, 6]
|
||||
var c = a + b
|
||||
var d = a + (4..6)
|
||||
var e = a + []
|
||||
var f = [] + a
|
||||
var g = [] + []
|
||||
|
||||
System.print(a) // expect: [1, 2, 3]
|
||||
System.print(b) // expect: [4, 5, 6]
|
||||
System.print(c) // expect: [1, 2, 3, 4, 5, 6]
|
||||
System.print(d) // expect: [1, 2, 3, 4, 5, 6]
|
||||
System.print(e) // expect: [1, 2, 3]
|
||||
System.print(f) // expect: [1, 2, 3]
|
||||
System.print(g) // expect: []
|
||||
System.print([1, 2, 3] + [4, 5, 6]) // expect: [1, 2, 3, 4, 5, 6]
|
||||
System.print([1, 2, 3] + (4..6)) // expect: [1, 2, 3, 4, 5, 6]
|
||||
System.print([1, 2, 3] + "abc") // expect: [1, 2, 3, a, b, c]
|
||||
System.print([] + []) // expect: []
|
||||
System.print([1, 2] + []) // expect: [1, 2]
|
||||
System.print([] + [3, 4]) // expect: [3, 4]
|
||||
|
||||
// Doesn't modify original list.
|
||||
var a = [1, 2, 3]
|
||||
a * 5
|
||||
System.print(a) // expect: [1, 2, 3]
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
[1, 2, 3] + 4 // expect runtime error: Num does not implement 'iterate(_)'.
|
||||
Reference in New Issue
Block a user