inside out iterating

monkyyy crazymonkyyy at gmail.com
Thu Aug 28 18:42:11 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
#!opend -unittest -main -run app.d
import std;
auto insideout(R,I)(R r,I i){
	struct foreach_{
		R r;
		I i;
		alias E=typeof(r[i]);
		int opApply(int delegate(ref E a,ref E b) dg){
			int result=dg(r[i],r[i]);
			I j=i+1;
			if(result>1) {goto exit;}
			while(j<r.length){
				result=dg(r[j],r[j-1]);
				if(result){
					if(result==1){break;}
					goto exit;
				}
				j++;
			}
			j=i-1;
			while(j>=0){
				result=dg(r[j],r[j+1]);
				if(result){
					break;
				}
				j--;
			}
			exit:return result;
		}
	}
	return foreach_(r,i);
}

unittest{
	lable: foreach(ref me,ref other;[1,2,3,4,5,6,7,8].insideout(2)){
		writeln(me,',',other);
		if(me==5){break;}
	}
	"end".writeln;
}
```


More information about the Digitalmars-d-learn mailing list