digitalmars.D.learn - How to convert a chunks result to a two-dimensional array
- forkit (13/13) Jan 14 2022 I want int[][] like this -> [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10],
- Paul Backus (4/17) Jan 14 2022 import std.algorithm: map;
- forkit (4/7) Jan 14 2022 oh. thanks!
- Steven Schveighoffer (6/31) Jan 14 2022 Alternatively (with only one allocation for the int[] data):
- forkit (11/16) Jan 14 2022 All answers were helpful ;-)
- Salih Dincer (4/15) Jan 14 2022 I loved it too. I think this is because chunks() is very
- H. S. Teoh (7/9) Jan 14 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 [][] } // ---
Jan 14 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;
Jan 14 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)
Jan 14 2022
On 1/14/22 9:41 PM, Paul Backus wrote:On Saturday, 15 January 2022 at 01:49:14 UTC, forkit wrote:Alternatively (with only one allocation for the int[] data): ```d int[][] arrayOfArrays = iota(1, 16).array.chunks(5).array; ``` -SteveI 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
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; ``` -SteveAll 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
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:I loved it too. I think this is because chunks() is very functional. I should mention this in the Turkish Forum. Salihint[][] arrayOfArrays = iota(1,16). array.chunks(5).array; -SteveAll answers were helpful ;-) But I like this one the best, because I find it both easier to remember, and easier to expand...
Jan 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.
Jan 14 2022