Is there a function for this?

bauss jj_1337 at live.dk
Sat Oct 6 20:07:42 UTC 2018


On Saturday, 6 October 2018 at 15:35:39 UTC, Basile B wrote:
> On Saturday, 6 October 2018 at 13:56:32 UTC, bauss wrote:
>> On Saturday, 6 October 2018 at 13:35:38 UTC, Basile B wrote:
>>> On Saturday, 6 October 2018 at 13:17:22 UTC, bauss wrote:
>>>> Let's say you have a range with struct, but some of the 
>>>> struct are duplicates of each other.
>>>>
>>>> Is there a standard function in Phobos to remove duplicates?
>>>>
>>>> My first thought was "uniq", but it can't really do it like 
>>>> that, but it doesn't work.
>>>>
>>>> See: https://run.dlang.io/is/IcFEtw
>>>>
>>>> Is there another function in Phobos that perhaps would work?
>>>>
>>>> I can of course write my own function, but if there is a 
>>>> standard function that can do it, then I'd rather use that.
>>>
>>> see 
>>> https://www.programming-idioms.org/idiom/119/deduplicate-list.
>>
>> Did you even read my post? I stated I could already write the 
>> function myself, but if a standard function existed I'd rather 
>> use that.
>
> There are two solutions there. One shows how to use uniq, which 
> is why i posted this since you said that it doesn't work as you 
> expected. I didn't suggest you to write a full double loop 
> dedup. routine, i.e àla Golang. Unfortunately the site doesn't 
> allow to link the solutions of a particular language among 
> those who implement the idiom.

uniq will not work with, say a class and the class will require 
you to implement opCmp, which you can't always do for classes you 
don't have access to.

That's the problem here. It's easy enough without, but you cannot 
do it by ex. a property of a class. It will not work properly.

You can't even use .group and then .map because you need to use 
.sort and it requires opCmp again.

The whole problem is actually that they do not work with ranges 
that aren't sorted. Things like .group and .uniq should work 
without sorted ranges. You can't always expect a range to be 
sorted to perform such algorithms.



More information about the Digitalmars-d-learn mailing list