diff options
Diffstat (limited to 'doc/docs/doc/README.md')
| -rwxr-xr-x | doc/docs/doc/README.md | 38 |
1 files changed, 38 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: |
