Skip to content

Commit 232cd4b

Browse files
committed
added comments
1 parent 12e6a15 commit 232cd4b

3 files changed

Lines changed: 33 additions & 23 deletions

File tree

docs/Loops/iteration.md

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,35 @@ This page documents all user-facing iteration helpers in Pythonic, including ran
99

1010
## Range & Views
1111

12-
| Function / Macro | Description | Example |
13-
| -------------------------------- | ------------------------------ | ------------------------------------------------------ |
14-
| `range(end)` | Range from 0 to end-1 | `for_range(i, 10) { ... }` |
15-
| `range(start, end)` | Range from start to end-1 | `for_range(i, 1, 10) { ... }` |
16-
| `range(start, end, step)` | Range with explicit step | `for_range(i, 10, 0, -1) { ... }` |
17-
| `views::take_n(r, n)` | View of first n elements | `for (auto x : views::take_n(lst, 5)) { ... }` |
18-
| `views::drop_n(r, n)` | View dropping first n elements | `for (auto x : views::drop_n(lst, 2)) { ... }` |
19-
| `views::filter_view(r, pred)` | Filtered view by predicate | `for (auto x : views::filter_view(lst, pred)) { ... }` |
20-
| `views::transform_view(r, func)` | Transformed view by function | `for (auto x : views::transform_view(lst, f)) { ... }` |
21-
| `views::reverse_view(r)` | Reverse view (lazy, no copy) | `for (auto x : views::reverse_view(lst)) { ... }` |
22-
| `views::iota_view(start, end)` | Integer range view | `for (auto i : views::iota_view(0, 5)) { ... }` |
12+
| Function / Macro | Description | Example |
13+
| -------------------------------- | ---------------------------------------------------------------------- | ------------------------------------------------------ |
14+
| `range(end)` | Range from 0 to end-1 | `for_range(i, 10) { ... }` |
15+
| `range(start, end)` | Range from start to end-1 | `for_range(i, 1, 10) { ... }` |
16+
| `range(start, end, step)` | Range with explicit step | `for_range(i, 10, 0, -1) { ... }` |
17+
| `views::take_n(r, n)` | View of first n elements | `for (auto x : views::take_n(lst, 5)) { ... }` |
18+
| `views::drop_n(r, n)` | View dropping first n elements | `for (auto x : views::drop_n(lst, 2)) { ... }` |
19+
| `views::filter_view(r, pred)` | Filtered view by predicate | `for (auto x : views::filter_view(lst, pred)) { ... }` |
20+
| `views::transform_view(r, func)` | Transformed view by function | `for (auto x : views::transform_view(lst, f)) { ... }` |
21+
| `views::reverse_view(r)` | Reverse view (lazy, no copy, only for std containers with rbegin/rend) | _Not supported for pythonic::vars::var or list_ |
22+
| `views::iota_view(start, end)` | Integer range view | `for (auto i : views::iota_view(0, 5)) { ... }` |
2323

2424
---
2525

2626
## Enumerate & Zip
2727

28-
| Function / Macro | Description | Example |
29-
| ------------------------------- | ----------------------- | -------------------------------------------------------- |
30-
| `enumerate(container, start=0)` | Index/value pairs | `for_enumerate(i, x, lst) { ... }` |
31-
| `views::enumerate_view(r)` | Enumerated view (C++20) | `for (auto [i, x] : views::enumerate_view(lst)) { ... }` |
32-
| `zip(a, b, ...)` | Zip multiple containers | `for (auto [x, y] : zip(lst1, lst2)) { ... }` |
28+
| Function / Macro | Description | Example |
29+
| ------------------------------- | ------------------------------------------------------------------------ | -------------------------------------------------------------------------------- |
30+
| `enumerate(container, start=0)` | Index/value pairs (not for pythonic::vars::var or dynamic list) | `for_enumerate(i, x, lst) { ... }` (not for pythonic::vars::var or dynamic list) |
31+
| `views::enumerate_view(r)` | Enumerated view (C++20, works with pythonic::vars::var and dynamic list) | `for (auto [i, x] : views::enumerate_view(lst)) { ... }` |
32+
| `zip(a, b, ...)` | Zip multiple containers | `for (auto [x, y] : zip(lst1, lst2)) { ... }` |
3333

