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
+23
View File
@@ -0,0 +1,23 @@
// Handle empty list.
IO.print([].join(",") == "") // expect: true
// Handle a simple list with an empty delimeter.
IO.print([1, 2, 3].join("")) // expect: 123
// Handle a simple list with no separator.
IO.print([1, 2, 3].join) // expect: 123
// Does not quote strings.
IO.print([1, "2", true].join(",")) // expect: 1,2,true
// Nested lists.
IO.print([1, [2, [3], 4], 5].join(",")) // expect: 1,[2, [3], 4],5
// Calls toString on elements.
class Foo {
toString { "Foo.toString" }
}
IO.print([1, new Foo, 2].join(", ")) // expect: 1, Foo.toString, 2
// TODO: Handle lists that contain themselves.
+1
View File
@@ -0,0 +1 @@
[1, 2, 3].join(2) // expect runtime error: Right operand must be a string.