www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Regarding std.range.indexed

reply "bearophile" <bearophileHUGS lycos.com> writes:
Code like this doesn't compile, the swap doesn't work:


import std.range: indexed;
import std.algorithm: swap;
void main() {
     immutable source = [10, 20, 30];
     auto ind = indexed(source, [0, 1, 2]);
     swap(ind[0], ind[1]);
}


But isn't an important usage case of indexed() to shuffle and 
sort a view of an immutable array, like sorting a 
immutable(int)*[] ?

Bye,
bearophile
Jan 13 2014
parent reply "Jesse Phillips" <Jesse.K.Phillips+D gmail.com> writes:
On Tuesday, 14 January 2014 at 01:14:41 UTC, bearophile wrote:
 Code like this doesn't compile, the swap doesn't work:


 import std.range: indexed;
 import std.algorithm: swap;
 void main() {
     immutable source = [10, 20, 30];
     auto ind = indexed(source, [0, 1, 2]);
     swap(ind[0], ind[1]);
 }


 But isn't an important usage case of indexed() to shuffle and 
 sort a view of an immutable array, like sorting a 
 immutable(int)*[] ?

 Bye,
 bearophile
I thought it was to provide an index number during a foreach iteration. If you have an randomAccess range with assignable elements, why use indexed?
Jan 13 2014
parent "bearophile" <bearophileHUGS lycos.com> writes:
Jesse Phillips:

 I thought it was to provide an index number during a foreach 
 iteration.
That's the enumerate() range, that is still missing in Phobos.
 If you have an randomAccess range with assignable elements, why 
 use indexed?
But "source" is immutable, so its elements aren't assignable. Bye, bearophile
Jan 13 2014