inside out iterating

monkyyy crazymonkyyy at gmail.com
Mon Sep 1 01:06:00 UTC 2025


On Tuesday, 26 August 2025 at 20:04:36 UTC, monkyyy wrote:
> does anyone how a concept for insideout iterating
>
> I have this:
> ```d
> struct cursor(T){
>     T[] data; alias data this;
>     int index;
>     ref opIndex(int i){
>         return data[min($-1,max(0,i+index))];
>     }
>     auto forward(){
>         struct range{
>             T[] data;
>             int index;
>             ref T front()=>data[0];
>             void popFront(){
>                 data=data[1..$];
>                 index++;
>             }
>             bool empty()=>data.length==0;
>         }
>         return range(data[index..$],0);
>     }
>     auto back(){
>         struct range{
>             T[] data;
>             int index;
>             ref T front()=>data[$-1];
>             void popFront(){
>                 data=data[0..$-1];
>                 index--;
>             }
>             bool empty()=>data.length==0;
>         }
>         return range(data[0..index],-1);
>     }
>     auto forwardthenback()=>chain(forward,back);
> }
> ```
> and Im using [index-sign(index)] to access the reliant other 
> member surely theres a better way
>
> ---
> ```d
> foreach(ref me,ref other;[1,2,3,4,5].???(2)){
>   writeln(me,','other);
> }
> ```
>
> ```
> 3,3
> 4,3
> 5,4
> 2,3
> 1,2
> ```
> (reminder tuples and refness dont actaully work, and im 
> modifying the center out)

```d
import std;

auto insideoutcounter(int i,int j){
     return chain(
         zip(repeat(0),iota(i,i+1)),
         zip(repeat(1),iota(i+1,j)),
         zip(repeat(-1),iota(0,i).retro));
}
unittest{
     insideoutcounter(3,10).each!writeln;
}
```



More information about the Digitalmars-d-learn mailing list