Thread overview
How to convert a chunks result to a two-dimensional array
Jan 15, 2022
forkit
Jan 15, 2022
Paul Backus
Jan 15, 2022
forkit
Jan 15, 2022
forkit
Jan 15, 2022
Salih Dincer
Jan 15, 2022
H. S. Teoh
January 15, 2022
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 [][]
}
// ---
January 15, 2022
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;
January 15, 2022
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)
January 14, 2022
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.
January 14, 2022

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):

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

-Steve

January 15, 2022
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;

January 15, 2022

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