diff options
Diffstat (limited to 'doc/docs/doc/README.md')
| -rwxr-xr-x | doc/docs/doc/README.md | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/doc/docs/doc/README.md b/doc/docs/doc/README.md index c0312f7..9f7d969 100755 --- a/doc/docs/doc/README.md +++ b/doc/docs/doc/README.md | |||
| @@ -586,6 +586,21 @@ merge = {...a, ...b} | |||
| 586 | </pre> | 586 | </pre> |
| 587 | </YueDisplay> | 587 | </YueDisplay> |
| 588 | 588 | ||
| 589 | ### Table Reversed Indexing | ||
| 590 | |||
| 591 | You can use the **#** operator to get the last elements of a table. | ||
| 592 | |||
| 593 | ```moonscript | ||
| 594 | print data.items[#] -- get the last element of a table | ||
| 595 | print data.items[#-1] -- get the second last element of a table | ||
| 596 | ``` | ||
| 597 | <YueDisplay> | ||
| 598 | <pre> | ||
| 599 | print data.items[#] -- get the last element of a table | ||
| 600 | print data.items[#-1] -- get the second last element of a table | ||
| 601 | </pre> | ||
| 602 | </YueDisplay> | ||
| 603 | |||
| 589 | ### Metatable | 604 | ### Metatable |
| 590 | 605 | ||
| 591 | The **<>** operator can be used as a shortcut for metatable manipulation. | 606 | The **<>** operator can be used as a shortcut for metatable manipulation. |
| @@ -1287,6 +1302,52 @@ You can use `_` as placeholder when doing a list destructuring: | |||
| 1287 | </pre> | 1302 | </pre> |
| 1288 | </YueDisplay> | 1303 | </YueDisplay> |
| 1289 | 1304 | ||
| 1305 | ### Range Destructuring | ||
| 1306 | |||
| 1307 | You can use the spread operator `...` in list destructuring to capture a range of values. This is useful when you want to extract specific elements from the beginning and end of a list while collecting the rest in between. | ||
| 1308 | |||
| 1309 | ```moonscript | ||
| 1310 | orders = ["first", "second", "third", "fourth", "last"] | ||
| 1311 | [first, ...bulk, last] = orders | ||
| 1312 | print first -- prints: first | ||
| 1313 | print bulk -- prints: {"second", "third", "fourth"} | ||
| 1314 | print last -- prints: last | ||
| 1315 | ``` | ||
| 1316 | <YueDisplay> | ||
| 1317 | <pre> | ||
| 1318 | orders = ["first", "second", "third", "fourth", "last"] | ||
| 1319 | [first, ...bulk, last] = orders | ||
| 1320 | print first -- prints: first | ||
| 1321 | print bulk -- prints: {"second", "third", "fourth"} | ||
| 1322 | print last -- prints: last | ||
| 1323 | </pre> | ||
| 1324 | </YueDisplay> | ||
| 1325 | |||
| 1326 | The spread operator can be used in different positions to capture different ranges, and you can use `_` as a placeholder for the values you don't want to capture: | ||
| 1327 | |||
| 1328 | ```moonscript | ||
| 1329 | -- Capture everything after first element | ||
| 1330 | [first, ...rest] = orders | ||
| 1331 | |||
| 1332 | -- Capture everything before last element | ||
| 1333 | [...start, last] = orders | ||
| 1334 | |||
| 1335 | -- Capture things except the middle elements | ||
| 1336 | [first, _..., last] = orders | ||
| 1337 | ``` | ||
| 1338 | <YueDisplay> | ||
| 1339 | <pre> | ||
| 1340 | -- Capture everything after first element | ||
| 1341 | [first, ...rest] = orders | ||
| 1342 | |||
| 1343 | -- Capture everything before last element | ||
| 1344 | [...start, last] = orders | ||
| 1345 | |||
| 1346 | -- Capture things except the middle elements | ||
| 1347 | [first, _..., last] = orders | ||
| 1348 | </pre> | ||
| 1349 | </YueDisplay> | ||
| 1350 | |||
| 1290 | ### Destructuring In Other Places | 1351 | ### Destructuring In Other Places |
| 1291 | 1352 | ||
| 1292 | Destructuring can also show up in places where an assignment implicitly takes place. An example of this is a for loop: | 1353 | Destructuring can also show up in places where an assignment implicitly takes place. An example of this is a for loop: |
| @@ -2344,6 +2405,45 @@ slice = [item for item in *items[,,2]] | |||
| 2344 | </pre> | 2405 | </pre> |
| 2345 | </YueDisplay> | 2406 | </YueDisplay> |
| 2346 | 2407 | ||
| 2408 | Both the minimum and maximum bounds can be negative, which means that the bounds are counted from the end of the table. | ||
| 2409 | |||
| 2410 | ```moonscript | ||
| 2411 | -- take the last 4 items | ||
| 2412 | slice = [item for item in *items[-4,-1]] | ||
| 2413 | ``` | ||
| 2414 | <YueDisplay> | ||
| 2415 | <pre> | ||
| 2416 | -- take the last 4 items | ||
| 2417 | slice = [item for item in *items[-4,-1]] | ||
| 2418 | </pre> | ||
| 2419 | </YueDisplay> | ||
| 2420 | |||
| 2421 | The step size can also be negative, which means that the items are taken in reverse order. | ||
| 2422 | |||
| 2423 | ```moonscript | ||
| 2424 | reverse_slice = [item for item in *items[-1,1,-1]] | ||
| 2425 | ``` | ||
| 2426 | <YueDisplay> | ||
| 2427 | <pre> | ||
| 2428 | reverse_slice = [item for item in *items[-1,1,-1]] | ||
| 2429 | </pre> | ||
| 2430 | </YueDisplay> | ||
| 2431 | |||
| 2432 | #### Slicing Expression | ||
| 2433 | |||
| 2434 | Slicing can also be used as an expression. This is useful for getting a sub-list of a table. | ||
| 2435 | |||
| 2436 | ```moonscript | ||
| 2437 | -- take the 2nd and 4th items as a new list | ||
| 2438 | sub_list = items[2, 4] | ||
| 2439 | ``` | ||
| 2440 | <YueDisplay> | ||
| 2441 | <pre> | ||
| 2442 | -- take the 2nd and 4th items as a new list | ||
| 2443 | sub_list = items[2, 4] | ||
| 2444 | </pre> | ||
| 2445 | </YueDisplay> | ||
| 2446 | |||
| 2347 | ## For Loop | 2447 | ## For Loop |
| 2348 | 2448 | ||
| 2349 | There are two for loop forms, just like in Lua. A numeric one and a generic one: | 2449 | There are two for loop forms, just like in Lua. A numeric one and a generic one: |
| @@ -2989,6 +3089,27 @@ switch tb | |||
| 2989 | </pre> | 3089 | </pre> |
| 2990 | </YueDisplay> | 3090 | </YueDisplay> |
| 2991 | 3091 | ||
| 3092 | Match against a list and capture a range of elements. | ||
| 3093 | |||
| 3094 | ```moonscript | ||
| 3095 | segments = ["admin", "users", "logs", "view"] | ||
| 3096 | switch segments | ||
| 3097 | when [...groups, resource, action] | ||
| 3098 | print "Group:", groups -- prints: {"admin", "users"} | ||
| 3099 | print "Resource:", resource -- prints: "logs" | ||
| 3100 | print "Action:", action -- prints: "view" | ||
| 3101 | ``` | ||
| 3102 | <YueDisplay> | ||
| 3103 | <pre> | ||
| 3104 | segments = ["admin", "users", "logs", "view"] | ||
| 3105 | switch segments | ||
| 3106 | when [...groups, resource, action] | ||
| 3107 | print "Group:", groups -- prints: {"admin", "users"} | ||
| 3108 | print "Resource:", resource -- prints: "logs" | ||
| 3109 | print "Action:", action -- prints: "view" | ||
| 3110 | </pre> | ||
| 3111 | </YueDisplay> | ||
| 3112 | |||
| 2992 | ## Object Oriented Programming | 3113 | ## Object Oriented Programming |
| 2993 | 3114 | ||
| 2994 | In these examples, the generated Lua code may appear overwhelming. It is best to focus on the meaning of the YueScript code at first, then look into the Lua code if you wish to know the implementation details. | 3115 | In these examples, the generated Lua code may appear overwhelming. It is best to focus on the meaning of the YueScript code at first, then look into the Lua code if you wish to know the implementation details. |
