How to flatten N-dimensional array?

Pavel Shkadzko p.shkadzko at gmail.com
Sun May 24 11:40:34 UTC 2020


On Sunday, 24 May 2020 at 11:21:00 UTC, 9il wrote:
> On Saturday, 23 May 2020 at 18:15:32 UTC, Pavel Shkadzko wrote:
>> [...]
>
> If the common nd-array isn't jugged (a parallelotop), you can 
> use fuse function.
>
> ----------
> /+dub.sdl:
> dependency "mir-algorithm" version="~>3.8.12"
> +/
> import std.stdio: writeln;
> import mir.ndslice;
>
> void main() {
>     auto arr =
>     [[[0, 1, 2, 3, 4],
>       [5, 6, 7, 8, 9]],
>      [[10, 11, 12, 13, 14],
>       [15, 16, 17, 18, 19]],
>      [[20, 21, 22, 23, 24],
>       [25, 26, 27, 28, 29]]];
>     auto flatten = arr.fuse.field;
>
>     static assert(is(typeof(flatten) == int[]));
>    	assert(flatten == 30.iota);
> }
> ----------
>
> It performs exactly one allocation.

Yep, I know about Mir fuse. I was more wondering about the 
absence of flatten in Phobos. But on dlang forum some people 
claimed that when it comes to anything multidimensional, use Mir.


More information about the Digitalmars-d-learn mailing list