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:
+15
-9
@@ -85,6 +85,20 @@ static const char* libSource =
|
||||
" return result\n"
|
||||
" }\n"
|
||||
"\n"
|
||||
" join { join(\"\") }\n"
|
||||
"\n"
|
||||
" join(sep) {\n"
|
||||
" var first = true\n"
|
||||
" var result = \"\"\n"
|
||||
"\n"
|
||||
" for (element in this) {\n"
|
||||
" if (!first) result = result + sep\n"
|
||||
" first = false\n"
|
||||
" result = result + element.toString\n"
|
||||
" }\n"
|
||||
"\n"
|
||||
" return result\n"
|
||||
" }\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"class String is Sequence {}\n"
|
||||
@@ -97,15 +111,7 @@ static const char* libSource =
|
||||
" return other\n"
|
||||
" }\n"
|
||||
"\n"
|
||||
" toString {\n"
|
||||
" var result = \"[\"\n"
|
||||
" for (i in 0...count) {\n"
|
||||
" if (i > 0) result = result + \", \"\n"
|
||||
" result = result + this[i].toString\n"
|
||||
" }\n"
|
||||
" result = result + \"]\"\n"
|
||||
" return result\n"
|
||||
" }\n"
|
||||
" toString { \"[\" + join(\", \") + \"]\" }\n"
|
||||
"\n"
|
||||
" +(other) {\n"
|
||||
" var result = this[0..-1]\n"
|
||||
|
||||
Reference in New Issue
Block a user