aboutsummaryrefslogtreecommitdiff
path: root/doc/docs/doc/README.md
diff options
context:
space:
mode:
Diffstat (limited to 'doc/docs/doc/README.md')
-rwxr-xr-xdoc/docs/doc/README.md27
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
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.