diff options
Diffstat (limited to 'doc/docs/zh/doc/README.md')
-rwxr-xr-x | doc/docs/zh/doc/README.md | 27 |
1 files changed, 26 insertions, 1 deletions
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 | -- 列表推导 | ||
33 | map = (arr, action) -> | ||
34 | [action item for item in *arr] | ||
35 | |||
36 | filter = (arr, cond) -> | ||
37 | [item for item in *arr when cond item] | ||
38 | |||
39 | reduce = (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 | ||
2304 | first_large = for n in *numbers | ||
2305 | break n if n > 10 | ||
2306 | ``` | ||
2307 | <YueDisplay> | ||
2308 | <pre> | ||
2309 | first_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 |
2294 | func_a = -> for i = 1, 10 do print i | 2319 | func_a = -> for i = 1, 10 do print i |