On Sunday, 18 February 2018 at 13:08:09 UTC, thorstein wrote:
On Sunday, 18 February 2018 at 12:51:04 UTC, thorstein wrote:
// 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.
Q#1: i would guess solution 1 would be the fastest and solution 2
the slowest.
Q#2: you get a error because each returns void.
Q#3: you cant use ufcs with static imports, you have to call the
function normaly:
static import algo = std.algorithm;
algo.each!((a) => algo.each!((b) => b[] *= skalar)(a))(arr);