transversal sum

Justin Whear via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Nov 6 09:08:23 PST 2014


On Thu, 06 Nov 2014 16:57:48 +0000, Marc Schütz wrote:

> On Thursday, 6 November 2014 at 15:53:27 UTC, Jack Applegame wrote:
>> I have rectangular forward range of forward ranges (not arrays):
>> [
>>   [a11, a12, ... a1N],
>>   [a21, a22, ... a2N],
>>   ...
>>   [aM1, aM2, ... aMN]
>> ]
>>
>> I need lazy forward range:
>> [
>>  a11 + a21 + ... aM1,
>>  a12 + a22 + ... aM2,
>>  ...
>>  a1N + a2N + ... aMN
>> ]
>> Range of sum elements of every columns;
>>
>> M, N - runtime values;
>>
>> Is there a way to do this using only Phobos algorithms and range
>> functions?
> 
> Untested:
> 
>      import std.algorithm: map, sum;
>      auto rangeOfSums = rectangularRange.map!(r => r.sum);

This would sum along the wrong dimension.  I think the correct solution 
will
make use of std.range.frontTraversal, but it will be a bit more complex 
due to
needing to sum every column.  std.range.traversal would make it easy, but 
it
requires random access.


More information about the Digitalmars-d-learn mailing list