Alternate signs in a range

bearophile bearophileHUGS at lycos.com
Sun Jan 26 05:06:37 PST 2014


matovitch:

> I got a problem which I'm sure can be solved by a smart one 
> liner. I would like to obtain the term by term product of a 
> given range by the infinite cyclic range cycle([1,-1]). How can 
> I do that ?

void main() {
     import std.stdio, std.range, std.algorithm;
     auto r = [10, 20, 30, 40, 50];
     auto p = cycle([1, -1]);
     auto result = r.zip(p).map!(rp => rp[0] * rp[1]);
     result.writeln;
}


But it's easy to avoid the multiplication and replace it with a 
conditional neg.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list