r/ProgrammingLanguages • u/Savings-Debt-5383 • Jan 05 '26
Iterator fusion similar to Rust's — are there other languages that really do this, and what enables it?
While trying to learn Rust, and while reading about iterators, I came across the following definition, which really caught my attention:
Iterator fusion is a compilation technique in which a chain of iterator adapters is combined into a single loop, eliminating intermediate iterators and producing code equivalent to an optimal hand-written implementation.
Digging a bit further, I learned that in Rust this means code like:
let sum: i32 = data
.iter()
.map(|x| x * 2)
.filter(|x| *x > 10)
.sum();can end up being equivalent (after optimization) to something like:
let mut sum = 0;
for x in data {
let y = x * 2;
if y > 10 {
sum += y;
}
}No temporary collections, no multiple passes — just a single tight loop.
This really stood out to me because, in the past, I tried implementing iterators in Go for a toy project. What I ended up with involved:
- temporary allocations,
- multiple loops over the data (even when one would be enough),
- and quite a bit of “voodoo” just to achieve laziness.
I’m sure my design wasn’t ideal, but I learned a lot from it.
What really surprised me here is the idea that iterator handling is largely resolved at compile time, rather than being a runtime library mechanism. That feels very different from what I’m used to, and honestly very appealing.
Coincidentally, I’m once again in the phase of designing yet another programming language (like the previous two attempts, there’s a good chance I’ll abandon it in six months 😄).
Reading that definition immediately made me think:
“If I were designing a language, I wouldn’t even know where to start to get something like this.”
So I have a few questions, mostly from a language-design and learning perspective:
Are there other languages that really offer this in the same sense?
Not just “the compiler might optimize it if you’re lucky” (looking at you, LINQ), but cases where programmers can reasonably expect and rely on this kind of transformation when writing idiomatic code.
What enables this kind of behavior?
From a language-design point of view:
- What kinds of design choices make iterator fusion possible?
- What choices make it hard or unrealistic?
This whole question really came from reading that one definition and thinking:
“Wow — this is a powerful idea, and I don’t even know where to begin implementing something like it.”