Why is std.algorithm so complicated to use?

Timon Gehr timon.gehr at gmx.ch
Tue Jul 10 05:59:32 PDT 2012


On 07/10/2012 02:50 PM, Nick Treleaven wrote:
> On 10/07/2012 12:37, Jacob Carlborg wrote:
>> The corresponding D version would be:
>>
>> auto a = [5, 3, 5, 6, 8].uniq.map!(x => x.to!(string)).array.sort.array;
>> writeln(a);
>>
>> I'm guessing that's three allocations. But that doesn't even work, it
>> prints:
>>
>> ["3", "5", "5", "6", "8"]
>
> uniq needs sorted input:
>
> auto r = [5, 3, 5, 6, 8].sort.uniq.map!(x => x.to!string);
> writeln(r);
>
> Tested with dmd 2.059.
> I think the above should be one allocation (except for the strings).
>
> Maybe uniq should require a SortedRange?
>
> Nick
>
>

uniq provides useful functionality even if the input is not sorted.


More information about the Digitalmars-d mailing list