Skip to content

Commit a575de6

Browse files
committed
Updated docs. [skip CI]
1 parent 0e72454 commit a575de6

2 files changed

Lines changed: 244 additions & 2 deletions

File tree

doc/docs/doc/README.md

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,21 @@ merge = {...a, ...b}
586586
</pre>
587587
</YueDisplay>
588588

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+
589604
### Metatable
590605

591606
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:
12871302
</pre>
12881303
</YueDisplay>
12891304

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+
12901351
### Destructuring In Other Places
12911352

12921353
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]]
23442405
</pre>
23452406
</YueDisplay>
23462407

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+
23472447
## For Loop
23482448

23492449
There are two for loop forms, just like in Lua. A numeric one and a generic one:
@@ -2989,6 +3089,27 @@ switch tb
29893089
</pre>
29903090
</YueDisplay>
29913091

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+
29923113
## Object Oriented Programming
29933114

29943115
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.

doc/docs/zh/doc/README.md

Lines changed: 123 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,21 @@ merge = {...a, ...b}
585585
</pre>
586586
</YueDisplay>
587587

588+
### 表反向索引
589+
590+
你可以使用 **#** 操作符来反向索引表中的元素。
591+
592+
```moonscript
593+
print data.items[#] -- 获取表的最后一个元素
594+
print data.items[#-1] -- 获取表的倒数第二个元素
595+
```
596+
<YueDisplay>
597+
<pre>
598+
print data.items[#] -- 获取表的最后一个元素
599+
print data.items[#-1] -- 获取表的倒数第二个元素
600+
</pre>
601+
</YueDisplay>
602+
588603
### 元表
589604

590605
**<>** 操作符可提供元表操作的快捷方式。
@@ -1285,9 +1300,55 @@ print first, second, color
12851300
</pre>
12861301
</YueDisplay>
12871302

1288-
### 在其它地方的解构
1303+
### 范围解构
1304+
1305+
你可以使用展开运算符 `...` 在列表解构中来捕获一个范围的值到子列表中。这在当你想要从列表的开头和结尾提取特定元素,同时收集中间的元素时非常有用。
1306+
1307+
```moonscript
1308+
orders = ["first", "second", "third", "fourth", "last"]
1309+
[first, ...bulk, last] = orders
1310+
print first -- 打印: first
1311+
print bulk -- 打印: {"second", "third", "fourth"}
1312+
print last -- 打印: last
1313+
```
1314+
<YueDisplay>
1315+
<pre>
1316+
orders = ["first", "second", "third", "fourth", "last"]
1317+
[first, ...bulk, last] = orders
1318+
print first -- 打印: first
1319+
print bulk -- 打印: {"second", "third", "fourth"}
1320+
print last -- 打印: last
1321+
</pre>
1322+
</YueDisplay>
1323+
1324+
展开运算符可以用在不同的位置来捕获不同的范围,并且你可以使用 `_` 作为占位符来表示你想跳过对应范围的捕获:
1325+
1326+
```moonscript
1327+
-- 捕获第一个元素之后的所有元素
1328+
[first, ...rest] = orders
1329+
1330+
-- 捕获最后一个元素之前的所有元素
1331+
[...start, last] = orders
1332+
1333+
-- 跳过中间的元素,只捕获第一个和最后一个元素
1334+
[first, _..., last] = orders
1335+
```
1336+
<YueDisplay>
1337+
<pre>
1338+
-- 捕获第一个元素之后的所有元素
1339+
[first, ...rest] = orders
1340+
1341+
-- 捕获最后一个元素之前的所有元素
1342+
[...start, last] = orders
1343+
1344+
-- 跳过中间的元素,只捕获第一个和最后一个元素
1345+
[first, _..., last] = orders
1346+
</pre>
1347+
</YueDisplay>
1348+
1349+
### 在其它地方的解构赋值
12891350

1290-
解构也可以出现在其它隐式进行赋值的地方。一个例子是用在for循环
1351+
解构赋值也可以出现在其它隐式进行赋值的地方。一个例子是用在for循环中
12911352

12921353
```moonscript
12931354
tuples = [
@@ -2305,6 +2366,45 @@ slice = [item for item in *items[,,2]]
23052366
</pre>
23062367
</YueDisplay>
23072368

2369+
最小和最大边界都可以是负数,使用负数意味着边界是从表的末尾开始计算的。
2370+
2371+
```moonscript
2372+
-- 取最后4个元素
2373+
slice = [item for item in *items[-4,-1]]
2374+
```
2375+
<YueDisplay>
2376+
<pre>
2377+
-- 取最后4个元素
2378+
slice = [item for item in *items[-4,-1]]
2379+
</pre>
2380+
</YueDisplay>
2381+
2382+
切片的步长也可以是负数,这意味着元素会以相反的顺序被取出。
2383+
2384+
```moonscript
2385+
reverse_slice = [item for item in *items[-1,1,-1]]
2386+
```
2387+
<YueDisplay>
2388+
<pre>
2389+
reverse_slice = [item for item in *items[-1,1,-1]]
2390+
</pre>
2391+
</YueDisplay>
2392+
2393+
#### 切片表达式
2394+
2395+
切片也可以作为表达式来使用。可以用于获取一个表包含的子列表。
2396+
2397+
```moonscript
2398+
-- 取第2和第4个元素作为新的列表
2399+
sub_list = items[2, 4]
2400+
```
2401+
<YueDisplay>
2402+
<pre>
2403+
-- 取第2和第4个元素作为新的列表
2404+
sub_list = items[2, 4]
2405+
</pre>
2406+
</YueDisplay>
2407+
23082408
## for 循环
23092409

23102410
Lua中有两种for循环形式,数字型和通用型:
@@ -2948,6 +3048,27 @@ switch tb
29483048
</pre>
29493049
</YueDisplay>
29503050

3051+
匹配一个列表并捕获特定范围内的元素。
3052+
3053+
```moonscript
3054+
segments = ["admin", "users", "logs", "view"]
3055+
switch segments
3056+
when [...groups, resource, action]
3057+
print "Group:", groups -- 打印: {"admin", "users"}
3058+
print "Resource:", resource -- 打印: "logs"
3059+
print "Action:", action -- 打印: "view"
3060+
```
3061+
<YueDisplay>
3062+
<pre>
3063+
segments = ["admin", "users", "logs", "view"]
3064+
switch segments
3065+
when [...groups, resource, action]
3066+
print "Group:", groups -- 打印: {"admin", "users"}
3067+
print "Resource:", resource -- 打印: "logs"
3068+
print "Action:", action -- 打印: "view"
3069+
</pre>
3070+
</YueDisplay>
3071+
29513072
## 面向对象编程
29523073

29533074
在以下的示例中,月之脚本生成的Lua代码可能看起来会很复杂。所以最好主要关注月之脚本代码层面的意义,然后如果你想知道关于面向对象功能的实现细节,再查看Lua代码。

0 commit comments

Comments
 (0)