From 56785e82235e2d86d7c726c046653545a61e7a3d Mon Sep 17 00:00:00 2001 From: Colin Caine Date: Mon, 30 Mar 2020 18:10:03 +0100 Subject: [PATCH] Add isgreater Defines a descending total order where unorderable values and missing are last. This makes defining min, argmin, etc, simpler. --- NEWS.md | 1 + base/exports.jl | 1 + base/operators.jl | 24 +++++++++++++++++++++++- test/operators.jl | 8 ++++++++ 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 0e2e8daf06c105..0b43d78b69c508 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 -------------------- diff --git a/base/exports.jl b/base/exports.jl index 3e7e6ce7647e45..bbd0921c629b1f 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -656,6 +656,7 @@ export isequal, ismutable, isless, + isgreater, ifelse, objectid, sizeof, diff --git a/base/operators.jl b/base/operators.jl index 55de805671a5f5..1563ccaccaf8d9 100644 --- a/base/operators.jl +++ b/base/operators.jl @@ -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). @@ -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 diff --git a/test/operators.jl b/test/operators.jl index a806c5cead5df8..dc9c5ccac73e49 100644 --- a/test/operators.jl +++ b/test/operators.jl @@ -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