aboutsummaryrefslogtreecommitdiff
path: root/doc/docs
diff options
context:
space:
mode:
Diffstat (limited to 'doc/docs')
-rwxr-xr-xdoc/docs/doc/README.md38
-rwxr-xr-xdoc/docs/zh/doc/README.md38
2 files changed, 76 insertions, 0 deletions
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"
595</pre> 595</pre>
596</YueDisplay> 596</YueDisplay>
597 597
598You can also use the spread operator `...` to append all elements from one list to another:
599
600```moonscript
601tbA = [1, 2, 3]
602tbB = [4, 5, 6]
603tbA[] = ...tbB
604-- tbA is now [1, 2, 3, 4, 5, 6]
605```
606<YueDisplay>
607<pre>
608tbA = [1, 2, 3]
609tbB = [4, 5, 6]
610tbA[] = ...tbB
611-- tbA is now [1, 2, 3, 4, 5, 6]
612</pre>
613</YueDisplay>
614
598### Table Spreading 615### Table Spreading
599 616
600You can concatenate array tables or hash tables using spread operator `...` before expressions in table literals. 617You 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]
2465</pre> 2482</pre>
2466</YueDisplay> 2483</YueDisplay>
2467 2484
2485In list comprehensions, you can also use the spread operator `...` to flatten nested lists, achieving a flat map effect:
2486
2487```moonscript
2488data =
2489 a: {1,2,3}
2490 b: {4,5,6}
2491
2492flat = [...v for k,v in pairs data]
2493-- flat is now [1, 2, 3, 4, 5, 6]
2494```
2495<YueDisplay>
2496<pre>
2497data =
2498 a: {1,2,3}
2499 b: {4,5,6}
2500
2501flat = [...v for k,v in pairs data]
2502-- flat is now [1, 2, 3, 4, 5, 6]
2503</pre>
2504</YueDisplay>
2505
2468The for and when clauses can be chained as much as desired. The only requirement is that a comprehension has at least one for clause. 2506The for and when clauses can be chained as much as desired. The only requirement is that a comprehension has at least one for clause.
2469 2507
2470Using multiple for clauses is the same as using nested loops: 2508Using 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"
594</pre> 594</pre>
595</YueDisplay> 595</YueDisplay>
596 596
597你还可以使用展开操作符 `...` 来将一个列表中的所有元素追加到另一个列表中:
598
599```moonscript
600tbA = [1, 2, 3]
601tbB = [4, 5, 6]
602tbA[] = ...tbB
603-- tbA 现在为 [1, 2, 3, 4, 5, 6]
604```
605<YueDisplay>
606<pre>
607tbA = [1, 2, 3]
608tbB = [4, 5, 6]
609tbA[] = ...tbB
610-- tbA 现在为 [1, 2, 3, 4, 5, 6]
611</pre>
612</YueDisplay>
613
597### 表扩展 614### 表扩展
598 615
599你可以使用前置 `...` 操作符在Lua表中插入数组表或哈希表。 616你可以使用前置 `...` 操作符在Lua表中插入数组表或哈希表。
@@ -2425,6 +2442,27 @@ doubled = [item * 2 for item in *items]
2425</pre> 2442</pre>
2426</YueDisplay> 2443</YueDisplay>
2427 2444
2445在列表推导式中,你还可以使用展开操作符 `...` 来实现对列表嵌套层级进行扁平化的处理:
2446
2447```moonscript
2448data =
2449 a: {1,2,3}
2450 b: {4,5,6}
2451
2452flat = [...v for k,v in pairs data]
2453-- flat 现在为 [1, 2, 3, 4, 5, 6]
2454```
2455<YueDisplay>
2456<pre>
2457data =
2458 a: {1,2,3}
2459 b: {4,5,6}
2460
2461flat = [...v for k,v in pairs data]
2462-- flat 现在为 [1, 2, 3, 4, 5, 6]
2463</pre>
2464</YueDisplay>
2465
2428for和when子句可以根据需要进行链式操作。唯一的要求是推导式中至少要有一个for子句。 2466for和when子句可以根据需要进行链式操作。唯一的要求是推导式中至少要有一个for子句。
2429 2467
2430使用多个for子句与使用多重循环的效果相同: 2468使用多个for子句与使用多重循环的效果相同: