From 652a8839f885b73fff57942a9db8b26e9cb5233b Mon Sep 17 00:00:00 2001 From: Li Jin Date: Sun, 22 Oct 2023 00:55:02 +0800 Subject: fixing issues from #152. --- doc/docs/zh/doc/README.md | 182 ++++++++++++++++++++++++++-------------------- 1 file changed, 102 insertions(+), 80 deletions(-) (limited to 'doc/docs/zh') diff --git a/doc/docs/zh/doc/README.md b/doc/docs/zh/doc/README.md index 1d234a0..8a236c1 100755 --- a/doc/docs/zh/doc/README.md +++ b/doc/docs/zh/doc/README.md @@ -386,6 +386,80 @@ tb::func! if tb != nil +### 链式比较 + +您可以在月之脚本中进行比较表达式的链式书写: + +```moonscript +print 1 < 2 <= 2 < 3 == 3 > 2 >= 1 == 1 < 3 != 5 +-- 输出:true + +a = 5 +print 1 <= a <= 10 +-- 输出:true +``` + +
+print 1 < 2 <= 2 < 3 == 3 > 2 >= 1 == 1 < 3 != 5
+-- 输出:true
+
+a = 5
+print 1 <= a <= 10
+-- 输出:true
+
+
+ +可以注意一下链式比较表达式的求值行为: + +```moonscript +v = (x)-> + print x + x + +print v(1) < v(2) <= v(3) +--[[ + 输出: + 2 + 1 + 3 + true +]] + +print v(1) > v(2) <= v(3) +--[[ + 输出: + 2 + 1 + false +]] +``` + +
+v = (x)->
+   print x
+   x
+
+print v(1) < v(2) <= v(3)
+--[[
+   输出:
+   2
+   1
+   3
+   true
+]]
+
+print v(1) > v(2) <= v(3)
+--[[
+   输出:
+   2
+   1
+   false
+]]
+
+
+ +在上面的例子里,中间的表达式`v(2)`仅被计算一次,如果把表达式写成`v(1) < v(2) and v(2) <= v(3)`的方式,中间的`v(2)`才会被计算两次。在链式比较中,求值的顺序往往是未定义的。所以强烈建议不要在链式比较中使用具有副作用(比如做打印操作)的表达式。如果需要使用有副作用的函数,应明确使用短路 `and` 运算符来做连接。 + ### 表追加 **[] =** 操作符用于向Lua表的最后插入值。 @@ -982,17 +1056,17 @@ do 最好是通过示例来解释。以下是如何从表格中解包前两个值的方法: ```moonscript -thing = {1, 2} +thing = [1, 2] -{a, b} = thing +[a, b] = thing print a, b ```
-thing = {1, 2}
+thing = [1, 2]
 
-{a, b} = thing
+[a, b] = thing
 print a, b
 
