Skip to content

Commit

Permalink
Add isgreater
Browse files Browse the repository at this point in the history
Defines a descending total order where unorderable values and missing are last.
This makes defining min, argmin, etc, simpler.
  • Loading branch information
cmcaine committed Nov 21, 2020
1 parent 5efd675 commit 56785e8
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ New library functions
* New function `insorted` for determining whether an element is in a sorted collection or not ([#37490]).
* New function `Base.rest` for taking the rest of a collection, starting from a specific
iteration state, in a generic way ([#37410]).
* New function `isgreater(a, b)` defines a descending total order where unorderable values and missing are ordered smaller than any regular value.

New library features
--------------------
Expand Down
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,7 @@ export
isequal,
ismutable,
isless,
isgreater,
ifelse,
objectid,
sizeof,
Expand Down
24 changes: 23 additions & 1 deletion base/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ is defined, it is expected to satisfy the following:
`isless(x, y) && isless(y, z)` implies `isless(x, z)`.
Values that are normally unordered, such as `NaN`,
are ordered in an arbitrary but consistent fashion.
are ordered after regular values.
[`missing`](@ref) values are ordered last.
This is the default comparison used by [`sort`](@ref).
Expand All @@ -168,6 +168,28 @@ isless(x::AbstractFloat, y::AbstractFloat) = (!isnan(x) & (isnan(y) | signless(x
isless(x::Real, y::AbstractFloat) = (!isnan(x) & (isnan(y) | signless(x, y))) | (x < y)
isless(x::AbstractFloat, y::Real ) = (!isnan(x) & (isnan(y) | signless(x, y))) | (x < y)

"""
isgreater(x, y)
Test whether `x` is greater than `y`, according to a fixed total order.
`isgreater` is defined in terms of `isless`, but is not the opposite of that function.
`isless` defines a fixed total order that ascends with unorderable values (such as `NaN`) and
[`missing`](@ref) ordered last (biggest).
`isgreater` defines a fixed total order that descends with unorderable values
and `missing` ordered last (smallest).
Values that are normally unordered, such as `NaN`,
are ordered after regular values.
[`missing`](@ref) values are ordered last.
# Implementation
Types should usually not implement this function. Instead, implement `isless`.
"""
isgreater(a, b) = _is_unorderable(a) || _is_unorderable(b) ? isless(a, b) : isless(b, a)
_is_unorderable(x) = !isa(x == x, Bool) || x != x



function ==(T::Type, S::Type)
@_pure_meta
Expand Down
8 changes: 8 additions & 0 deletions test/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ import Base.<

@test isless('a','b')

@testset "isgreater" begin
@test !isgreater(missing, 1)
@test isgreater(5, 1)
@test !isgreater(1, 5)
@test isgreater(1, missing)
@test isgreater(1, NaN)
end

@testset "vectorized comparisons between numbers" begin
@test 1 .!= 2
@test 1 .== 1
Expand Down

0 comments on commit 56785e8

Please sign in to comment.