diff options
Diffstat (limited to 'doc/docs/doc/README.md')
-rwxr-xr-x | doc/docs/doc/README.md | 27 |
1 files changed, 27 insertions, 0 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 | ||
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 | -- 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 | ||
2337 | In 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 | |||
2339 | For example, to find the first number greater than 10: | ||
2340 | |||
2341 | ```moonscript | ||
2342 | first_large = for n in *numbers | ||
2343 | break n if n > 10 | ||
2344 | ``` | ||
2345 | <YueDisplay> | ||
2346 | <pre> | ||
2347 | first_large = for n in *numbers | ||
2348 | break n if n > 10 | ||
2349 | </pre> | ||
2350 | </YueDisplay> | ||
2351 | |||
2352 | This break-with-value syntax enables concise and expressive search or early-exit patterns directly within loop expressions. | ||
2353 | |||
2327 | You can also filter values by combining the for loop expression with the continue statement. | 2354 | You can also filter values by combining the for loop expression with the continue statement. |
2328 | 2355 | ||
2329 | For 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. | 2356 | For 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. |