www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - inside out iterating

reply monkyyy <crazymonkyyy gmail.com> writes:
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)
Aug 26
next sibling parent monkyyy <crazymonkyyy gmail.com> writes:
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 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; } ```
Aug 28
prev sibling next sibling parent reply user1234 <user1234 12.de> writes:
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.
Aug 28
parent monkyyy <crazymonkyyy gmail.com> writes:
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.
```d 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]); } ```
Aug 28
prev sibling parent monkyyy <crazymonkyyy gmail.com> writes:
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; } ```
Aug 31