aboutsummaryrefslogtreecommitdiff
path: root/doc/docs
diff options
context:
space:
mode:
Diffstat (limited to 'doc/docs')
-rwxr-xr-xdoc/docs/doc/README.md27
-rwxr-xr-xdoc/docs/zh/doc/README.md27
2 files changed, 53 insertions, 1 deletions
diff --git a/doc/docs/doc/README.md b/doc/docs/doc/README.md
index c275c52..dac96ee 100755
--- a/doc/docs/doc/README.md
+++ b/doc/docs/doc/README.md
@@ -29,6 +29,16 @@ inventory =
29 * name: "bread" 29 * name: "bread"
30 count: 3 30 count: 3
31 31
32-- list comprehension
33map = (arr, action) ->
34 [action item for item in *arr]
35
36filter = (arr, cond) ->
37 [item for item in *arr when cond item]
38
39reduce = (arr, init, action): init ->
40 init = action init, item for item in *arr
41
32-- pipe operator 42-- pipe operator
33[1, 2, 3] 43[1, 2, 3]
34 |> map (x) -> x * 2 44 |> map (x) -> x * 2
@@ -2324,6 +2334,23 @@ doubled_evens = for i = 1, 20
2324</pre> 2334</pre>
2325</YueDisplay> 2335</YueDisplay>
2326 2336
2337In addition, for loops support break with a return value, allowing the loop itself to be used as an expression that exits early with a meaningful result.
2338
2339For example, to find the first number greater than 10:
2340
2341```moonscript
2342first_large = for n in *numbers
2343 break n if n > 10
2344```
2345<YueDisplay>
2346<pre>
2347first_large = for n in *numbers
2348 break n if n > 10
2349</pre>
2350</YueDisplay>
2351
2352This break-with-value syntax enables concise and expressive search or early-exit patterns directly within loop expressions.
2353
2327You can also filter values by combining the for loop expression with the continue statement. 2354You can also filter values by combining the for loop expression with the continue statement.
2328 2355
2329For loops at the end of a function body are not accumulated into a table for a return value (Instead the function will return nil). Either an explicit return statement can be used, or the loop can be converted into a list comprehension. 2356For loops at the end of a function body are not accumulated into a table for a return value (Instead the function will return nil). Either an explicit return statement can be used, or the loop can be converted into a list comprehension.
diff --git a/doc/docs/zh/doc/README.md b/doc/docs/zh/doc/README.md
index 11dc108..850afed 100755
--- a/doc/docs/zh/doc/README.md
+++ b/doc/docs/zh/doc/README.md
@@ -29,6 +29,16 @@ inventory =
29 * name: "bread" 29 * name: "bread"
30 count: 3 30 count: 3
31 31
32-- 列表推导
33map = (arr, action) ->
34 [action item for item in *arr]
35
36filter = (arr, cond) ->
37 [item for item in *arr when cond item]
38
39reduce = (arr, init, action): init ->
40 init = action init, item for item in *arr
41
32-- 管道操作符 42-- 管道操作符
33[1, 2, 3] 43[1, 2, 3]
34 |> map (x) -> x * 2 44 |> map (x) -> x * 2
@@ -2286,9 +2296,24 @@ doubled_evens = for i = 1, 20
2286</pre> 2296</pre>
2287</YueDisplay> 2297</YueDisplay>
2288 2298
2299此外,for循环还支持带返回值的break语句,这样循环本身就可以作为一个表达式,在满足条件时提前退出并返回有意义的结果。
2300
2301例如,查找第一个大于10的数字:
2302
2303```moonscript
2304first_large = for n in *numbers
2305 break n if n > 10
2306```
2307<YueDisplay>
2308<pre>
2309first_large = for n in *numbers
2310 break n if n > 10
2311</pre>
2312</YueDisplay>
2313
2289你还可以结合for循环表达式与continue语句来过滤值。 2314你还可以结合for循环表达式与continue语句来过滤值。
2290 2315
2291注意出现在函数体末尾的for循环,不会被当作是一个表达式,并将循环结果累积到一个列表中作为返回值(相反,函数将返回nil)。如果要函数末尾的循环转换为列表表达式,可以使用返回语句加for循环表达式。 2316注意出现在函数体末尾的for循环,不会被当作是一个表达式并将循环结果累积到一个列表中作为返回值(相反,函数将返回nil)。如果要函数末尾的循环转换为列表表达式,可以显式地使用返回语句加for循环表达式。
2292 2317
2293```moonscript 2318```moonscript
2294func_a = -> for i = 1, 10 do print i 2319func_a = -> for i = 1, 10 do print i