diff options
| -rwxr-xr-x | doc/docs/doc/README.md | 98 | ||||
| -rwxr-xr-x | doc/docs/zh/doc/README.md | 98 |
2 files changed, 98 insertions, 98 deletions
diff --git a/doc/docs/doc/README.md b/doc/docs/doc/README.md index d2f838d..0047a3f 100755 --- a/doc/docs/doc/README.md +++ b/doc/docs/doc/README.md | |||
| @@ -1528,55 +1528,6 @@ print ok, count, first | |||
| 1528 | </pre> | 1528 | </pre> |
| 1529 | </YueDisplay> | 1529 | </YueDisplay> |
| 1530 | 1530 | ||
| 1531 | ### Named Varargs | ||
| 1532 | |||
| 1533 | You can use the `(...t) ->` syntax to automatically store varargs into a named table. This table will contain all passed arguments (including `nil` values), and the `n` field of the table will store the actual number of arguments passed (including `nil` values). | ||
| 1534 | |||
| 1535 | ```moonscript | ||
| 1536 | f = (...t) -> | ||
| 1537 | print "argument count:", t.n | ||
| 1538 | print "table length:", #t | ||
| 1539 | for i = 1, t.n | ||
| 1540 | print t[i] | ||
| 1541 | |||
| 1542 | f 1, 2, 3 | ||
| 1543 | f "a", "b", "c", "d" | ||
| 1544 | f! | ||
| 1545 | |||
| 1546 | -- Handling cases with nil values | ||
| 1547 | process = (...args) -> | ||
| 1548 | sum = 0 | ||
| 1549 | for i = 1, args.n | ||
| 1550 | if args[i] != nil and type(args[i]) == "number" | ||
| 1551 | sum += args[i] | ||
| 1552 | sum | ||
| 1553 | |||
| 1554 | process 1, nil, 3, nil, 5 | ||
| 1555 | ``` | ||
| 1556 | <YueDisplay> | ||
| 1557 | <pre> | ||
| 1558 | f = (...t) -> | ||
| 1559 | print "argument count:", t.n | ||
| 1560 | print "table length:", #t | ||
| 1561 | for i = 1, t.n | ||
| 1562 | print t[i] | ||
| 1563 | |||
| 1564 | f 1, 2, 3 | ||
| 1565 | f "a", "b", "c", "d" | ||
| 1566 | f! | ||
| 1567 | |||
| 1568 | -- Handling cases with nil values | ||
| 1569 | process = (...args) -> | ||
| 1570 | sum = 0 | ||
| 1571 | for i = 1, args.n | ||
| 1572 | if args[i] != nil and type(args[i]) == "number" | ||
| 1573 | sum += args[i] | ||
| 1574 | sum | ||
| 1575 | |||
| 1576 | process 1, nil, 3, nil, 5 | ||
| 1577 | </pre> | ||
| 1578 | </YueDisplay> | ||
| 1579 | |||
| 1580 | ## Whitespace | 1531 | ## Whitespace |
| 1581 | 1532 | ||
| 1582 | YueScript is a whitespace significant language. You have to write some code block in the same indent with space **' '** or tab **'\t'** like function body, value list and some control blocks. And expressions containing different whitespaces might mean different things. Tab is treated like 4 space, but it's better not mix the use of spaces and tabs. | 1533 | YueScript is a whitespace significant language. You have to write some code block in the same indent with space **' '** or tab **'\t'** like function body, value list and some control blocks. And expressions containing different whitespaces might mean different things. Tab is treated like 4 space, but it's better not mix the use of spaces and tabs. |
| @@ -2277,6 +2228,55 @@ findFirstEven = (list) -> | |||
| 2277 | 2228 | ||
| 2278 | The only difference is that you can move the final return expression before the `->` or `=>` token to indicate the function’s implicit return value as the last statement. This way, even in functions with multiple nested loops or conditional branches, you no longer need to write a trailing return expression at the end of the function body, making the logic structure more straightforward and easier to follow. | 2229 | The only difference is that you can move the final return expression before the `->` or `=>` token to indicate the function’s implicit return value as the last statement. This way, even in functions with multiple nested loops or conditional branches, you no longer need to write a trailing return expression at the end of the function body, making the logic structure more straightforward and easier to follow. |
| 2279 | 2230 | ||
| 2231 | ### Named Varargs | ||
| 2232 | |||
| 2233 | You can use the `(...t) ->` syntax to automatically store varargs into a named table. This table will contain all passed arguments (including `nil` values), and the `n` field of the table will store the actual number of arguments passed (including `nil` values). | ||
| 2234 | |||
| 2235 | ```moonscript | ||
| 2236 | f = (...t) -> | ||
| 2237 | print "argument count:", t.n | ||
| 2238 | print "table length:", #t | ||
| 2239 | for i = 1, t.n | ||
| 2240 | print t[i] | ||
| 2241 | |||
| 2242 | f 1, 2, 3 | ||
| 2243 | f "a", "b", "c", "d" | ||
| 2244 | f! | ||
| 2245 | |||
| 2246 | -- Handling cases with nil values | ||
| 2247 | process = (...args) -> | ||
| 2248 | sum = 0 | ||
| 2249 | for i = 1, args.n | ||
| 2250 | if args[i] != nil and type(args[i]) == "number" | ||
| 2251 | sum += args[i] | ||
| 2252 | sum | ||
| 2253 | |||
| 2254 | process 1, nil, 3, nil, 5 | ||
| 2255 | ``` | ||
| 2256 | <YueDisplay> | ||
| 2257 | <pre> | ||
| 2258 | f = (...t) -> | ||
| 2259 | print "argument count:", t.n | ||
| 2260 | print "table length:", #t | ||
| 2261 | for i = 1, t.n | ||
| 2262 | print t[i] | ||
| 2263 | |||
| 2264 | f 1, 2, 3 | ||
| 2265 | f "a", "b", "c", "d" | ||
| 2266 | f! | ||
| 2267 | |||
| 2268 | -- Handling cases with nil values | ||
| 2269 | process = (...args) -> | ||
| 2270 | sum = 0 | ||
| 2271 | for i = 1, args.n | ||
| 2272 | if args[i] != nil and type(args[i]) == "number" | ||
| 2273 | sum += args[i] | ||
| 2274 | sum | ||
| 2275 | |||
| 2276 | process 1, nil, 3, nil, 5 | ||
| 2277 | </pre> | ||
| 2278 | </YueDisplay> | ||
| 2279 | |||
| 2280 | ## Backcalls | 2280 | ## Backcalls |
| 2281 | 2281 | ||
| 2282 | Backcalls are used for unnesting callbacks. They are defined using arrows pointed to the left as the last parameter by default filling in a function call. All the syntax is mostly the same as regular arrow functions except that it is just pointing the other way and the function body does not require indent. | 2282 | Backcalls are used for unnesting callbacks. They are defined using arrows pointed to the left as the last parameter by default filling in a function call. All the syntax is mostly the same as regular arrow functions except that it is just pointing the other way and the function body does not require indent. |
diff --git a/doc/docs/zh/doc/README.md b/doc/docs/zh/doc/README.md index 34d577c..de5dff1 100755 --- a/doc/docs/zh/doc/README.md +++ b/doc/docs/zh/doc/README.md | |||
| @@ -1526,55 +1526,6 @@ print ok, count, first | |||
| 1526 | </pre> | 1526 | </pre> |
| 1527 | </YueDisplay> | 1527 | </YueDisplay> |
| 1528 | 1528 | ||
| 1529 | ### 命名变长参数 | ||
| 1530 | |||
| 1531 | 你可以使用 `(...t) ->` 语法来将变长参数自动存储到一个命名表中。这个表会包含所有传入的参数(包括 `nil` 值),并且会在表的 `n` 字段中存储实际传入的参数个数(包括 `nil` 值在内的个数)。 | ||
| 1532 | |||
| 1533 | ```moonscript | ||
| 1534 | f = (...t) -> | ||
| 1535 | print "参数个数:", t.n | ||
| 1536 | print "表长度:", #t | ||
| 1537 | for i = 1, t.n | ||
| 1538 | print t[i] | ||
| 1539 | |||
| 1540 | f 1, 2, 3 | ||
| 1541 | f "a", "b", "c", "d" | ||
| 1542 | f! | ||
| 1543 | |||
| 1544 | -- 处理包含 nil 的情况 | ||
| 1545 | process = (...args) -> | ||
| 1546 | sum = 0 | ||
| 1547 | for i = 1, args.n | ||
| 1548 | if args[i] != nil and type(args[i]) == "number" | ||
| 1549 | sum += args[i] | ||
| 1550 | sum | ||
| 1551 | |||
| 1552 | process 1, nil, 3, nil, 5 | ||
| 1553 | ``` | ||
| 1554 | <YueDisplay> | ||
| 1555 | <pre> | ||
| 1556 | f = (...t) -> | ||
| 1557 | print "参数个数:", t.n | ||
| 1558 | print "表长度:", #t | ||
| 1559 | for i = 1, t.n | ||
| 1560 | print t[i] | ||
| 1561 | |||
| 1562 | f 1, 2, 3 | ||
| 1563 | f "a", "b", "c", "d" | ||
| 1564 | f! | ||
| 1565 | |||
| 1566 | -- 处理包含 nil 的情况 | ||
| 1567 | process = (...args) -> | ||
| 1568 | sum = 0 | ||
| 1569 | for i = 1, args.n | ||
| 1570 | if args[i] != nil and type(args[i]) == "number" | ||
| 1571 | sum += args[i] | ||
| 1572 | sum | ||
| 1573 | |||
| 1574 | process 1, nil, 3, nil, 5 | ||
| 1575 | </pre> | ||
| 1576 | </YueDisplay> | ||
| 1577 | |||
| 1578 | ## 空白 | 1529 | ## 空白 |
| 1579 | 1530 | ||
| 1580 | 月之脚本是一个对空白敏感的语言。你必须在相同的缩进中使用空格 **' '** 或制表符 **'\t'** 来编写一些代码块,如函数体、值列表和一些控制块。包含不同空白的表达式可能意味着不同的事情。制表符被视为4个空格,但最好不要混合使用空格和制表符。 | 1531 | 月之脚本是一个对空白敏感的语言。你必须在相同的缩进中使用空格 **' '** 或制表符 **'\t'** 来编写一些代码块,如函数体、值列表和一些控制块。包含不同空白的表达式可能意味着不同的事情。制表符被视为4个空格,但最好不要混合使用空格和制表符。 |
| @@ -2237,6 +2188,55 @@ findFirstEven = (list) -> | |||
| 2237 | 2188 | ||
| 2238 | 唯一的区别在于:你可以将函数的返回值表达式提前写在 `->` 或 `=>` 前,用以指示该函数应隐式返回该表达式的值。这样即使在多层循环或条件判断的场景下,也无需编写尾行悬挂的返回表达式,逻辑结构会更加直观清晰。 | 2189 | 唯一的区别在于:你可以将函数的返回值表达式提前写在 `->` 或 `=>` 前,用以指示该函数应隐式返回该表达式的值。这样即使在多层循环或条件判断的场景下,也无需编写尾行悬挂的返回表达式,逻辑结构会更加直观清晰。 |
| 2239 | 2190 | ||
| 2191 | ### 命名变长参数 | ||
| 2192 | |||
| 2193 | 你可以使用 `(...t) ->` 语法来将变长参数自动存储到一个命名表中。这个表会包含所有传入的参数(包括 `nil` 值),并且会在表的 `n` 字段中存储实际传入的参数个数(包括 `nil` 值在内的个数)。 | ||
| 2194 | |||
| 2195 | ```moonscript | ||
| 2196 | f = (...t) -> | ||
| 2197 | print "参数个数:", t.n | ||
| 2198 | print "表长度:", #t | ||
| 2199 | for i = 1, t.n | ||
| 2200 | print t[i] | ||
| 2201 | |||
| 2202 | f 1, 2, 3 | ||
| 2203 | f "a", "b", "c", "d" | ||
| 2204 | f! | ||
| 2205 | |||
| 2206 | -- 处理包含 nil 的情况 | ||
| 2207 | process = (...args) -> | ||
| 2208 | sum = 0 | ||
| 2209 | for i = 1, args.n | ||
| 2210 | if args[i] != nil and type(args[i]) == "number" | ||
| 2211 | sum += args[i] | ||
| 2212 | sum | ||
| 2213 | |||
| 2214 | process 1, nil, 3, nil, 5 | ||
| 2215 | ``` | ||
| 2216 | <YueDisplay> | ||
| 2217 | <pre> | ||
| 2218 | f = (...t) -> | ||
| 2219 | print "参数个数:", t.n | ||
| 2220 | print "表长度:", #t | ||
| 2221 | for i = 1, t.n | ||
| 2222 | print t[i] | ||
| 2223 | |||
| 2224 | f 1, 2, 3 | ||
| 2225 | f "a", "b", "c", "d" | ||
| 2226 | f! | ||
| 2227 | |||
| 2228 | -- 处理包含 nil 的情况 | ||
| 2229 | process = (...args) -> | ||
| 2230 | sum = 0 | ||
| 2231 | for i = 1, args.n | ||
| 2232 | if args[i] != nil and type(args[i]) == "number" | ||
| 2233 | sum += args[i] | ||
| 2234 | sum | ||
| 2235 | |||
| 2236 | process 1, nil, 3, nil, 5 | ||
| 2237 | </pre> | ||
| 2238 | </YueDisplay> | ||
| 2239 | |||
| 2240 | ## 反向回调 | 2240 | ## 反向回调 |
| 2241 | 2241 | ||
| 2242 | 反向回调用于减少函数回调的嵌套。它们使用指向左侧的箭头,并且默认会被定义为传入后续函数调用的最后一个参数。它的语法大部分与常规箭头函数相同,只是它指向另一方向,并且后续的函数体不需要进行缩进。 | 2242 | 反向回调用于减少函数回调的嵌套。它们使用指向左侧的箭头,并且默认会被定义为传入后续函数调用的最后一个参数。它的语法大部分与常规箭头函数相同,只是它指向另一方向,并且后续的函数体不需要进行缩进。 |
