Alternate signs in a range
matovitch
camille.brugel at laposte.net
Sun Jan 26 05:11:46 PST 2014
On Sunday, 26 January 2014 at 13:06:38 UTC, bearophile wrote:
> 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
How would you do this ?
map!(a => (a.index & 1) ? a : -a)([1, 2]) ? (index field isn't
available of course)
More information about the Digitalmars-d-learn
mailing list