aboutsummaryrefslogtreecommitdiff
path: root/doc
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2023-10-31 16:06:05 +0800
committerLi Jin <dragon-fly@qq.com>2023-10-31 16:06:18 +0800
commit2dc090daf42d9449b2c63e7167084092d0bd13ac (patch)
tree89ce76c2f25ab5d6ec020ad91b91efe010ddcbce /doc
parent630fef5a40d2f7a221b9c3752462c8780eb17af4 (diff)
downloadyuescript-2dc090daf42d9449b2c63e7167084092d0bd13ac.tar.gz
yuescript-2dc090daf42d9449b2c63e7167084092d0bd13ac.tar.bz2
yuescript-2dc090daf42d9449b2c63e7167084092d0bd13ac.zip
fix doc. add busted to testing work.
Diffstat (limited to 'doc')
-rwxr-xr-xdoc/docs/doc/README.md112
-rwxr-xr-xdoc/docs/zh/doc/README.md106
2 files changed, 109 insertions, 109 deletions
diff --git a/doc/docs/doc/README.md b/doc/docs/doc/README.md
index aaeea2d..54c6b72 100755
--- a/doc/docs/doc/README.md
+++ b/doc/docs/doc/README.md
@@ -30,7 +30,7 @@ inventory =
30 count: 3 30 count: 3
31 31
32-- pipe operator 32-- pipe operator
33{1, 2, 3} 33[1, 2, 3]
34 |> map (x)-> x * 2 34 |> map (x)-> x * 2
35 |> filter (x)-> x > 4 35 |> filter (x)-> x > 4
36 |> reduce 0, (a, b)-> a + b 36 |> reduce 0, (a, b)-> a + b
@@ -62,7 +62,7 @@ inventory =
62 count: 3 62 count: 3
63 63
64-- pipe operator 64-- pipe operator
65{1, 2, 3} 65[1, 2, 3]
66 |> map (x)-> x * 2 66 |> map (x)-> x * 2
67 |> filter (x)-> x > 4 67 |> filter (x)-> x > 4
68 |> reduce 0, (a, b)-> a + b 68 |> reduce 0, (a, b)-> a + b
@@ -339,7 +339,7 @@ import "utils" as {
339 $, -- symbol to import all macros 339 $, -- symbol to import all macros
340 $foreach: $each -- rename macro $foreach to $each 340 $foreach: $each -- rename macro $foreach to $each
341} 341}
342{1, 2, 3} |> $map(_ * 2) |> $filter(_ > 4) |> $each print _ 342[1, 2, 3] |> $map(_ * 2) |> $filter(_ > 4) |> $each print _
343``` 343```
344<YueDisplay> 344<YueDisplay>
345<pre> 345<pre>
@@ -355,7 +355,7 @@ import "utils" as {
355 $, -- symbol to import all macros 355 $, -- symbol to import all macros
356 $foreach: $each -- rename macro $foreach to $each 356 $foreach: $each -- rename macro $foreach to $each
357} 357}
358{1, 2, 3} |> $map(_ * 2) |> $filter(_ > 4) |> $each print _ 358[1, 2, 3] |> $map(_ * 2) |> $filter(_ > 4) |> $each print _
359]] 359]]
360</pre> 360</pre>
361</YueDisplay> 361</YueDisplay>
@@ -467,12 +467,12 @@ The middle expression is only evaluated once, rather than twice as it would be i
467The **[] =** operator is used to append values to tables. 467The **[] =** operator is used to append values to tables.
468 468
469```moonscript 469```moonscript
470tab = {} 470tab = []
471tab[] = "Value" 471tab[] = "Value"
472``` 472```
473<YueDisplay> 473<YueDisplay>
474<pre> 474<pre>
475tab = {} 475tab = []
476tab[] = "Value" 476tab[] = "Value"
477</pre> 477</pre>
478</YueDisplay> 478</YueDisplay>
@@ -1106,27 +1106,27 @@ This also works with nested data structures as well:
1106 1106
1107```moonscript 1107```moonscript
1108obj2 = { 1108obj2 = {
1109 numbers: {1,2,3,4} 1109 numbers: [1, 2, 3, 4]
1110 properties: { 1110 properties: {
1111 color: "green" 1111 color: "green"
1112 height: 13.5 1112 height: 13.5
1113 } 1113 }
1114} 1114}
1115 1115
1116{numbers: {first, second}} = obj2 1116{numbers: [first, second]} = obj2
1117print first, second, color 1117print first, second, color
1118``` 1118```
1119<YueDisplay> 1119<YueDisplay>
1120<pre> 1120<pre>
1121obj2 = { 1121obj2 = {
1122 numbers: {1,2,3,4} 1122 numbers: [1, 2, 3, 4]
1123 properties: { 1123 properties: {
1124 color: "green" 1124 color: "green"
1125 height: 13.5 1125 height: 13.5
1126 } 1126 }
1127} 1127}
1128 1128
1129{numbers: {first, second}} = obj2 1129{numbers: [first, second]} = obj2
1130print first, second, color 1130print first, second, color
1131</pre> 1131</pre>
1132</YueDisplay> 1132</YueDisplay>
@@ -1135,7 +1135,7 @@ If the destructuring statement is complicated, feel free to spread it out over a
1135 1135
1136```moonscript 1136```moonscript
1137{ 1137{
1138 numbers: {first, second} 1138 numbers: [first, second]
1139 properties: { 1139 properties: {
1140 color: color 1140 color: color
1141 } 1141 }
@@ -1144,7 +1144,7 @@ If the destructuring statement is complicated, feel free to spread it out over a
1144<YueDisplay> 1144<YueDisplay>
1145<pre> 1145<pre>
1146{ 1146{
1147 numbers: {first, second} 1147 numbers: [first, second]
1148 properties: { 1148 properties: {
1149 color: color 1149 color: color
1150 } 1150 }
@@ -1188,11 +1188,11 @@ You can write default values while doing destructuring like:
1188You can use `_` as placeholder when doing a list destructuring: 1188You can use `_` as placeholder when doing a list destructuring:
1189 1189
1190```moonscript 1190```moonscript
1191{_, two, _, four} = items 1191[_, two, _, four] = items
1192``` 1192```
1193<YueDisplay> 1193<YueDisplay>
1194<pre> 1194<pre>
1195{_, two, _, four} = items 1195[_, two, _, four] = items
1196</pre> 1196</pre>
1197</YueDisplay> 1197</YueDisplay>
1198 1198
@@ -1201,22 +1201,22 @@ You can use `_` as placeholder when doing a list destructuring:
1201Destructuring can also show up in places where an assignment implicitly takes place. An example of this is a for loop: 1201Destructuring can also show up in places where an assignment implicitly takes place. An example of this is a for loop:
1202 1202
1203```moonscript 1203```moonscript
1204tuples = { 1204tuples = [
1205 {"hello", "world"} 1205 ["hello", "world"]
1206 {"egg", "head"} 1206 ["egg", "head"]
1207} 1207]
1208 1208
1209for {left, right} in *tuples 1209for [left, right] in *tuples
1210 print left, right 1210 print left, right
1211``` 1211```
1212<YueDisplay> 1212<YueDisplay>
1213<pre> 1213<pre>
1214tuples = { 1214tuples = [
1215 {"hello", "world"} 1215 ["hello", "world"]
1216 {"egg", "head"} 1216 ["egg", "head"]
1217} 1217]
1218 1218
1219for {left, right} in *tuples 1219for [left, right] in *tuples
1220 print left, right 1220 print left, right
1221</pre> 1221</pre>
1222</YueDisplay> 1222</YueDisplay>
@@ -1275,7 +1275,7 @@ print "OK"
1275 1275
1276You can assign the results returned from a function to a varargs symbol `...`. And then access its content using the Lua way. 1276You can assign the results returned from a function to a varargs symbol `...`. And then access its content using the Lua way.
1277```moonscript 1277```moonscript
1278list = {1, 2, 3, 4, 5} 1278list = [1, 2, 3, 4, 5]
1279fn = (ok) -> ok, table.unpack list 1279fn = (ok) -> ok, table.unpack list
1280ok, ... = fn true 1280ok, ... = fn true
1281count = select '#', ... 1281count = select '#', ...
@@ -1284,7 +1284,7 @@ print ok, count, first
1284``` 1284```
1285<YueDisplay> 1285<YueDisplay>
1286<pre> 1286<pre>
1287list = {1, 2, 3, 4, 5} 1287list = [1, 2, 3, 4, 5]
1288fn = (ok) -> ok, table.unpack list 1288fn = (ok) -> ok, table.unpack list
1289ok, ... = fn true 1289ok, ... = fn true
1290count = select '#', ... 1290count = select '#', ...
@@ -1731,36 +1731,36 @@ my_func 5, 6, 7,
1731Because tables also use the comma as a delimiter, this indentation syntax is helpful for letting values be part of the argument list instead of being part of the table. 1731Because tables also use the comma as a delimiter, this indentation syntax is helpful for letting values be part of the argument list instead of being part of the table.
1732 1732
1733```moonscript 1733```moonscript
1734x = { 1734x = [
1735 1, 2, 3, 4, a_func 4, 5, 1735 1, 2, 3, 4, a_func 4, 5,
1736 5, 6, 1736 5, 6,
1737 8, 9, 10 1737 8, 9, 10
1738} 1738]
1739``` 1739```
1740<YueDisplay> 1740<YueDisplay>
1741<pre> 1741<pre>
1742x = { 1742x = [
1743 1, 2, 3, 4, a_func 4, 5, 1743 1, 2, 3, 4, a_func 4, 5,
1744 5, 6, 1744 5, 6,
1745 8, 9, 10 1745 8, 9, 10
1746} 1746]
1747</pre> 1747</pre>
1748</YueDisplay> 1748</YueDisplay>
1749 1749
1750Although uncommon, notice how we can give a deeper indentation for function arguments if we know we will be using a lower indentation further on. 1750Although uncommon, notice how we can give a deeper indentation for function arguments if we know we will be using a lower indentation further on.
1751 1751
1752```moonscript 1752```moonscript
1753y = { my_func 1, 2, 3, 1753y = [ my_func 1, 2, 3,
1754 4, 5, 1754 4, 5,
1755 5, 6, 7 1755 5, 6, 7
1756} 1756]
1757``` 1757```
1758<YueDisplay> 1758<YueDisplay>
1759<pre> 1759<pre>
1760y = { my_func 1, 2, 3, 1760y = [ my_func 1, 2, 3,
1761 4, 5, 1761 4, 5,
1762 5, 6, 7 1762 5, 6, 7
1763} 1763]
1764</pre> 1764</pre>
1765</YueDisplay> 1765</YueDisplay>
1766 1766
@@ -1826,12 +1826,12 @@ print @value
1826You can specify a placeholder for where you want the backcall function to go as a parameter. 1826You can specify a placeholder for where you want the backcall function to go as a parameter.
1827 1827
1828```moonscript 1828```moonscript
1829(x) <- map _, {1, 2, 3} 1829(x) <- map _, [1, 2, 3]
1830x * 2 1830x * 2
1831``` 1831```
1832<YueDisplay> 1832<YueDisplay>
1833<pre> 1833<pre>
1834(x) <- map _, {1, 2, 3} 1834(x) <- map _, [1, 2, 3]
1835x * 2 1835x * 2
1836</pre> 1836</pre>
1837</YueDisplay> 1837</YueDisplay>
@@ -1862,11 +1862,11 @@ print result, msg
1862Like in Lua, tables are delimited in curly braces. 1862Like in Lua, tables are delimited in curly braces.
1863 1863
1864```moonscript 1864```moonscript
1865some_values = { 1, 2, 3, 4 } 1865some_values = [1, 2, 3, 4]
1866``` 1866```
1867<YueDisplay> 1867<YueDisplay>
1868<pre> 1868<pre>
1869some_values = { 1, 2, 3, 4 } 1869some_values = [1, 2, 3, 4]
1870</pre> 1870</pre>
1871</YueDisplay> 1871</YueDisplay>
1872 1872
@@ -1895,14 +1895,14 @@ The curly braces can be left off if a single table of key value pairs is being a
1895profile = 1895profile =
1896 height: "4 feet", 1896 height: "4 feet",
1897 shoe_size: 13, 1897 shoe_size: 13,
1898 favorite_foods: {"ice cream", "donuts"} 1898 favorite_foods: ["ice cream", "donuts"]
1899``` 1899```
1900<YueDisplay> 1900<YueDisplay>
1901<pre> 1901<pre>
1902profile = 1902profile =
1903 height: "4 feet", 1903 height: "4 feet",
1904 shoe_size: 13, 1904 shoe_size: 13,
1905 favorite_foods: {"ice cream", "donuts"} 1905 favorite_foods: ["ice cream", "donuts"]
1906</pre> 1906</pre>
1907</YueDisplay> 1907</YueDisplay>
1908 1908
@@ -1998,13 +1998,13 @@ t = {
1998Lua tables have both an array part and a hash part, but sometimes you want to make a semantic distinction between array and hash usage when writing Lua tables. Then you can write Lua table with **[ ]** instead of **{ }** to represent an array table and writing any key value pair in a list table won't be allowed. 1998Lua tables have both an array part and a hash part, but sometimes you want to make a semantic distinction between array and hash usage when writing Lua tables. Then you can write Lua table with **[ ]** instead of **{ }** to represent an array table and writing any key value pair in a list table won't be allowed.
1999 1999
2000```moonscript 2000```moonscript
2001some_values = [ 1, 2, 3, 4 ] 2001some_values = [1, 2, 3, 4]
2002list_with_one_element = [ 1, ] 2002list_with_one_element = [1, ]
2003``` 2003```
2004<YueDisplay> 2004<YueDisplay>
2005<pre> 2005<pre>
2006some_values = [ 1, 2, 3, 4 ] 2006some_values = [1, 2, 3, 4]
2007list_with_one_element = [ 1, ] 2007list_with_one_element = [1, ]
2008</pre> 2008</pre>
2009</YueDisplay> 2009</YueDisplay>
2010 2010
@@ -2137,12 +2137,12 @@ The key-value tuple in a table comprehension can also come from a single express
2137In this example we convert an array of pairs to a table where the first item in the pair is the key and the second is the value. 2137In this example we convert an array of pairs to a table where the first item in the pair is the key and the second is the value.
2138 2138
2139```moonscript 2139```moonscript
2140tuples = {{"hello", "world"}, {"foo", "bar"}} 2140tuples = [ ["hello", "world"], ["foo", "bar"]]
2141tbl = {unpack tuple for tuple in *tuples} 2141tbl = {unpack tuple for tuple in *tuples}
2142``` 2142```
2143<YueDisplay> 2143<YueDisplay>
2144<pre> 2144<pre>
2145tuples = { {"hello", "world"}, {"foo", "bar"} } 2145tuples = [ ["hello", "world"], ["foo", "bar"]]
2146tbl = {unpack tuple for tuple in *tuples} 2146tbl = {unpack tuple for tuple in *tuples}
2147</pre> 2147</pre>
2148</YueDisplay> 2148</YueDisplay>
@@ -2371,14 +2371,14 @@ while i < 10
2371continue can also be used with loop expressions to prevent that iteration from accumulating into the result. This examples filters the array table into just even numbers: 2371continue can also be used with loop expressions to prevent that iteration from accumulating into the result. This examples filters the array table into just even numbers:
2372 2372
2373```moonscript 2373```moonscript
2374my_numbers = {1, 2, 3, 4, 5, 6} 2374my_numbers = [1, 2, 3, 4, 5, 6]
2375odds = for x in *my_numbers 2375odds = for x in *my_numbers
2376 continue if x % 2 == 1 2376 continue if x % 2 == 1
2377 x 2377 x
2378``` 2378```
2379<YueDisplay> 2379<YueDisplay>
2380<pre> 2380<pre>
2381my_numbers = {1, 2, 3, 4, 5, 6} 2381my_numbers = [1, 2, 3, 4, 5, 6]
2382odds = for x in *my_numbers 2382odds = for x in *my_numbers
2383 continue if x % 2 == 1 2383 continue if x % 2 == 1
2384 x 2384 x
@@ -2492,7 +2492,7 @@ You can write range checking code with an `in-expression`.
2492```moonscript 2492```moonscript
2493a = 5 2493a = 5
2494 2494
2495if a in {1, 3, 5, 7} 2495if a in [1, 3, 5, 7]
2496 print "checking equality with discrete values" 2496 print "checking equality with discrete values"
2497 2497
2498if a in list 2498if a in list
@@ -2502,7 +2502,7 @@ if a in list
2502<pre> 2502<pre>
2503a = 5 2503a = 5
2504 2504
2505if a in {1, 3, 5, 7} 2505if a in [1, 3, 5, 7]
2506 print "checking equality with discrete values" 2506 print "checking equality with discrete values"
2507 2507
2508if a in list 2508if a in list
@@ -2780,7 +2780,7 @@ Consider the example below, the clothes property is shared amongst all instances
2780 2780
2781```moonscript 2781```moonscript
2782class Person 2782class Person
2783 clothes: {} 2783 clothes: []
2784 give_item: (name)=> 2784 give_item: (name)=>
2785 table.insert @clothes, name 2785 table.insert @clothes, name
2786 2786
@@ -2796,7 +2796,7 @@ print item for item in *a.clothes
2796<YueDisplay> 2796<YueDisplay>
2797<pre> 2797<pre>
2798class Person 2798class Person
2799 clothes: {} 2799 clothes: []
2800 give_item: (name)=> 2800 give_item: (name)=>
2801 table.insert @clothes, name 2801 table.insert @clothes, name
2802 2802
@@ -2816,13 +2816,13 @@ The proper way to avoid this problem is to create the mutable state of the objec
2816```moonscript 2816```moonscript
2817class Person 2817class Person
2818 new: => 2818 new: =>
2819 @clothes = {} 2819 @clothes = []
2820``` 2820```
2821<YueDisplay> 2821<YueDisplay>
2822<pre> 2822<pre>
2823class Person 2823class Person
2824 new: => 2824 new: =>
2825 @clothes = {} 2825 @clothes = []
2826</pre> 2826</pre>
2827</YueDisplay> 2827</YueDisplay>
2828 2828
@@ -3278,7 +3278,7 @@ create_person = (name, relatives)->
3278 .name = name 3278 .name = name
3279 \add_relative relative for relative in *relatives 3279 \add_relative relative for relative in *relatives
3280 3280
3281me = create_person "Leaf", {dad, mother, sister} 3281me = create_person "Leaf", [dad, mother, sister]
3282``` 3282```
3283<YueDisplay> 3283<YueDisplay>
3284<pre> 3284<pre>
@@ -3287,7 +3287,7 @@ create_person = (name, relatives)->
3287 .name = name 3287 .name = name
3288 \add_relative relative for relative in *relatives 3288 \add_relative relative for relative in *relatives
3289 3289
3290me = create_person "Leaf", {dad, mother, sister} 3290me = create_person "Leaf", [dad, mother, sister]
3291</pre> 3291</pre>
3292</YueDisplay> 3292</YueDisplay>
3293 3293
diff --git a/doc/docs/zh/doc/README.md b/doc/docs/zh/doc/README.md
index 340498b..69f5e68 100755
--- a/doc/docs/zh/doc/README.md
+++ b/doc/docs/zh/doc/README.md
@@ -30,7 +30,7 @@ inventory =
30 count: 3 30 count: 3
31 31
32-- 管道操作符 32-- 管道操作符
33{1, 2, 3} 33[1, 2, 3]
34 |> map (x)-> x * 2 34 |> map (x)-> x * 2
35 |> filter (x)-> x > 4 35 |> filter (x)-> x > 4
36 |> reduce 0, (a, b)-> a + b 36 |> reduce 0, (a, b)-> a + b
@@ -62,7 +62,7 @@ inventory =
62 count: 3 62 count: 3
63 63
64-- 管道操作符 64-- 管道操作符
65{1, 2, 3} 65[1, 2, 3]
66 |> map (x)-> x * 2 66 |> map (x)-> x * 2
67 |> filter (x)-> x > 4 67 |> filter (x)-> x > 4
68 |> reduce 0, (a, b)-> a + b 68 |> reduce 0, (a, b)-> a + b
@@ -336,7 +336,7 @@ import "utils" as {
336 $, -- 表示导入所有宏的符号 336 $, -- 表示导入所有宏的符号
337 $foreach: $each -- 重命名宏 $foreach 为 $each 337 $foreach: $each -- 重命名宏 $foreach 为 $each
338} 338}
339{1, 2, 3} |> $map(_ * 2) |> $filter(_ > 4) |> $each print _ 339[1, 2, 3] |> $map(_ * 2) |> $filter(_ > 4) |> $each print _
340``` 340```
341<YueDisplay> 341<YueDisplay>
342<pre> 342<pre>
@@ -352,7 +352,7 @@ import "utils" as {
352 $, -- 表示导入所有宏的符号 352 $, -- 表示导入所有宏的符号
353 $foreach: $each -- 重命名宏 $foreach 为 $each 353 $foreach: $each -- 重命名宏 $foreach 为 $each
354} 354}
355{1, 2, 3} |> $map(_ * 2) |> $filter(_ > 4) |> $each print _ 355[1, 2, 3] |> $map(_ * 2) |> $filter(_ > 4) |> $each print _
356]] 356]]
357</pre> 357</pre>
358</YueDisplay> 358</YueDisplay>
@@ -465,12 +465,12 @@ print v(1) > v(2) <= v(3)
465**[] =** 操作符用于向Lua表的最后插入值。 465**[] =** 操作符用于向Lua表的最后插入值。
466 466
467```moonscript 467```moonscript
468tab = {} 468tab = []
469tab[] = "Value" 469tab[] = "Value"
470``` 470```
471<YueDisplay> 471<YueDisplay>
472<pre> 472<pre>
473tab = {} 473tab = []
474tab[] = "Value" 474tab[] = "Value"
475</pre> 475</pre>
476</YueDisplay> 476</YueDisplay>
@@ -1104,27 +1104,27 @@ print hello, the_day
1104 1104
1105```moonscript 1105```moonscript
1106obj2 = { 1106obj2 = {
1107 numbers: {1,2,3,4} 1107 numbers: [1,2,3,4]
1108 properties: { 1108 properties: {
1109 color: "green" 1109 color: "green"
1110 height: 13.5 1110 height: 13.5
1111 } 1111 }
1112} 1112}
1113 1113
1114{numbers: {first, second}} = obj2 1114{numbers: [first, second]} = obj2
1115print first, second, color 1115print first, second, color
1116``` 1116```
1117<YueDisplay> 1117<YueDisplay>
1118<pre> 1118<pre>
1119obj2 = { 1119obj2 = {
1120 numbers: {1,2,3,4} 1120 numbers: [1,2,3,4]
1121 properties: { 1121 properties: {
1122 color: "green" 1122 color: "green"
1123 height: 13.5 1123 height: 13.5
1124 } 1124 }
1125} 1125}
1126 1126
1127{numbers: {first, second}} = obj2 1127{numbers: [first, second]} = obj2
1128print first, second, color 1128print first, second, color
1129</pre> 1129</pre>
1130</YueDisplay> 1130</YueDisplay>
@@ -1133,7 +1133,7 @@ print first, second, color
1133 1133
1134```moonscript 1134```moonscript
1135{ 1135{
1136 numbers: {first, second} 1136 numbers: [first, second]
1137 properties: { 1137 properties: {
1138 color: color 1138 color: color
1139 } 1139 }
@@ -1142,7 +1142,7 @@ print first, second, color
1142<YueDisplay> 1142<YueDisplay>
1143<pre> 1143<pre>
1144{ 1144{
1145 numbers: {first, second} 1145 numbers: [first, second]
1146 properties: { 1146 properties: {
1147 color: color 1147 color: color
1148 } 1148 }
@@ -1186,7 +1186,7 @@ print first, second, color
1186在进行列表解构时,您可以使用`_`作为占位符: 1186在进行列表解构时,您可以使用`_`作为占位符:
1187 1187
1188```moonscript 1188```moonscript
1189{_, two, _, four} = items 1189[_, two, _, four] = items
1190``` 1190```
1191<YueDisplay> 1191<YueDisplay>
1192<pre> 1192<pre>
@@ -1199,22 +1199,22 @@ print first, second, color
1199解构也可以出现在其它隐式进行赋值的地方。一个例子是用在for循环: 1199解构也可以出现在其它隐式进行赋值的地方。一个例子是用在for循环:
1200 1200
1201```moonscript 1201```moonscript
1202tuples = { 1202tuples = [
1203 {"hello", "world"} 1203 ["hello", "world"]
1204 {"egg", "head"} 1204 ["egg", "head"]
1205} 1205]
1206 1206
1207for {left, right} in *tuples 1207for [left, right] in *tuples
1208 print left, right 1208 print left, right
1209``` 1209```
1210<YueDisplay> 1210<YueDisplay>
1211<pre> 1211<pre>
1212tuples = { 1212tuples = [
1213 {"hello", "world"} 1213 ["hello", "world"]
1214 {"egg", "head"} 1214 ["egg", "head"]
1215} 1215]
1216 1216
1217for {left, right} in *tuples 1217for [left, right] in *tuples
1218 print left, right 1218 print left, right
1219</pre> 1219</pre>
1220</YueDisplay> 1220</YueDisplay>
@@ -1273,7 +1273,7 @@ print "好的"
1273 1273
1274您可以将函数返回的结果赋值给一个可变参数符号 `...`。然后使用Lua的方式访问其内容。 1274您可以将函数返回的结果赋值给一个可变参数符号 `...`。然后使用Lua的方式访问其内容。
1275```moonscript 1275```moonscript
1276list = {1, 2, 3, 4, 5} 1276list = [1, 2, 3, 4, 5]
1277fn = (ok) -> ok, table.unpack list 1277fn = (ok) -> ok, table.unpack list
1278ok, ... = fn true 1278ok, ... = fn true
1279count = select '#', ... 1279count = select '#', ...
@@ -1282,7 +1282,7 @@ print ok, count, first
1282``` 1282```
1283<YueDisplay> 1283<YueDisplay>
1284<pre> 1284<pre>
1285list = {1, 2, 3, 4, 5} 1285list = [1, 2, 3, 4, 5]
1286fn = (ok) -> ok, table.unpack list 1286fn = (ok) -> ok, table.unpack list
1287ok, ... = fn true 1287ok, ... = fn true
1288count = select '#', ... 1288count = select '#', ...
@@ -1691,36 +1691,36 @@ my_func 5, 6, 7,
1691因为Lua表也使用逗号作为分隔符,这种缩进语法有助于让值成为参数列表的一部分,而不是Lua表的一部分。 1691因为Lua表也使用逗号作为分隔符,这种缩进语法有助于让值成为参数列表的一部分,而不是Lua表的一部分。
1692 1692
1693```moonscript 1693```moonscript
1694x = { 1694x = [
1695 1, 2, 3, 4, a_func 4, 5, 1695 1, 2, 3, 4, a_func 4, 5,
1696 5, 6, 1696 5, 6,
1697 8, 9, 10 1697 8, 9, 10
1698} 1698]
1699``` 1699```
1700<YueDisplay> 1700<YueDisplay>
1701<pre> 1701<pre>
1702x = { 1702x = [
1703 1, 2, 3, 4, a_func 4, 5, 1703 1, 2, 3, 4, a_func 4, 5,
1704 5, 6, 1704 5, 6,
1705 8, 9, 10 1705 8, 9, 10
1706} 1706]
1707</pre> 1707</pre>
1708</YueDisplay> 1708</YueDisplay>
1709 1709
1710有个不常见的写法可以注意一下,如果我们将在后面使用较低的缩进,我们可以为函数参数提供更深的缩进来区分列表的归属。 1710有个不常见的写法可以注意一下,如果我们将在后面使用较低的缩进,我们可以为函数参数提供更深的缩进来区分列表的归属。
1711 1711
1712```moonscript 1712```moonscript
1713y = { my_func 1, 2, 3, 1713y = [ my_func 1, 2, 3,
1714 4, 5, 1714 4, 5,
1715 5, 6, 7 1715 5, 6, 7
1716} 1716]
1717``` 1717```
1718<YueDisplay> 1718<YueDisplay>
1719<pre> 1719<pre>
1720y = { my_func 1, 2, 3, 1720y = [ my_func 1, 2, 3,
1721 4, 5, 1721 4, 5,
1722 5, 6, 7 1722 5, 6, 7
1723} 1723]
1724</pre> 1724</pre>
1725</YueDisplay> 1725</YueDisplay>
1726 1726
@@ -1786,12 +1786,12 @@ print @value
1786您可以通过一个占位符指定回调函数的传参位置。 1786您可以通过一个占位符指定回调函数的传参位置。
1787 1787
1788```moonscript 1788```moonscript
1789(x) <- map _, {1, 2, 3} 1789(x) <- map _, [1, 2, 3]
1790x * 2 1790x * 2
1791``` 1791```
1792<YueDisplay> 1792<YueDisplay>
1793<pre> 1793<pre>
1794(x) <- map _, {1, 2, 3} 1794(x) <- map _, [1, 2, 3]
1795x * 2 1795x * 2
1796</pre> 1796</pre>
1797</YueDisplay> 1797</YueDisplay>
@@ -1822,11 +1822,11 @@ print result, msg
1822和Lua一样,表格可以通过花括号进行定义。 1822和Lua一样,表格可以通过花括号进行定义。
1823 1823
1824```moonscript 1824```moonscript
1825some_values = { 1, 2, 3, 4 } 1825some_values = [1, 2, 3, 4]
1826``` 1826```
1827<YueDisplay> 1827<YueDisplay>
1828<pre> 1828<pre>
1829some_values = { 1, 2, 3, 4 } 1829some_values = [1, 2, 3, 4]
1830</pre> 1830</pre>
1831</YueDisplay> 1831</YueDisplay>
1832 1832
@@ -1855,14 +1855,14 @@ some_values = {
1855profile = 1855profile =
1856 height: "4英尺", 1856 height: "4英尺",
1857 shoe_size: 13, 1857 shoe_size: 13,
1858 favorite_foods: {"冰淇淋", "甜甜圈"} 1858 favorite_foods: ["冰淇淋", "甜甜圈"]
1859``` 1859```
1860<YueDisplay> 1860<YueDisplay>
1861<pre> 1861<pre>
1862profile = 1862profile =
1863 height: "4英尺", 1863 height: "4英尺",
1864 shoe_size: 13, 1864 shoe_size: 13,
1865 favorite_foods: {"冰淇淋", "甜甜圈"} 1865 favorite_foods: ["冰淇淋", "甜甜圈"]
1866</pre> 1866</pre>
1867</YueDisplay> 1867</YueDisplay>
1868 1868
@@ -1977,12 +1977,12 @@ list_with_one_element = [ 1, ]
1977以下操作创建了一个items表的副本,但所有包含的值都翻倍了。 1977以下操作创建了一个items表的副本,但所有包含的值都翻倍了。
1978 1978
1979```moonscript 1979```moonscript
1980items = [ 1, 2, 3, 4 ] 1980items = [1, 2, 3, 4]
1981doubled = [item * 2 for i, item in ipairs items] 1981doubled = [item * 2 for i, item in ipairs items]
1982``` 1982```
1983<YueDisplay> 1983<YueDisplay>
1984<pre> 1984<pre>
1985items = [ 1, 2, 3, 4 ] 1985items = [1, 2, 3, 4]
1986doubled = [item * 2 for i, item in ipairs items] 1986doubled = [item * 2 for i, item in ipairs items]
1987</pre> 1987</pre>
1988</YueDisplay> 1988</YueDisplay>
@@ -2097,12 +2097,12 @@ sqrts = {i, math.sqrt i for i in *numbers}
2097在下面的示例中,我们将一些数组转换为一个表,其中每个数组里的第一项是键,第二项是值。 2097在下面的示例中,我们将一些数组转换为一个表,其中每个数组里的第一项是键,第二项是值。
2098 2098
2099```moonscript 2099```moonscript
2100tuples = {{"hello", "world"}, {"foo", "bar"}} 2100tuples = [ ["hello", "world"], ["foo", "bar"]]
2101tbl = {unpack tuple for tuple in *tuples} 2101tbl = {unpack tuple for tuple in *tuples}
2102``` 2102```
2103<YueDisplay> 2103<YueDisplay>
2104<pre> 2104<pre>
2105tuples = { {"hello", "world"}, {"foo", "bar"} } 2105tuples = [ ["hello", "world"], ["foo", "bar"]]
2106tbl = {unpack tuple for tuple in *tuples} 2106tbl = {unpack tuple for tuple in *tuples}
2107</pre> 2107</pre>
2108</YueDisplay> 2108</YueDisplay>
@@ -2332,14 +2332,14 @@ while i < 10
2332继续语句也可以与各种循环表达式一起使用,以防止当前的循环迭代结果累积到结果列表中。以下示例将数组表过滤为仅包含偶数的数组: 2332继续语句也可以与各种循环表达式一起使用,以防止当前的循环迭代结果累积到结果列表中。以下示例将数组表过滤为仅包含偶数的数组:
2333 2333
2334```moonscript 2334```moonscript
2335my_numbers = {1, 2, 3, 4, 5, 6} 2335my_numbers = [1, 2, 3, 4, 5, 6]
2336odds = for x in *my_numbers 2336odds = for x in *my_numbers
2337 continue if x % 2 == 1 2337 continue if x % 2 == 1
2338 x 2338 x
2339``` 2339```
2340<YueDisplay> 2340<YueDisplay>
2341<pre> 2341<pre>
2342my_numbers = {1, 2, 3, 4, 5, 6} 2342my_numbers = [1, 2, 3, 4, 5, 6]
2343odds = for x in *my_numbers 2343odds = for x in *my_numbers
2344 continue if x % 2 == 1 2344 continue if x % 2 == 1
2345 x 2345 x
@@ -2453,7 +2453,7 @@ print "你真幸运!" unless math.random! > 0.1
2453```moonscript 2453```moonscript
2454a = 5 2454a = 5
2455 2455
2456if a in {1, 3, 5, 7} 2456if a in [1, 3, 5, 7]
2457 print "检查离散值的相等性" 2457 print "检查离散值的相等性"
2458 2458
2459if a in list 2459if a in list
@@ -2463,7 +2463,7 @@ if a in list
2463<pre> 2463<pre>
2464a = 5 2464a = 5
2465 2465
2466if a in {1, 3, 5, 7} 2466if a in [1, 3, 5, 7]
2467 print "检查离散值的相等性" 2467 print "检查离散值的相等性"
2468 2468
2469if a in list 2469if a in list
@@ -2741,7 +2741,7 @@ inv\add_item "pants"
2741 2741
2742```moonscript 2742```moonscript
2743class Person 2743class Person
2744 clothes: {} 2744 clothes: []
2745 give_item: (name)=> 2745 give_item: (name)=>
2746 table.insert @clothes, name 2746 table.insert @clothes, name
2747 2747
@@ -2757,7 +2757,7 @@ print item for item in *a.clothes
2757<YueDisplay> 2757<YueDisplay>
2758<pre> 2758<pre>
2759class Person 2759class Person
2760 clothes: {} 2760 clothes: []
2761 give_item: (name)=> 2761 give_item: (name)=>
2762 table.insert @clothes, name 2762 table.insert @clothes, name
2763 2763
@@ -2777,13 +2777,13 @@ print item for item in *a.clothes
2777```moonscript 2777```moonscript
2778class Person 2778class Person
2779 new: => 2779 new: =>
2780 @clothes = {} 2780 @clothes = []
2781``` 2781```
2782<YueDisplay> 2782<YueDisplay>
2783<pre> 2783<pre>
2784class Person 2784class Person
2785 new: => 2785 new: =>
2786 @clothes = {} 2786 @clothes = []
2787</pre> 2787</pre>
2788</YueDisplay> 2788</YueDisplay>
2789 2789
@@ -3239,7 +3239,7 @@ create_person = (name, relatives)->
3239 .name = name 3239 .name = name
3240 \add_relative relative for relative in *relatives 3240 \add_relative relative for relative in *relatives
3241 3241
3242me = create_person "Leaf", {dad, mother, sister} 3242me = create_person "Leaf", [dad, mother, sister]
3243``` 3243```
3244<YueDisplay> 3244<YueDisplay>
3245<pre> 3245<pre>
@@ -3248,7 +3248,7 @@ create_person = (name, relatives)->
3248 .name = name 3248 .name = name
3249 \add_relative relative for relative in *relatives 3249 \add_relative relative for relative in *relatives
3250 3250
3251me = create_person "Leaf", {dad, mother, sister} 3251me = create_person "Leaf", [dad, mother, sister]
3252</pre> 3252</pre>
3253</YueDisplay> 3253</YueDisplay>
3254 3254