confused with data types

Adam D. Ruppe destructionator at gmail.com
Sat Feb 17 16:56:32 UTC 2018


On Saturday, 17 February 2018 at 16:12:48 UTC, thorstein wrote:
> public double[][] skalar_m_2d(double[][] array, double skalar)
> {
>   return array.map!(b => b[].map!(c => c * skalar));
> }

It'd probably be better to just do that in-place...

foreach(row; array)
   row[] *= skalar;
return array;

Note that'd overwrite the existing data though. But the compiler 
could more aggressively optimize that than the individual maps.

> How can I get the result as double[][] ???


But if you do need the double[][] type exactly as well as a new 
copy instead of editing the one you have, you can:

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


The std.array.array function copies the return value of functions 
like `map` into a new array. It is called twice here because 
double[][] is an array of arrays.

(the word "array" is used way too much there lol)


The reason map doesn't do this automatically btw is because 
copying the arrays can be somewhat expensive, so it doesn't force 
you to do work you don't often need.


More information about the Digitalmars-d-learn mailing list