Skip to content

Commit

Permalink
complete curried comparisons (#30915)
Browse files Browse the repository at this point in the history
* add !=(x) <=(x) >=(x) <(x) >(x)
* add tests for curried comparisons
  • Loading branch information
JeffreySarnoff authored and StefanKarpinski committed Feb 2, 2019
1 parent b595579 commit e7e726b
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
50 changes: 50 additions & 0 deletions base/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions test/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit e7e726b

Please sign in to comment.