digitalmars.D.learn - How can I make a nested array and flatten it at run time in D?
- Philos Kim (7/7) Mar 07 2019 I want to make a nested array and flatten it at run-time like
- James Blachly (4/17) Mar 07 2019 There are simpler ways, but looking at the below Rosetta Code link can
- H. S. Teoh (11/16) Mar 07 2019 You can't write it this way because the nested array has a different
I want to make a nested array and flatten it at run-time like this. auto nestedArray = [1, 2, [3, 4], 5]; auto flattenedArray = myFun(nestedArray); writeln(flattenedArray); // => [1, 2, 3, 4, 5] How can I do this in D? Please help me out!
Mar 07 2019
On 3/7/19 8:00 PM, Philos Kim wrote:I want to make a nested array and flatten it at run-time like this. auto nestedArray = [1, 2, [3, 4], 5]; auto flattenedArray = myFun(nestedArray); writeln(flattenedArray); // => [1, 2, 3, 4, 5] How can I do this in D? Please help me out!There are simpler ways, but looking at the below Rosetta Code link can be very instructive: https://rosettacode.org/wiki/Flatten_a_list
Mar 07 2019
On Fri, Mar 08, 2019 at 01:00:43AM +0000, Philos Kim via Digitalmars-d-learn wrote:I want to make a nested array and flatten it at run-time like this. auto nestedArray = [1, 2, [3, 4], 5];You can't write it this way because the nested array has a different type from the other elements. You have to write this as: auto nestedArray = [[1], [2], [3, 4], [5]];auto flattenedArray = myFun(nestedArray);Use std.algorithm.iteration.joiner: flattenedArray = nestedArray.joiner.array; Or simpler, use std.array.join: flattenedArray = nestedArray.join;writeln(flattenedArray); // => [1, 2, 3, 4, 5]T -- People say I'm indecisive, but I'm not sure about that. -- YHL, CONLANG
Mar 07 2019