code wiki / (root) / nx_iterator.nx

nx_iterator.nx

buildroot/runtime/nx_iterator.nx

12276 B375 linesdepth 3pulls 3 transitivereach 1 importersview sourcekind library
docsdependenciesstructsconstsfunctions

about

nx_iterator.nx -- composable iteration; kills the nested-loops bug class. User cardinal 2026-05-15: "how many loops within loops type bullshit do we create that then caused memory leaks and infinite bloat etc, isnt it better just to build the system so its bulletproof there where a loop can call another loop like a function but with better structure i feel like javascript c etc have all created a shit paradigm not a usability paradagim." THE STRUCTURAL FIX: first-class iterators. A pipeline of N transformations expresses as ONE while loop at the consumption end instead of N nested loops. Zero raw nesting; zero per-iteration allocations; clear ownership. Example transformation -- 5 stages, 1 loop: let it: *Iterator = nx_iter_range(0, 100) it = nx_iter_filter(it, NX_FILTER_EVEN, 0) it = nx_iter_map(it, NX_MAP_SQUARE, 0) it = nx_iter_take(it, 5) let v: *nx_int = (sys_mmap(NX_SIZEOF_NX_INT)) as *nx_int while nx_iter_next(it, v) == 1 { // v[0] = next squared even number, up to 5 of them } Same logic in nested-loop style would be 4 deep with per-level state, easy to leak. Iterator style: linear, audit-able, the substrate's Captain Moroni doctrine applied to iteration. NO-CLOSURE-NEEDED DESIGN: filter / map use the same sealed-enum predicate/op kinds as nx_array_ops.nx (NX_FILTER_* and NX_MAP_*). Substrate dispatches internally. ~80% coverage; the remaining 20% custom predicates wait for closure support (queued nxc2 work). CONSUMERS use NX_REDUCE_* op kinds from nx_array_ops.nx (SUM / PRODUCT / MIN / MAX / COUNT / AND / OR / XOR) for fold semantics. MEMORY: each iterator is a fixed-size struct (8 nx_int fields + 1 array pointer + 2 inner pointers = ~88 bytes). Per-pipeline allocation is O(stages) and bounded; no per-iteration allocations.

dependencies 3 imports · 1 importers

nx_syscalls.nx nx_tier.nx nx_array_ops.nx nx_iterator.nx nx_iterator_test.nx

imports: nx_syscalls.nxnx_tier.nxnx_array_ops.nx

imported by: nx_iterator_test.nx

structs

86struct Iterator

consts

57const NX_MAGIC_1024: i64 = 1024
61const NX_ITER_KIND_RANGE: nx_int = 0
62const NX_ITER_KIND_ARRAY: nx_int = 1
63const NX_ITER_KIND_FILTERED: nx_int = 2
64const NX_ITER_KIND_MAPPED: nx_int = 3
65const NX_ITER_KIND_CHAINED: nx_int = 4
66const NX_ITER_KIND_TAKE: nx_int = 5
67const NX_ITER_KIND_SKIP: nx_int = 6
68const NX_ITER_N_KINDS: nx_int = 7

functions

99func _iter_alloc() -> *Iterator
107func nx_iter_range(lo: nx_int, hi: nx_int) -> *Iterator
called by 1: main calls 1: _iter_alloc
115func nx_iter_array(arr: *nx_int, n: nx_int) -> *Iterator
called by 1: main calls 1: _iter_alloc
126func nx_iter_filter(source: *Iterator, pred: nx_int, param: nx_int) -> *Iterator
called by 1: main calls 1: _iter_alloc
135func nx_iter_map(source: *Iterator, op: nx_int, param: nx_int) -> *Iterator
called by 1: main calls 1: _iter_alloc
144func nx_iter_chain(a: *Iterator, b: *Iterator) -> *Iterator
called by 1: main calls 1: _iter_alloc
153func nx_iter_take(source: *Iterator, n: nx_int) -> *Iterator
called by 1: main calls 1: _iter_alloc
161func nx_iter_skip(source: *Iterator, n: nx_int) -> *Iterator
called by 1: main calls 1: _iter_alloc
175func _iter_filter_match(v: nx_int, pred: nx_int, param: nx_int) -> nx_int
called by 1: nx_iter_next
196func _iter_map_apply(v: nx_int, op: nx_int, param: nx_int) -> nx_int
called by 1: nx_iter_next
230func nx_iter_next(it: *Iterator, out: *nx_int) -> nx_int
302func nx_iter_count(it: *Iterator) -> nx_int
called by 1: main calls 2: sys_mmapnx_iter_next
312func nx_iter_reduce(it: *Iterator, op: nx_int) -> nx_int
344func nx_iter_sum(it: *Iterator) -> nx_int
called by 1: main calls 1: nx_iter_reduce
348func nx_iter_min(it: *Iterator) -> nx_int
called by 1: main calls 1: nx_iter_reduce
352func nx_iter_max(it: *Iterator) -> nx_int
called by 1: main calls 1: nx_iter_reduce
358func nx_iter_collect(it: *Iterator, out: *nx_int, max_out: nx_int) -> nx_int
called by 1: main calls 2: sys_mmapnx_iter_next
371func nx_iter_kind_is_valid(k: nx_int) -> nx_int
called by 1: main