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 Mar 30, 2020
1 parent f6a2e73 commit 7bbb84f
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 @@ -86,6 +86,7 @@ New library functions
* New function `bitreverse` for reversing the order of bits in a fixed-width integer ([#34791]).
* New function `bitrotate(x, k)` for rotating the bits in a fixed-width integer ([#33937]).
* One argument methods `startswith(x)` and `endswith(x)` have been added, returning partially-applied versions of the functions, similar to existing methods like `isequal(x)` ([#33193]).
* 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 @@ -645,6 +645,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 @@ -157,6 +157,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 7bbb84f

Please sign in to comment.