From 69f896ca6960419133bf9a5ecc231f7aa934ac56 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Fri, 14 Nov 2025 18:23:38 +0800 Subject: Fixed issue #223. --- doc/docs/doc/README.md | 38 ++++++++++++++++++++++++++++++++++++++ doc/docs/zh/doc/README.md | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) (limited to 'doc') diff --git a/doc/docs/doc/README.md b/doc/docs/doc/README.md index ad26be5..2a3047a 100755 --- a/doc/docs/doc/README.md +++ b/doc/docs/doc/README.md @@ -595,6 +595,23 @@ tab[] = "Value" +You can also use the spread operator `...` to append all elements from one list to another: + +```moonscript +tbA = [1, 2, 3] +tbB = [4, 5, 6] +tbA[] = ...tbB +-- tbA is now [1, 2, 3, 4, 5, 6] +``` + +
+tbA = [1, 2, 3]
+tbB = [4, 5, 6]
+tbA[] = ...tbB
+-- tbA is now [1, 2, 3, 4, 5, 6]
+
+
+ ### Table Spreading You can concatenate array tables or hash tables using spread operator `...` before expressions in table literals. @@ -2465,6 +2482,27 @@ doubled = [item * 2 for item in *items] +In list comprehensions, you can also use the spread operator `...` to flatten nested lists, achieving a flat map effect: + +```moonscript +data = + a: {1,2,3} + b: {4,5,6} + +flat = [...v for k,v in pairs data] +-- flat is now [1, 2, 3, 4, 5, 6] +``` + +
+data =
+  a: {1,2,3}
+  b: {4,5,6}
+
+flat = [...v for k,v in pairs data]
+-- flat is now [1, 2, 3, 4, 5, 6]
+
+
+ The for and when clauses can be chained as much as desired. The only requirement is that a comprehension has at least one for clause. Using multiple for clauses is the same as using nested loops: diff --git a/doc/docs/zh/doc/README.md b/doc/docs/zh/doc/README.md index 86796e9..a799a5b 100755 --- a/doc/docs/zh/doc/README.md +++ b/doc/docs/zh/doc/README.md @@ -594,6 +594,23 @@ tab[] = "Value" +你还可以使用展开操作符 `...` 来将一个列表中的所有元素追加到另一个列表中: + +```moonscript +tbA = [1, 2, 3] +tbB = [4, 5, 6] +tbA[] = ...tbB +-- tbA 现在为 [1, 2, 3, 4, 5, 6] +``` + +
+tbA = [1, 2, 3]
+tbB = [4, 5, 6]
+tbA[] = ...tbB
+-- tbA 现在为 [1, 2, 3, 4, 5, 6]
+
+
+ ### 表扩展 你可以使用前置 `...` 操作符在Lua表中插入数组表或哈希表。 @@ -2425,6 +2442,27 @@ doubled = [item * 2 for item in *items] +在列表推导式中,你还可以使用展开操作符 `...` 来实现对列表嵌套层级进行扁平化的处理: + +```moonscript +data = + a: {1,2,3} + b: {4,5,6} + +flat = [...v for k,v in pairs data] +-- flat 现在为 [1, 2, 3, 4, 5, 6] +``` + +
+data =
+  a: {1,2,3}
+  b: {4,5,6}
+
+flat = [...v for k,v in pairs data]
+-- flat 现在为 [1, 2, 3, 4, 5, 6]
+
+
+ for和when子句可以根据需要进行链式操作。唯一的要求是推导式中至少要有一个for子句。 使用多个for子句与使用多重循环的效果相同: -- cgit v1.2.3-55-g6feb