Alternate signs in a range
    Stanislav Blinov 
    stanislav.blinov at gmail.com
       
    Sun Jan 26 05:21:23 PST 2014
    
    
  
>> 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;
}
    
    
More information about the Digitalmars-d-learn
mailing list