Skip to content
This repository has been archived by the owner on May 4, 2019. It is now read-only.

Commit

Permalink
Merge pull request #141 from JuliaStats/nl/isless
Browse files Browse the repository at this point in the history
Add isless(::Nullable, ::Nullable) -> Bool
  • Loading branch information
davidagold authored Aug 30, 2016
2 parents a0d210d + b62fdfc commit 2e1fd1c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/operators.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## Lifted operators

importall Base.Operators
import Base: promote_op, abs, abs2, sqrt, cbrt, scalarmin, scalarmax
import Base: promote_op, abs, abs2, sqrt, cbrt, scalarmin, scalarmax, isless
using Compat: @functorize

# Methods adapted from Base Julia 0.5
Expand Down Expand Up @@ -85,7 +85,8 @@ end
if VERSION >= v"0.5.0-dev"
for op in (:+, :-, :*, :/, :&, :|, :<<, :>>, :(>>>),
:(==), :<, :>, :<=, :>=,
:scalarmin, :scalarmax)
:scalarmin, :scalarmax,
:isless)
@eval begin
# to fix ambiguities
null_safe_op{S<:SafeFloats,
Expand Down Expand Up @@ -130,3 +131,24 @@ for op in (:+, :-, :*, :/, :%, :÷, :&, :|, :^, :<<, :>>, :(>>>),
$op{S}(x::Nullable{S}, y::Nullable{Union{}}) = Nullable{S}()
end
end

if !method_exists(isless, (Nullable, Nullable))
function isless{S,T}(x::Nullable{S}, y::Nullable{T})
# NULL values are sorted last
if null_safe_op(@functorize(isless), S, T)
(!x.isnull & y.isnull) |
(!x.isnull & !y.isnull & isless(x.value, y.value))
else
if x.isnull
return false
elseif y.isnull
return true
else
return isless(x.value, y.value)
end
end
end
isless(x::Nullable{Union{}}, y::Nullable{Union{}}) = false
isless(x::Nullable{Union{}}, y::Nullable) = false
isless(x::Nullable, y::Nullable{Union{}}) = !x.isnull
end
11 changes: 11 additions & 0 deletions test/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,17 @@ module TestOperators
x = op(Nullable(), Nullable())
@test isa(x, Nullable{Union{}}) && isnull(x)
end

# isless
@test isless(Nullable(u), Nullable(v)) === isless(u, v)

@test isless(Nullable(u), Nullable(v, true)) === true
@test isless(Nullable(u, true), Nullable(v)) === false
@test isless(Nullable(u, true), Nullable(v, true)) === false

@test isless(Nullable(u), Nullable()) === true
@test isless(Nullable(), Nullable(v)) === false
@test isless(Nullable(), Nullable()) === false
end
end
end

0 comments on commit 2e1fd1c

Please sign in to comment.