Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions next/language/fundamentals.md
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,17 @@ A functional loop consumes arguments and returns a value. It is defined using th
Currently in `loop exprs { ... }`, `exprs` is nonempty list, while `for { ... }` is accepted for infinite loop.
```

### Labelled Continue/Break

When a loop is labelled, it can be referenced from a `break` or `continue` from
within a nested loop. For example:

```{literalinclude} /sources/language/src/controls/top.mbt
:language: moonbit
:start-after: start loop label
:end-before: end loop label
```

## Iterator

An iterator is an object that traverse through a sequence while providing access
Expand Down
37 changes: 35 additions & 2 deletions next/sources/language/src/controls/top.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ test {
i += j
}
assert_eq!(i, 45)

let mut k = 0
for l in 0..=10 {
k += l
Expand All @@ -220,6 +219,40 @@ test {
}
// end for loop 10

// start loop label
test "break label" {
let mut count = 0
let xs = [1, 2, 3]
let ys = [4, 5, 6]
let res = outer~: for i in xs {
for j in ys {
count = count + i
break outer~ j
}
} else {
-1
}
assert_eq!(res, 4)
assert_eq!(count, 1)
}

test "continue label" {
let mut count = 0
let init = 10
let res =outer~: loop init {
0 => 42
i => {
for {
count = count + 1
continue outer~ i - 1
}
}
}
assert_eq!(res, 42)
assert_eq!(count, 10)
}
// end loop label

// start guard 1
fn guarded_get(array : Array[Int], index : Int) -> Int? {
guard index >= 0 && index < array.length() else { None }
Expand Down Expand Up @@ -277,4 +310,4 @@ fn decide_sport(weather : String, humidity : Int) -> String {
test {
assert_eq!(decide_sport("sunny", 0), "tennis")
}
// end match 1
// end match 1
Loading