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
{{ message }}
This repository was archived by the owner on Dec 12, 2024. It is now read-only.
Copy file name to clipboardExpand all lines: pages/book/maps.mdx
+19-1Lines changed: 19 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -181,7 +181,25 @@ contract Example {
181
181
182
182
### Traverse over entries [#traverse]
183
183
184
-
At the moment Tact doesn't have a special syntax for iterating over maps. However, it's possible to use maps as a simple arrays if you define a `map<Int, v>{:tact}` with an [`Int{:tact}`][int] type for the keys and keep track of the number of items in the separate variable:
184
+
To iterate over map entries there is a [`foreach{:tact}`](/book/statements#foreach-loop) loop statement:
185
+
186
+
```tact
187
+
// Empty map
188
+
let fizz: map<Int, Int> = emptyMap();
189
+
190
+
// Setting a couple of values under different keys
191
+
fizz.set(42, 321);
192
+
fizz.set(7, 123);
193
+
194
+
// Iterating over in a sequential order: from the smallest keys to the biggest ones
195
+
foreach (key, value in fizz) {
196
+
dump(key); // will dump 7 on the first iteration, then 42 on the second
197
+
}
198
+
```
199
+
200
+
Read more about it: [`foreach{:tact}` loop in Book→Statements](/book/statements#foreach-loop).
201
+
202
+
Note, that it's also possible to use maps as simple arrays if you define a `map<Int, V>{:tact}` with an [`Int{:tact}`][int] type for the keys, any allowed `V` type for values and keep track of the number of items in the separate variable:
While loop continues executing the block of code as long as the given condition is `true{:tact}`.
152
+
The `while{:tact}` loop continues executing the block of code as long as the given condition is `true{:tact}`.
153
153
154
154
In the following example, the value of `x` is decremented by $1$ on each iteration, so the loop will run $10$ times:
155
155
@@ -173,8 +173,52 @@ do {
173
173
} until (x <= 0);
174
174
```
175
175
176
+
### `foreach`[#foreach-loop]
177
+
178
+
The `foreach{:tact}` loop operates on key-value pairs (entries) of [`map<K, V>{:tact}`](/book/maps) type in sequential order: from the smallest keys of the map to the biggest ones.
179
+
180
+
This loop executes a block of code for each entry in the given map, capturing the key and value on each iteration. This is handy when you don't know in advance how many items there is in the map or don't want to explicitly look for each of the entry using [`.set(){:tact}`](/book/maps#set)[method](/book/functions#extension-function) of maps.
181
+
182
+
Note, that the names of captured key and value pair on each iteration are arbitrary and can be any valid Tact identifier, provided that they're new to the current scope. The most common options are: `k` and `v`, or `key` and `value`.
183
+
184
+
In the following example, map `cells` has $4$ entries, so the loop will run $4$ times:
let s: Slice = value.beginParse(); // convert Cell to Slice
202
+
sum += s.loadUint(16); // sum the Slice values
203
+
}
204
+
dump(sum); // 1000
205
+
```
206
+
207
+
<Callouttype="warning"emoji="⚠️">
208
+
209
+
Note, that at the moment `foreach{:tact}` works only with explicitly provided map identifiers. That is, returning a map from a function and trying to iterate over its entries won't work:
210
+
211
+
```tact
212
+
foreach (k, v in emptyMap()) {
213
+
// ^ this will give a rather cryptic error message:
214
+
// Syntax error: expected ")"
215
+
}
216
+
```
217
+
218
+
</Callout>
219
+
176
220
<Callout>
177
221
178
-
For more loop examples see: [Loops in Tact-By-Example](https://tact-by-example.org/04-loops).
222
+
For additional loop examples see: [Loops in Tact-By-Example](https://tact-by-example.org/04-loops).
0 commit comments