www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to convert a chunks result to a two-dimensional array

reply forkit <forkit gmail.com> writes:
I want int[][] like this -> [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], 
[11, 12, 13, 14, 15]]

Any help will be appreciated.

note: to2Darray is not a valid statement ;-)


// ---
module test;

import std;

void main()
{
     int[][] arrayOfarrays = iota(1, 16).chunks(5).to2Darray; // 
how to convert this into [][]
}
// ---
Jan 14 2022
next sibling parent reply Paul Backus <snarwin gmail.com> writes:
On Saturday, 15 January 2022 at 01:49:14 UTC, forkit wrote:
 I want int[][] like this -> [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], 
 [11, 12, 13, 14, 15]]

 Any help will be appreciated.

 note: to2Darray is not a valid statement ;-)


 // ---
 module test;

 import std;

 void main()
 {
     int[][] arrayOfarrays = iota(1, 16).chunks(5).to2Darray; // 
 how to convert this into [][]
 }
 // ---
import std.algorithm: map; import std.array: array; int[][] arrayOfArrays = iota(1, 16).chunks(5).map!array.array;
Jan 14 2022
next sibling parent forkit <forkit gmail.com> writes:
On Saturday, 15 January 2022 at 02:41:08 UTC, Paul Backus wrote:
 import std.algorithm: map;
 import std.array: array;

 int[][] arrayOfArrays = iota(1, 16).chunks(5).map!array.array;
oh. thanks! also it seems I reposted the same question (didn't realise this one got posted)
Jan 14 2022
prev sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 1/14/22 9:41 PM, Paul Backus wrote:
 On Saturday, 15 January 2022 at 01:49:14 UTC, forkit wrote:
 I want int[][] like this -> [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 
 12, 13, 14, 15]]

 Any help will be appreciated.

 note: to2Darray is not a valid statement ;-)


 // ---
 module test;

 import std;

 void main()
 {
     int[][] arrayOfarrays = iota(1, 16).chunks(5).to2Darray; // how to 
 convert this into [][]
 }
 // ---
import std.algorithm: map; import std.array: array; int[][] arrayOfArrays = iota(1, 16).chunks(5).map!array.array;
Alternatively (with only one allocation for the int[] data): ```d int[][] arrayOfArrays = iota(1, 16).array.chunks(5).array; ``` -Steve
Jan 14 2022
parent reply forkit <forkit gmail.com> writes:
On Saturday, 15 January 2022 at 03:48:41 UTC, Steven 
Schveighoffer wrote:
 Alternatively (with only one allocation for the int[] data):

 ```d
 int[][] arrayOfArrays = iota(1, 16).array.chunks(5).array;
 ```

 -Steve
All answers were helpful ;-) But I like this one the best, because I find it both easier to remember, and easier to expand upon (i.e. for each extra [] just add another .chunks(val).array e.g int[][][] arrayOfArraysV4 = iota(1, 16).array.chunks(2).array.chunks(3).array; int[][][][] arrayOfArraysV5 = iota(1, 16).array.chunks(2).array.chunks(3).array.chunks(4).array;
Jan 14 2022
parent Salih Dincer <salihdb hotmail.com> writes:
On Saturday, 15 January 2022 at 06:43:05 UTC, forkit wrote:
 On Saturday, 15 January 2022 at 03:48:41 UTC, Steven 
 Schveighoffer wrote:
 int[][] arrayOfArrays = iota(1,16).
 array.chunks(5).array;

 -Steve
All answers were helpful ;-) But I like this one the best, because I find it both easier to remember, and easier to expand...
I loved it too. I think this is because chunks() is very functional. I should mention this in the Turkish Forum. Salih
Jan 14 2022
prev sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Sat, Jan 15, 2022 at 01:49:14AM +0000, forkit via Digitalmars-d-learn wrote:
[...]
     int[][] arrayOfarrays = iota(1, 16).chunks(5).to2Darray; // how to
 convert this into [][]
[...] auto arrayOfArrays = iota(1, 16).chunks(5).map!(r => r.array).array; T -- Winners never quit, quitters never win. But those who never quit AND never win are idiots.
Jan 14 2022