in在数组和集合中的不同表现

julia> missing in [1,2,3]
missing
但是如果是集合,结果是
julia> missing in Set([1,2,3])
false
为什么二者有这样不同的结果?

?in

Some collections follow a slightly different definition. For example, Sets check whether the item isequal to one of the elements. Dicts look for key=>value pairs, and the key is compared using isequal. To test for the presence of a key in a dictionary, use haskey or k in keys(dict). For these collections, the result is always a Bool and never missing.

1 个赞

正解,因为集合、和字典对数据的比较采用的isequal()函数来进行的,所以返回的结果一定是Bool,不可能是missing。谢谢你!!

因为Set{T}的实现是Dict{T, Nothing}
in的实现是多态的,当

in(x, s::Set) = haskey(s.dict, x)

返回值是bool没问题,但是in的实现还包括

function in(x, itr)
    anymissing = false
    for y in itr
        v = (y == x)
        if ismissing(v)
            anymissing = true
        elseif v
            return true
        end
    end
    return anymissing ? missing : false
end

看起来, 此处做了专门处理.
这个行为可能是为了更好地支持DataFrame