Best syntax for a diagonal and vertical slice

Timon Gehr via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jul 24 10:05:07 PDT 2017


On 22.07.2017 22:55, kerdemdemir wrote:
> We have awesome way for creating slices like:
> 
>      a = new int[5];
>      int[] b = a[0..2];
> 
> But what about if I have 2D array and I don't want to go vertical. 
> Something like :
> 
> int[3][3] matrix = [
>      [ 1, 2, 3 ],
>      [ 4, 5, 6 ],
>      [ 7, 8, 9 ]
> ];
> 
> I believe I can use std.range function "RoundRobin"(or maybe it won't 
> work with 2D array directly) for having a good looking vertical slice 
> which will have 1,4,7 or 2,5,8 or 3,6,9 in my example above.
> 
> And what if I want to go diagonal like 1,5,9 or 3,5,7 in the example 
> above. Is there a good solution in std without using for loops?
> 
> I have one more requirement for fulfilling the task that I working on. 
> This slices do not have to be the same size as the array. For example in 
> the example above slice size could have 2 instead of 3. In this case I 
> need to have slices like 1,5;2,6;4,8;5,9 ... and so on for diagonal case.
> 
> Erdem
> 
> Ps: Converting the 2D array to 1D array is possible in my case.


horizontal: matrix[i][j..j+k]
vertical:   matrix[i..i+k].map!(x=>x[j])
diagonal 1: iota(k).map!(x=>matrix[i+x][j+x])
diagonal 2: iota(k).map!(x=>matrix[i+x][j-x])


More information about the Digitalmars-d-learn mailing list