feat: implement single-argument reduce with manual iteration and empty-sequence abort

Replace the previous slice-based single-argument reduce implementation with an explicit iterator loop that seeds from the first element. This avoids the out-of-bounds range error on single-element lists and adds a Fiber.abort for empty sequences. Update the test to verify correct behavior for single-element and empty sequences.
This commit is contained in:
Luchs
2015-01-16 08:24:18 +00:00
parent 6cc513f13c
commit fb14301967
3 changed files with 26 additions and 3 deletions
+12 -1
View File
@@ -72,7 +72,18 @@ static const char* libSource =
" return acc\n"
" }\n"
"\n"
" reduce(f) { this[1..-1].reduce(this[0], f) }\n"
" reduce(f) {\n"
" var iter = iterate(null)\n"
" if (!iter) Fiber.abort(\"Can't reduce an empty sequence.\")\n"
"\n"
" // Seed with the first element.\n"
" var result = iteratorValue(iter)\n"
" while (iter = iterate(iter)) {\n"
" result = f.call(result, iteratorValue(iter))\n"
" }\n"
"\n"
" return result\n"
" }\n"
"\n"
"}\n"
"\n"