Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

complete curried comparisons #30915

Merged
merged 2 commits into from
Feb 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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