Why is std.algorithm so complicated to use?

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Tue Jul 10 07:54:52 PDT 2012


On 7/10/12 10:26 AM, Andrei Alexandrescu wrote:
> On 7/10/12 8:50 AM, 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?
>
> Yes, please file a bug. Thanks!
>
> Andrei

Actually I take that back. One may use uniq to remove duplicates in 
unsorted ranges.

Andrei



More information about the Digitalmars-d mailing list