diff --git a/builtin/core.wren b/builtin/core.wren index 56389e22..0906afa0 100644 --- a/builtin/core.wren +++ b/builtin/core.wren @@ -10,17 +10,13 @@ class List { } + that { - var newList = [] - if (this.count > 0) { - for (element in this) { - newList.add(element) - } + var result = [] + for (element in this) { + result.add(element) } - if (that is Range || that.count > 0) { - for (element in that) { - newList.add(element) - } + for (element in that) { + result.add(element) } - return newList + return result } } diff --git a/src/wren_core.c b/src/wren_core.c index 0f87f251..7081e30b 100644 --- a/src/wren_core.c +++ b/src/wren_core.c @@ -53,18 +53,14 @@ static const char* libSource = " }\n" "\n" " + that {\n" -" var newList = []\n" -" if (this.count > 0) {\n" -" for (element in this) {\n" -" newList.add(element)\n" -" }\n" +" var result = []\n" +" for (element in this) {\n" +" result.add(element)\n" " }\n" -" if (that is Range || that.count > 0) {\n" -" for (element in that) {\n" -" newList.add(element)\n" -" }\n" +" for (element in that) {\n" +" result.add(element)\n" " }\n" -" return newList\n" +" return result\n" " }\n" "}\n"; @@ -336,12 +332,17 @@ DEF_NATIVE(list_insert) DEF_NATIVE(list_iterate) { + ObjList* list = AS_LIST(args[0]); + // If we're starting the iteration, return the first index. - if (IS_NULL(args[1])) RETURN_NUM(0); + if (IS_NULL(args[1])) + { + if (list->count == 0) RETURN_FALSE; + RETURN_NUM(0); + } if (!validateInt(vm, args, 1, "Iterator")) return PRIM_ERROR; - ObjList* list = AS_LIST(args[0]); int index = (int)AS_NUM(args[1]); // Stop if we're out of bounds. diff --git a/test/list/iterate.wren b/test/list/iterate.wren index 8c9e3c61..8470229d 100644 --- a/test/list/iterate.wren +++ b/test/list/iterate.wren @@ -5,3 +5,6 @@ IO.print(a.iterate(1)) // expect: 2 IO.print(a.iterate(2)) // expect: 3 IO.print(a.iterate(3)) // expect: false IO.print(a.iterate(-1)) // expect: false + +// Nothing to iterate in an empty list. +IO.print([].iterate(null)) // expect: false diff --git a/test/list/concat.wren b/test/list/plus.wren similarity index 84% rename from test/list/concat.wren rename to test/list/plus.wren index 838e97e8..d0dda410 100644 --- a/test/list/concat.wren +++ b/test/list/plus.wren @@ -13,3 +13,6 @@ IO.print(d) // expect: [1, 2, 3, 4, 5, 6] IO.print(e) // expect: [1, 2, 3] IO.print(f) // expect: [1, 2, 3] IO.print(g) // expect: [] + +// Doesn't modify original list. +IO.print(a) // expect: [1, 2, 3]