diff --git a/base/operators.jl b/base/operators.jl index becbe2cce0a53..0977e5dcb90b4 100644 --- a/base/operators.jl +++ b/base/operators.jl @@ -927,6 +927,56 @@ used to implement specialized methods. """ ==(x) = Fix2(==, x) +""" + !=(x) + +Create a function that compares its argument to `x` using [`!=`](@ref), i.e. +a function equivalent to `y -> y != x`. +The returned function is of type `Base.Fix2{typeof(!=)}`, which can be +used to implement specialized methods. +""" +!=(x) = Fix2(!=, x) + +""" + >=(x) + +Create a function that compares its argument to `x` using [`>=`](@ref), i.e. +a function equivalent to `y -> y >= x`. +The returned function is of type `Base.Fix2{typeof(>=)}`, which can be +used to implement specialized methods. +""" +>=(x) = Fix2(>=, x) + +""" + <=(x) + +Create a function that compares its argument to `x` using [`<=`](@ref), i.e. +a function equivalent to `y -> y <= x`. +The returned function is of type `Base.Fix2{typeof(<=)}`, which can be +used to implement specialized methods. +""" +<=(x) = Fix2(<=, x) + +""" + >(x) + +Create a function that compares its argument to `x` using [`>`](@ref), i.e. +a function equivalent to `y -> y > x`. +The returned function is of type `Base.Fix2{typeof(>)}`, which can be +used to implement specialized methods. +""" +>(x) = Fix2(>, x) + +""" + <(x) + +Create a function that compares its argument to `x` using [`<`](@ref), i.e. +a function equivalent to `y -> y < x`. +The returned function is of type `Base.Fix2{typeof(<)}`, which can be +used to implement specialized methods. +""" +<(x) = Fix2(<, x) + """ splat(f) diff --git a/test/operators.jl b/test/operators.jl index fc574cafa654b..3a265dff7ccc8 100644 --- a/test/operators.jl +++ b/test/operators.jl @@ -196,3 +196,20 @@ end @test fx(y) == x / y @test fy(x) == x / y end + + +@testset "curried comparisons" begin + eql5 = (==)(5) + neq5 = (!=)(5) + gte5 = (>=)(5) + lte5 = (<=)(5) + gt5 = (>)(5) + lt5 = (<)(5) + + @test eql5(5) && !eql5(0) + @test neq5(6) && !neq5(5) + @test gte5(5) && gte5(6) + @test lte5(5) && lte5(4) + @test gt5(6) && !gt5(5) + @test lt5(4) && !lt5(5) +end