How to define my own "unique" function?

Hello!

Suppose I have a list of tuples given as:

ps
17-element Vector{Tuple{Int64, Int64, Float64}}:
 (6, 1, 0.1943167542320367)
 (6, 7, 0.3711794598818039)
 (6, 3, 0.24015959913323964)
 (3, 1, 0.10660277812749026)
 (3, 7, 0.15676846198395794)
 (3, 6, 0.24015959913323964)
 (1, 7, 0.18199430878135997)
 (1, 6, 0.1943167542320367)
 (7, 6, 0.3711794598818039)
 (1, 3, 0.10660277812749026)
 (7, 3, 0.15676846198395794)
 (4, 8, 0.18129600615558053)
 (8, 4, 0.18129600615558053)
 (8, 9, 0.12927343763884552)
 (9, 8, 0.12927343763884552)
 (9, 5, 0.17768623230197966)
 (5, 9, 0.17768623230197966)

Each element consists of (i,j,d), where i and j are indices and d is the distance between them. How would I then go about removing “repeat entries”, which is for example at the bottom:

 (9, 5, 0.17768623230197966)
 (5, 9, 0.17768623230197966)

Where I have i = j, j = i.

So basically my question is, how do I filter this list of tuples to remove cases in which i and j are “flipped”?

What I should end up with is:

ps
X-element Vector{Tuple{Int64, Int64, Float64}}:
 (6, 1, 0.1943167542320367)  #or (1, 6, 0.1943167542320367)
 (6, 7, 0.3711794598818039)  #or (7, 6, 0.3711794598818039)
 (6, 3, 0.24015959913323964) #or (3, 6, 0.24015959913323964)
 (3, 1, 0.10660277812749026) #or (1, 3, 0.10660277812749026)
 (3, 7, 0.15676846198395794) #or (7, 3, 0.15676846198395794)
 (1, 7, 0.18199430878135997)
 (4, 8, 0.18129600615558053) #or  (8, 4, 0.18129600615558053)
 (8, 9, 0.12927343763884552) #or  (9, 8, 0.12927343763884552)
 (9, 5, 0.17768623230197966) #or  (5, 9, 0.17768623230197966)

Kind regards

unique(e->(e[1]*e[2], e[1]+e[2]), a)
1 Like

Same idea:

unique(e->minmax(e[1],e[2]), a)

and assuming distance is the same when ids reversed:

unique(sort∘collect, a)

The second option is a bit slow and brittle (could err if distances are noisy or can be confused with ids), so use first.

3 Likes
unique(e->(abs(e[1]-e[2]), e[1]+e[2]), a)
unique(e->min(e[1],e[2])^2*max(e[1],e[2])^3, a)
unique(e->sort(first(e,2)), a)
1 Like