Alternate signs in a range

matovitch camille.brugel at laposte.net
Sun Jan 26 05:24:04 PST 2014


On Sunday, 26 January 2014 at 13:21:24 UTC, Stanislav Blinov 
wrote:
>>> 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)
>
> import std.range;
> import std.algorithm;
> import std.stdio;
>
> void main() {
>
> 	auto myRange = iota(0,10);
>
> 	auto index = sequence!((a,n) => a[0]+n)(1);
> 	auto result = myRange.zip(index).map!(a => a[1] & 1 ? a[0] : 
> -a[0]);
> 	result.writeln;
> }

Zipping an index array is uglier in my opinion... ;-)



More information about the Digitalmars-d-learn mailing list