feat: add join method to Sequence and refactor List toString to use it

This commit is contained in:
Bob Nystrom
2015-01-24 22:39:45 +00:00
9 changed files with 76 additions and 18 deletions
+15 -9
View File
@@ -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"