You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/langref/loops.md
+34-4Lines changed: 34 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,16 +4,37 @@ Loops let you run the same code multiple times, in...well, in a loop.
4
4
5
5
## `for` Loops
6
6
7
-
A `for` loop iterates over each item in a list:
7
+
A `for` loop lets you run code on each item in an [iterator](iterators). For example:
8
8
9
9
```roc
10
10
var $sum = 0
11
+
12
+
for n in 1.to(5) {
13
+
$sum = $sum + n
14
+
}
15
+
```
16
+
17
+
> The `to` method returns a [range](numbers#ranges) which is an `Iter` of the number in question. For example, [`I64.to`](builtins/I64#to) returns `Iter(I64)`, and so `n` in this example would be an `I64`.
18
+
19
+
A loop body only includes statements; it does not have a final expression. The loop itself evaluates to `{}`.
20
+
21
+
### Iterating over types that have `iter`
22
+
23
+
`for` can also be used on types that have an `iter` method, as long as that method returns an [`Iter`](builtins/Iter). For example, [`List`](builtins/list) has (`List.iter`)[builtins/List#iter], so you can do a `for` loop over a list:
24
+
25
+
```roc
26
+
var $sum = 0
27
+
11
28
for n in [1, 2, 3, 4] {
12
29
$sum = $sum + n
13
30
}
14
31
```
15
32
16
-
The item can be destructured inline:
33
+
At runtime, this `[1, 2, 3, 4]` code snippet is exactly as efficient as the earlier `1.to(5)` one. In one case, `1.to(5)` will be evaluated to an `Iter` at compile time, and in the other, `[1, 2, 3, 4].iter()` will be evaluated at compile time to an identical `Iter`. By the time either program actually runs, they will have the same memory contents and will be executing the same instructions.
34
+
35
+
### Pattern matching in `for`
36
+
37
+
Whatever you put between `for` and `in` is treated as a [pattern](pattern-matching), meaning (for example) that the item can be destructured inline:
17
38
18
39
```roc
19
40
var $total = 0
@@ -22,7 +43,7 @@ for (x, y) in [(1, 2), (3, 4), (5, 6)] {
22
43
}
23
44
```
24
45
25
-
Use `_` if you don't want to name the item:
46
+
As usual, you can nest patterns as much as you like, and can use `_` if you don't want to name a pattern:
26
47
27
48
```roc
28
49
var $count = 0
@@ -31,7 +52,16 @@ for _ in items {
31
52
}
32
53
```
33
54
34
-
A loop body only includes statements; it does not have a final expression. The loop itself evaluates to `{}`.
55
+
Just like with [assignments](statements#assignments), the pattern you use here must be [exhaustive](pattern-matching#exhaustiveness). For example, the following would give an exhaustiveness error because the loop body couldn't know what value to use for `amount_to_add` if the item was ever `Err` at runtime:
56
+
57
+
```roc
58
+
var $count = 0
59
+
for Ok(amount_to_add) in items {
60
+
$count = $count + amount_to_add
61
+
}
62
+
```
63
+
64
+
If you can't write an exhaustive pattern-match, you can name the entire iterator item and then use [`match`](pattern-matching#match) on it inside the loop body.
0 commit comments