Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add <(::Tuple, ::Tuple) implementing three-valued logic #25099

Merged
merged 1 commit into from
Dec 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions base/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,20 @@ hash(x::Tuple{Any,}, h::UInt) = hash(x[1], hash((), h))
hash(x::Tuple{Any,Any}, h::UInt) = hash(x[1], hash(x[2], hash((), h)))
hash(x::Tuple, h::UInt) = hash(x[1], hash(x[2], hash(tail(tail(x)), h)))

function <(t1::Tuple, t2::Tuple)
n1, n2 = length(t1), length(t2)
for i = 1:min(n1, n2)
a, b = t1[i], t2[i]
eq = (a == b)
if ismissing(eq)
return missing
elseif !eq
return a < b
end
end
return n1 < n2
end

function isless(t1::Tuple, t2::Tuple)
n1, n2 = length(t1), length(t2)
for i = 1:min(n1, n2)
Expand Down
24 changes: 24 additions & 0 deletions test/missing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,30 @@ end
@test (missing, 1) != (missing, 2)
end

@testset "< and isless on tuples" begin
@test ismissing((1, missing) < (1, 3))
@test ismissing((1, missing) < (1, missing))
@test ismissing((missing, 1) < (missing, 2))
@test ismissing((1, 2) < (1, missing))
@test ismissing((1, missing) < (1, 2))
@test ismissing((missing,) < (missing,))
@test ismissing((1,) < (missing,))
@test () < (missing,)
@test (1,) < (2, missing)
@test (1, missing,) < (2, missing)

@test !isless((1, missing), (1, 3))
@test !isless((1, missing), (1, missing))
@test isless((missing, 1), (missing, 2))
@test isless((1, 2), (1, missing))
@test !isless((1, missing), (1, 2))
@test !isless((missing,), (missing,))
@test isless((1,), (missing,))
@test isless((), (missing,))
@test isless((1,), (2, missing))
@test isless((1, missing,), (2, missing))
end

@testset "any & all" begin
@test any([true, missing])
@test any(x -> x == 1, [1, missing])
Expand Down
7 changes: 6 additions & 1 deletion test/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ end
@test !==((1,2,3), (1,2,4))
@test !==((1,2,3), (1,2))

@test (1,2) < (1,3)
@test (1,) < (1,2)
@test !((1,2) < (1,2))
@test (2,1) > (1,2)

@test isless((1,2), (1,3))
@test isless((1,), (1,2))
@test !isless((1,2), (1,2))
Expand Down Expand Up @@ -358,4 +363,4 @@ end

@testset "issue 24707" begin
@test eltype(Tuple{Vararg{T}} where T<:Integer) >: Integer
end
end