confused with data types

thorstein torsten.lange at mail.de
Sun Feb 18 13:08:09 UTC 2018


On Sunday, 18 February 2018 at 12:51:04 UTC, thorstein wrote:
> Thank you for the very informative answers showing different 
> gears in D!
>
> However, there are still some details I'm struggling with:
>
> Assume some calculations on a very big numeric array 
> 'double[][][] arr'.
> Now we could choose 1 out of 3 different implementations:
> // Solution 1
> foreach(row; arr)
> { foreach(col; row)
>   { col[] *= skalar;
>   }
> }
> return arr;
>
> // Solution 2
> import std.array;
> return array(arr.map!(b => array(b[].map!(c => array(c[].map!(d 
> => d * skalar))))));
>
> // Solution 3
> import std.algorithm;
> arr.each!(a => a[].each!(b => b[] *= skalar));
> return arr;
>
> Q#1:
> Does the compiler optimizes all solutions equally strong or 
> does it prefer implementations like solution 1?
>
> Q#2:
> Solution 2 is a 1-liner but a bit harder to read. Why reducing 
> solution 3 to:
> return arr.each!(a => a[].each!(b => b[] *= skalar));
> gives a compile error? I do writeln() the function result.
>
> Q#3:
> If I can:
> static import std.array;
> return std.array.array(arr.map!(b => std.array.array(b[].map!(c 
> =>...
>
> How would I apply a similar version with 'static import 
> std.algorithm' to solution 3?
> static import std.algorithm;
> std.algorithm.arr.each!(a => a[]... //does obviously not work
>
> Thanks, thorstein

Sorry, Solution 2 should be:

import std.array;
return array(arr.map!(b => array(b[].map!(c => c[] *= skalar))));

and is as readable as Solution 3. However, Q#2 still remains.


More information about the Digitalmars-d-learn mailing list