3434
---
3535

3636
## Reversed
3737

38-
| Function / Macro | Description | Example |
39-
| ------------------------ | ---------------------------- | ------------------------------------------------- |
40-
| `reversed(container)` | Reverse iteration | `for (auto x : reversed(lst)) { ... }` |
41-
| `views::reverse_view(r)` | Reverse view (lazy, no copy) | `for (auto x : views::reverse_view(lst)) { ... }` |
38+
| Function / Macro | Description | Example |
39+
| --------------------- | ------------------------------------------------------------ | ----------------------------------------------- |
40+
| `reversed(container)` | Reverse iteration (only for std containers with rbegin/rend) | _Not supported for pythonic::vars::var or list_ |
4241

4342
---
4443

@@ -80,13 +79,16 @@ for_range(i, 1, 5) { print(i); } // 1 2 3 4
8079
for_range(i, 10, 0, -2) { print(i); } // 10 8 6 4 2
8180

8281
// --- Enumerate ---
83-
for_enumerate(i, x, list(10, 20, 30)) { print(i, x); }
82+
// NOTE: for_enumerate macro is not compatible with pythonic::vars::var or dynamic list. Use enumerate_view for those.
83+
for_enumerate(i, x, std::vector<int>{10, 20, 30}) { print(i, x); }
84+
for (auto [i, x] : views::enumerate_view(list(10, 20, 30))) { print(i, x); }
8485

8586
// --- Zip ---
8687
for (auto [x, y] : zip(list(1,2,3), list("a","b","c"))) { print(x, y); }
8788

8889
// --- Reversed ---
89-
for (auto x : reversed(list(1,2,3))) { print(x); }
90+
// Not supported for pythonic::vars::var or list. Use slicing with negative step for reverse iteration:
91+
for (auto x : list(1,2,3).slice(var(), var(), -1)) { print(x); } // 3 2 1
9092

9193
// --- Loop Macros ---
9294
for_each(x, list(1,2,3)) { print(x); }
@@ -107,7 +109,7 @@ print(all(list(1,1,1)));
107109
108110
## Notes
109111
110-
- All iteration helpers work with standard containers, Pythonic containers, and C++20 ranges.
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.
111113
- Loop macros provide Python-like syntax for common iteration patterns.
112114
- Views are lazy and efficient; use them for large data or pipelines.
113115
- Utility functions operate on any iterable, including ranges and views.

include/pythonic/pythonic.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ namespace Pythonic
2222
using namespace pythonic::fast;
2323
using namespace pythonic::overflow;
2424
using namespace pythonic::error;
25+
using namespace pythonic::views;
2526
using namespace pythonic::viewer; // Graph viewer (enabled via PYTHONIC_ENABLE_GRAPH_VIEWER)
2627

2728
}

include/pythonic/pythonicLoop.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414

1515
namespace pythonic
1616
{
17+
// ============================================================================
18+
// TODOs for future improvements:
19+
// - enumerate and for_enumerate macro are not safe for dynamic containers (pythonic::vars::var/list). Consider a class-based or type-trait-based solution for future.
20+
// - reverse_view and reversed are not supported for pythonic::vars::var/list. Consider adding bidirectional iterator support or a custom reverse adaptor for dynamic containers.
21+
// - Refactor all loop macros (for_each, for_index, for_enumerate, etc.) to class-based or constexpr function-based implementations for better type safety and IDE support.
22+
// ============================================================================
23+
1724
// ============================================================================
1825
// C++20 Concepts for Pythonic Containers and Iterables
1926
// ============================================================================

0 commit comments

Comments
 (0)