Transposing a static array
maik klein via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Feb 19 18:26:56 PST 2016
On Saturday, 20 February 2016 at 02:22:12 UTC, Ali Çehreli wrote:
> On 02/19/2016 06:00 PM, maik klein wrote:
>> How would I transpose
>>
>> float[3][2]
>>
>> to
>>
>> float[2][3]
>>
>> with http://dlang.org/phobos/std_range.html#.transposed
>>
>>
>
> Because static arrays are not ranges, they must be used as
> slices with the help of []. The following code does the same
> thing in two different ways:
>
> import std.stdio;
> import std.range;
> import std.algorithm;
>
> void main() {
> float[3][2] arr = [ [1, 2, 3],
> [4, 5, 6] ];
>
> // Method A
> {
> float[][] arr2;
>
> foreach (ref a; arr) {
> arr2 ~= a[];
> }
>
> writeln(arr2.transposed);
> }
>
> // Method B
> {
> auto r = arr[].map!((ref a) => a[]).array.transposed;
> writeln(r);
> }
> }
>
> Ali
Your "Method B" is how I did it too but how do I convert it back
to a static array of float[2][3]?
More information about the Digitalmars-d-learn
mailing list