Collapsing n-dimensional array to linear (1 dimensional)

abad via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jan 25 00:31:14 PST 2016


On Monday, 25 January 2016 at 02:27:57 UTC, Solomon E wrote:
> On Saturday, 23 January 2016 at 07:57:55 UTC, Ali Çehreli wrote:
>>
>> auto collapse(R)(R r)
>>         if (isArray!R) {
>>     return r.joiner.collapse.joiner;
>> }
>>
>> auto collapse(R)(R r)
>>         if (!isArray!R) {
>>     return r;
>> }
>>
>
> Ali, that code only passed the one test it had for collapsing a 
> three level array. It wouldn't collapse arrays of other numbers 
> of levels. It wasn't recursing as appeared to be intended.
>
> Is the following code better D? (I don't know because I'm still 
> learning D, so I'd like to be corrected if the comments in my 
> code are inaccurate or misleading.)
>
> (See https://issues.dlang.org/show_bug.cgi?id=12062 for where I 
> got the idea that `flatten` should be defined to mutate by 
> reference. A comment there suggests to use 
> std.experimental.ndslice and byElement for that, but ndlslice 
> doesn't seem to be in the library anymore.)
>

I will give this a try later.

Ruby's Array class includes this sort method for flattening and 
for me it was surprisingly useful, for instance when it was 
necessary to write the array to file.

I will also take a look at ndslice and see how intuitive its 
approach for achieving this would be.



More information about the Digitalmars-d-learn mailing list