Thread overview
inside out iterating
Aug 26
monkyyy
Aug 28
monkyyy
Aug 28
user1234
Aug 28
monkyyy
6 days ago
monkyyy
August 26

does anyone how a concept for insideout iterating

I have this:

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


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)

August 28

On Tuesday, 26 August 2025 at 20:04:36 UTC, monkyyy wrote:

>

does anyone how a concept for insideout iterating

I have this:

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


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)

#!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;
}
August 28

On Tuesday, 26 August 2025 at 20:04:36 UTC, monkyyy wrote:

>

[...]

can you write a unnittest such as ?

assert(insideout(a) == b);

that would be more clear for me.

August 28

On Thursday, 28 August 2025 at 19:15:47 UTC, user1234 wrote:

>

On Tuesday, 26 August 2025 at 20:04:36 UTC, monkyyy wrote:

>

[...]

can you write a unnittest such as ?

assert(insideout(a) == b);

that would be more clear for me.

unittest{
	int[] output;
	foreach(ref me,ref other;iota(10).insideout(3)){
		output~=me;
	}
	assert(output==[3, 4, 5, 6, 7, 8, 9, 2, 1, 0]);
}
6 days ago

On Tuesday, 26 August 2025 at 20:04:36 UTC, monkyyy wrote:

>

does anyone how a concept for insideout iterating

I have this:

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


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)

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;
}