feat: abstract List's toString into generic join method on Sequence

Add `join` and `join(sep)` methods to the `Sequence` class, allowing any sequence to produce a concatenated string of its elements with an optional separator. Refactor `List.toString` to delegate to `join(", ")` with bracket wrapping, removing the manual iteration logic from List. Include new test files for List, Range, and String join behavior, plus runtime error tests for non-string separators.
This commit is contained in:
Gavin Schulz
2015-01-24 04:45:23 +00:00
parent c65b03ac32
commit 9e20b3935a
9 changed files with 76 additions and 18 deletions
+15 -9
View File
@@ -42,6 +42,20 @@ class Sequence {
return result
}
join { join("") }
join(sep) {
var first = true
var result = ""
for (element in this) {
if (!first) result = result + sep
first = false
result = result + element.toString
}
return result
}
}
class String is Sequence {}
@@ -54,15 +68,7 @@ class List is Sequence {
return other
}
toString {
var result = "["
for (i in 0...count) {
if (i > 0) result = result + ", "
result = result + this[i].toString
}
result = result + "]"
return result
}
toString { "[" + join(", ") + "]" }
+(other) {
var result = this[0..-1]