InputRange for data structure without order?

Ali Çehreli acehreli at yahoo.com
Wed Jun 4 20:34:30 UTC 2025


On 6/4/25 12:53 PM, monkyyy wrote:

 > Iota should mutate the underlining data and does.

There is no underlying data for some ranges like iota because they are 
generators. Yes, it's a complication that D uses the term "range" for 
generators as well.

 > Unjustifiably slices
 > are in the weird limbo,

My mental model of slices differ from many old-timer D people here. 
There is no limbo in my mental model: Slices are always a lightweight 
interface (i.e. the range) to contained elements.

What is missing in D language is that there is always a container of 
elements that the slice provides access to. The fact that the container 
(the array) not having a name causes the confusion. People necessarily 
say "add an element to the slice". That is not accurate in my mental 
model: The element is always added to the container, the slice is just 
an interface to it.

Yes, it's weird that an element accessor is capable of adding elements 
to the unnamed array. I accept my mental model despite this weirdness.

 > the autodecoding's popFront is raw mutation.

As the following example shows, there is no mutation to the contained 
elements though. Only the slice (the range) changes:

import std.range : popFront;

void main() {
     auto slice1 = [ 1, 10, 42 ];

     auto slice2 = slice1;
     slice2.popFront();

     assert(slice1 == [ 1, 10, 42 ]);    // <-- No mutation
}

 > I
 > think the some of the rng primitives also just mutate. Nullable is a
 > range for some God-Forsaken reason, it would have to mutate data.

I wasn't aware of that. I wonder whether someone thought it would 
improve generality? But that topic is beside the point here, which was 
the separation of containers and ranges.

Ali



More information about the Digitalmars-d-learn mailing list