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
This page documents all user-facing iteration helpers in Pythonic, including range, enumerate, zip, reversed, loop macros, and utility functions, with concise examples.
6
+
This page documents all user-facing iteration helpers in Pythonic, including range, enumerate, zip, reversed, loop helpers, and utility functions, with concise examples.
7
+
8
+
---
9
+
10
+
## Mutation Detection (Python-like Safety)
11
+
12
+
Starting with the latest version, all `var` container types (`list`, `set`, `dict`, `ordered_set`, `ordered_dict`, `string`) now have **Python-like mutation detection during iteration**. If you modify a container while iterating over it, a `PythonicRuntimeError` will be thrown.
| Mutation during iteration throws | Modifying a container (append, clear, remove, etc.) while iterating throws |`for (auto x : lst) { lst.append(1); } // THROWS!`|
17
+
| Normal iteration is safe | Non-mutating iteration works as expected |`for (auto x : lst) { print(x); } // OK`|
18
+
| Iteration version tracking | Each container tracks a version counter that increments on mutation |`lst.get_version()`|
19
+
20
+
**Note:** This safety applies to all iteration methods: range-based for, `enumerate()`, `indexed()`, `each()`, `each_indexed()`, and the deprecated macros.
@@ -41,15 +56,32 @@ This page documents all user-facing iteration helpers in Pythonic, including ran
41
56
42
57
---
43
58
44
-
## Loop Macros
59
+
## Class-Based Loop Helpers (Recommended)
60
+
61
+
These are type-safe, template-based alternatives to macros. They provide better IDE support, compile-time error checking, and integrate with mutation detection.
|`each(container, func)`| Apply function to each element (early exit if func returns false) |`each(lst, [](auto& x) { print(x); });`|
66
+
|`each_indexed(container, func)`| Apply function with index (early exit if func returns false) |`each_indexed(lst, [](size_t i, auto& x) { print(i, x); });`|
// For var/list, use slicing with negative step instead:
169
+
for (auto x : lst.slice(var(), var(), -1)) { print(x); } // 30 20 10
97
170
98
171
// --- Utility Functions ---
99
-
print(len(list(1,2,3)));
100
-
print(to_list(range(5)));
101
-
print(sum(list(1,2,3)));
102
-
print(min(list(1,2,3)));
103
-
print(max(list(1,2,3)));
104
-
print(any(list(0,1,0)));
105
-
print(all(list(1,1,1)));
172
+
print(len(list(1,2,3))); // 3
173
+
print(to_list(range(5))); // [0,1,2,3,4]
174
+
print(sum(list(1,2,3))); // 6
175
+
print(min(list(1,2,3))); // 1
176
+
print(max(list(1,2,3))); // 3
177
+
print(any(list(0,1,0))); // true
178
+
print(all(list(1,1,1))); // true
106
179
```
107
180
108
181
---
109
182
110
183
## Notes
111
184
112
-
- All iteration helpers work with standard containers, Pythonic containers, and C++20 ranges, except reverse iteration for pythonic::vars::var/list (use .slice with negative step instead). -**Negative indices and negative step are supported in pythonic::vars::var/list slicing:** - `lst[-1]` gives the last element, `lst.slice(var(), var(), -1)` reverses the list, etc.
113
-
- Loop macros provide Python-like syntax for common iteration patterns.
185
+
- **Mutation detection is now enabled** for all `var` container types. Modifying during iteration throws.
186
+
- **Prefer class-based helpers** (`each()`, `each_indexed()`, `indexed()`, `times()`) over macros for type safety and IDE support.
187
+
- **Negative indices and negative step** are supported in `var`/list slicing for reverse iteration.
188
+
- Loop macros are deprecated but kept for backward compatibility.
114
189
- Views are lazy and efficient; use them for large data or pipelines.
115
190
- Utility functions operate on any iterable, including ranges and views.
0 commit comments