Skip to content

Commit 9629f69

Browse files
committed
more documentation
1 parent f75ebb8 commit 9629f69

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed

docs/reference.md

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ single line:
537537

538538
A for loop can also be used an expression. The last statement in the body of
539539
the for loop is coerced into an expression and appended to an accumulating
540-
table if the value of that expression is not `nil`.
540+
array table.
541541

542542
Doubling every even number:
543543

@@ -549,13 +549,8 @@ Doubling every even number:
549549
i
550550
```
551551

552-
Filtering out odd numbers:
553-
554-
```moon
555-
my_numbers = {1,2,3,4,5,6}
556-
odds = for x in *my_numbers
557-
if x % 2 == 1 then x
558-
```
552+
You can also filter values by combining the for loop expression with the
553+
[`continue`](#continue) statement.
559554

560555
For loops at the end of a function body are not accumulated into a table for a
561556
return value (Instead the function will return `nil`). Either an explicit
@@ -1369,6 +1364,26 @@ extract by mixing the syntax:
13691364
{:mix, :max, random: rand } = math
13701365
```
13711366

1367+
### Destructuring In Other Places
1368+
1369+
Destructuring can also show up in places where an assignment implicitly takes
1370+
place. An example of this is a `for` loop:
1371+
1372+
1373+
```moon
1374+
tuples = {
1375+
{"hello", "world"}
1376+
{"egg", "head"}
1377+
}
1378+
1379+
for {left, right} in *tuples
1380+
print left, right
1381+
```
1382+
1383+
We know each element in the array table is a two item tuple, so we can unpack
1384+
it directly in the names clause of the for statement using a destructure.
1385+
1386+
13721387
## Function Stubs
13731388

13741389
It is common to pass a function from an object around as a value, for example,
@@ -1497,7 +1512,7 @@ way to do this:
14971512
Upon installing MoonScript, a `moonscript` module is made available. The best
14981513
use of this module is making your Lua's require function MoonScript aware.
14991514

1500-
```moon
1515+
```lua
15011516
require "moonscript"
15021517
```
15031518

@@ -1522,7 +1537,6 @@ built in `load` function, which is run as the module.
15221537

15231538
### Load Functions
15241539

1525-
15261540
MoonScript provides `moonscript.load`, `moonscript.loadfile`,
15271541
`mooonscript.loadstring`, which are analogous to Lua's `load`, `loadfile`, and
15281542
`loadstring`.
@@ -1532,7 +1546,8 @@ with MoonScript code instead of Lua Code.
15321546

15331547

15341548
```moononly
1535-
require "moonscript"
1549+
moonscript = require "moonscript"
1550+
15361551
fn = moonscript.loadstring 'print "hi!"'
15371552
fn!
15381553
```
@@ -1543,7 +1558,7 @@ makes it so the file does not implicitly return its last statement.
15431558

15441559

15451560
```moononly
1546-
require "moonscript"
1561+
moonscript = require "moonscript"
15471562

15481563
fn = moonscript.loadstring "10"
15491564
print fn! -- prints "10"
@@ -1594,10 +1609,8 @@ Here is a quick example of how you would compile a MoonScript string to a Lua
15941609
String:
15951610

15961611
```moononly
1597-
require "moonscript.parse"
1598-
require "moonscript.compile"
1599-
1600-
import parse, compile from moonscript
1612+
parse = require "moonscript.parse"
1613+
compile = require "moonscript.compile"
16011614

16021615
moon_code = [[(-> print "hello world")!]]
16031616

0 commit comments

Comments
 (0)