diff options
Diffstat (limited to 'doc/docs')
| -rwxr-xr-x | doc/docs/doc/README.md | 38 | ||||
| -rwxr-xr-x | doc/docs/zh/doc/README.md | 38 |
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 | ||
| 598 | You can also use the spread operator `...` to append all elements from one list to another: | ||
| 599 | |||
| 600 | ```moonscript | ||
| 601 | tbA = [1, 2, 3] | ||
| 602 | tbB = [4, 5, 6] | ||
| 603 | tbA[] = ...tbB | ||
| 604 | -- tbA is now [1, 2, 3, 4, 5, 6] | ||
| 605 | ``` | ||
| 606 | <YueDisplay> | ||
| 607 | <pre> | ||
| 608 | tbA = [1, 2, 3] | ||
| 609 | tbB = [4, 5, 6] | ||
| 610 | tbA[] = ...tbB | ||
| 611 | -- tbA is now [1, 2, 3, 4, 5, 6] | ||
| 612 | </pre> | ||
| 613 | </YueDisplay> | ||
| 614 | |||
| 598 | ### Table Spreading | 615 | ### Table Spreading |
| 599 | 616 | ||
| 600 | You can concatenate array tables or hash tables using spread operator `...` before expressions in table literals. | 617 | 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] | |||
| 2465 | </pre> | 2482 | </pre> |
| 2466 | </YueDisplay> | 2483 | </YueDisplay> |
| 2467 | 2484 | ||
| 2485 | In list comprehensions, you can also use the spread operator `...` to flatten nested lists, achieving a flat map effect: | ||
| 2486 | |||
| 2487 | ```moonscript | ||
| 2488 | data = | ||
| 2489 | a: {1,2,3} | ||
| 2490 | b: {4,5,6} | ||
| 2491 | |||
| 2492 | flat = [...v for k,v in pairs data] | ||
| 2493 | -- flat is now [1, 2, 3, 4, 5, 6] | ||
| 2494 | ``` | ||
| 2495 | <YueDisplay> | ||
| 2496 | <pre> | ||
| 2497 | data = | ||
| 2498 | a: {1,2,3} | ||
| 2499 | b: {4,5,6} | ||
| 2500 | |||
| 2501 | flat = [...v for k,v in pairs data] | ||
| 2502 | -- flat is now [1, 2, 3, 4, 5, 6] | ||
| 2503 | </pre> | ||
| 2504 | </YueDisplay> | ||
| 2505 | |||
| 2468 | 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. | 2506 | 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. |
| 2469 | 2507 | ||
| 2470 | Using multiple for clauses is the same as using nested loops: | 2508 | 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" | |||
| 594 | </pre> | 594 | </pre> |
| 595 | </YueDisplay> | 595 | </YueDisplay> |
| 596 | 596 | ||
| 597 | 你还可以使用展开操作符 `...` 来将一个列表中的所有元素追加到另一个列表中: | ||
| 598 | |||
| 599 | ```moonscript | ||
| 600 | tbA = [1, 2, 3] | ||
| 601 | tbB = [4, 5, 6] | ||
| 602 | tbA[] = ...tbB | ||
| 603 | -- tbA 现在为 [1, 2, 3, 4, 5, 6] | ||
| 604 | ``` | ||
| 605 | <YueDisplay> | ||
| 606 | <pre> | ||
| 607 | tbA = [1, 2, 3] | ||
| 608 | tbB = [4, 5, 6] | ||
| 609 | tbA[] = ...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 | ||
| 2448 | data = | ||
| 2449 | a: {1,2,3} | ||
| 2450 | b: {4,5,6} | ||
| 2451 | |||
| 2452 | flat = [...v for k,v in pairs data] | ||
| 2453 | -- flat 现在为 [1, 2, 3, 4, 5, 6] | ||
| 2454 | ``` | ||
| 2455 | <YueDisplay> | ||
| 2456 | <pre> | ||
| 2457 | data = | ||
| 2458 | a: {1,2,3} | ||
| 2459 | b: {4,5,6} | ||
| 2460 | |||
| 2461 | flat = [...v for k,v in pairs data] | ||
| 2462 | -- flat 现在为 [1, 2, 3, 4, 5, 6] | ||
| 2463 | </pre> | ||
| 2464 | </YueDisplay> | ||
| 2465 | |||
| 2428 | for和when子句可以根据需要进行链式操作。唯一的要求是推导式中至少要有一个for子句。 | 2466 | for和when子句可以根据需要进行链式操作。唯一的要求是推导式中至少要有一个for子句。 |
| 2429 | 2467 | ||
| 2430 | 使用多个for子句与使用多重循环的效果相同: | 2468 | 使用多个for子句与使用多重循环的效果相同: |
