Why is std.algorithm so complicated to use?

Nick Treleaven nospam at example.net
Tue Jul 10 05:50:31 PDT 2012


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




More information about the Digitalmars-d mailing list