transversal sum
    John Colvin via Digitalmars-d-learn 
    digitalmars-d-learn at puremagic.com
       
    Thu Nov  6 14:18:26 PST 2014
    
    
  
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?
nasty inefficient solution, but might be ok if your ranges have 
cheap popFront:
import std.algorithm, std.range, std.stdio, std.array;
void main()
{
     auto M = 3;
     auto N = 10;
     //generate a RangeOfRanges for testing
     auto ror = iota(1, M+1)
                    .map!(l => iota(N)
                                   .map!(e => e * l^^2));
     auto rorFlat = ror.joiner;
     iota(N)
         .map!(i => rorFlat.save.drop(i).stride(N))
         .map!sum.writeln;
}
    
    
More information about the Digitalmars-d-learn
mailing list