Sorting after map

Tomek Sowiński just at ask.me
Sat Oct 23 04:26:11 PDT 2010


Dnia 20-10-2010 o 08:26:12 clueless <invalid at email.address> napisał(a):

> Hi. I want to have a sorted result of map-function, that is (in  
> pseudocode):
>
> sorted(map!(fun)(arr))
>
> How can I do that? I have tried something like:
>
> auto s = map!(fun)(arr);
> //sort(s);
> //sort(s[]);
> //sort(s.dup);
> writeln(s);
>
> but without success.

Map is a range of computed values, so you can't assign elements to it. Use  
an external index:

auto m = map!"a*a"([9,1,3,2]);
auto idx = new uint[](m.length);
makeIndex(m, idx);
foreach(i; idx)
     writeln(m[i]);

Prints:

1
4
9
81

One problem: makeIndex() doesn't return a SortedRange. Yet.
http://d.puremagic.com/issues/show_bug.cgi?id=5106
But you can still make a SortedRange of your own that accounts for  
dereferencing (see bug description).

-- 
Tomek


More information about the Digitalmars-d-learn mailing list