@@ -1881,6 +1955,19 @@ t = { +Lua的表同时具有数组部分和哈希部分,但有时候你会希望在书写Lua表时,对Lua表做数组和哈希不同用法的语义区分。然后你可以用 **[ ]** 而不是 **{ }** 来编写表示数组的 Lua 表,并且不允许在数组 Lua 表中写入任何键值对。 + +```moonscript +some_values = [ 1, 2, 3, 4 ] +list_with_one_element = [ 1, ] +``` + +
+some_values = [ 1, 2, 3, 4 ]
+list_with_one_element = [ 1, ]
+
+
+ ## 推导式 推导式为我们提供了一种便捷的语法,通过遍历现有对象并对其值应用表达式来构造出新的表格。月之脚本有两种推导式:列表推导式和表格推导式。它们最终都是产生Lua表格;列表推导式将值累积到类似数组的表格中,而表格推导式允许您在每次遍历时设置新表格的键和值。 @@ -1890,12 +1977,12 @@ t = { 以下操作创建了一个items表的副本,但所有包含的值都翻倍了。 ```moonscript -items = { 1, 2, 3, 4 } +items = [ 1, 2, 3, 4 ] doubled = [item * 2 for i, item in ipairs items] ```
-items = { 1, 2, 3, 4 }
+items = [ 1, 2, 3, 4 ]
 doubled = [item * 2 for i, item in ipairs items]
 
@@ -1929,18 +2016,18 @@ for和when子句可以根据需要进行链式操作。唯一的要求是推导 使用多个for子句与使用多重循环的效果相同: ```moonscript -x_coords = {4, 5, 6, 7} -y_coords = {9, 2, 3} +x_coords = [4, 5, 6, 7] +y_coords = [9, 2, 3] -points = [{x, y} for x in *x_coords \ +points = [ [x, y] for x in *x_coords \ for y in *y_coords] ```
-x_coords = {4, 5, 6, 7}
-y_coords = {9, 2, 3}
+x_coords = [4, 5, 6, 7]
+y_coords = [9, 2, 3]
 
-points = [{x, y} for x in *x_coords \
+points = [ [x, y] for x in *x_coords \
 for y in *y_coords]
 
@@ -1995,12 +2082,12 @@ no_color = {k, v for k, v in pairs thing when k != "color"} **\***操作符在表格推导式中能使用。在下面的例子里,我们为几个数字创建了一个平方根查找表。 ```moonscript -numbers = {1, 2, 3, 4} +numbers = [1, 2, 3, 4] sqrts = {i, math.sqrt i for i in *numbers} ```
-numbers = {1, 2, 3, 4}
+numbers = [1, 2, 3, 4]
 sqrts = {i, math.sqrt i for i in *numbers}
 
@@ -2361,20 +2448,11 @@ print "你真幸运!" unless math.random! > 0.1 ### 范围表达式 -您可以使用范围表达式来编写进行边界范围检查的代码。 +您可以使用范围表达式来编写进行范围检查的代码。 ```moonscript a = 5 -if a in [1, 10] - print "a在1到10的范围内" - -if a not in [1, 10] - print "a不在1到10的范围内" - -if a in (0, 11) - print "a在0到11的开放区间内" - if a in {1, 3, 5, 7} print "检查离散值的相等性" @@ -2385,15 +2463,6 @@ if a in list
 a = 5
 
-if a in [1, 10]
-  print "a在1到10的范围内"
-
-if a not in [1, 10]
-  print "a不在1到10的范围内"
-
-if a in (0, 11)
-  print "a在0到11的开放区间内"
-
 if a in {1, 3, 5, 7}
   print "检查离散值的相等性"
 
@@ -2610,53 +2679,6 @@ switch item
 
-### 范围匹配 - -使用`in`范围匹配表达式,你可以在switch的when子句中进行范围匹配的检查处理。 - -```moonscript -value = 5 - -switch item - -- 使用闭区间进行范围检查 - when in [1, 3] - print "1 <= value <= 3" - - -- 使用开闭区间进行范围检查 - when in (6, 8] - print "6 < value <= 8" - - -- 检查不在范围内 - when not in [1, 10) - print "不是 (1 <= value < 10)" - - -- 检查离散值 - when in {11, 21, 99} - print "值是 11, 21 或 99" -``` - -
-value = 5
-
-switch item
-  -- 使用闭区间进行范围检查
-  when in [1, 3]
-    print "1 <= value <= 3"
-
-  -- 使用开闭区间进行范围检查
-  when in (6, 8]
-    print "6 < value <= 8"
-
-  -- 检查不在范围内
-  when not in [1, 10)
-    print "不是 (1 <= value < 10)"
-
-  -- 检查离散值
-  when in {11, 21, 99}
-    print "值是 11, 21 或 99"
-
-
- ## 面向对象编程 在以下的示例中,月之脚本生成的Lua代码可能看起来会很复杂。所以最好主要关注月之脚本代码层面的意义,然后如果您想知道关于面向对象功能的实现细节,再查看Lua代码。 -- cgit v1.2.3-55-g6feb