transversal sum
Artur Skawina via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Nov 6 13:09:02 PST 2014
On 11/06/14 18:32, bearophile via Digitalmars-d-learn wrote:
> Marc Schütz:
>
>> We'd need something taking and returning a RoR that "mirrors" them diagonally. Then we could simply apply `map!(r => r.sum)` on the result.
>
> A simple solution is to create a row of values, and then sum them correctly while you scan the rows.
The simplest solution is probably something like:
auto transversal_sum(FR)(FR rr) {
static struct TS {
FR rr;
bool empty() @property const { return rr.front.empty; }
auto front() @property {
import std.algorithm;
return reduce!((a, b)=>a+b.front)(rr.front.front.init, rr);
}
void popFront() { foreach (ref r; rr) r.popFront(); }
}
return TS(rr);
}
but I think OP wanted a ready-made phobos solution, w/o all the
range boilerplate...
artur
More information about the Digitalmars-d-learn
mailing list