a = [1,2,3,4,5]
b = [4,5,6]
setdiff(a,b)
3-element Array{Int64,1}:
1
2
3
However this one doesn’t:
type X
a::Int
end
Base.broadcast(::typeof(==),x::X,y::X) = x.a == y.a
a = [X(1),X(2),X(4),X(5),X(6)]
b = [X(4),X(5),X(6)]
setdiff(a,b)
5-element Array{X,1}:
X(1)
X(2)
X(4)
X(5)
X(6)
I’m on julia 0.6.2. I think this is what you might want to do in your code instead of the broadcast thing.
import Base.==
==(x::X,y::X) = x.a == y.a
This gives you X(1) == X(1), not the broadcast function.
But this still cannot give you the setdiff result you want, at least on my julia v0.6.2. As the result is totally random on my julia 0.6.2. Sometime the result is a four element Array, sometime the result is a five element one.
I try to further dig into the issue. setdiff(a, b) leads to in(a_elem, bset) leads to haskey(bset.dict, a_elem), where bset = Set(b). And the following happens:
So I think this problem is related to the dictionary in julia 0.6.2. I may have seen some issues similar to this before and the solution to it, and I will try to search for that.
julia> m1 = [mtest(i) for i in 1:5]; m2 = [mtest(i) for i in 4:5]; setdiff(m1,m2)
5-element Array{mtest,1}:
mtest(1)
mtest(2)
mtest(3)
mtest(4)
mtest(5)
julia> i1 = [itest(i) for i in 1:5]; m2 = [itest(i) for i in 4:5]; setdiff(i1,i2)
3-element Array{itest,1}:
itest(1)
itest(2)
itest(3)