Skip to content

Commit

Permalink
add 1-arg methods for set comparisons (JuliaLang#50862)
Browse files Browse the repository at this point in the history
  • Loading branch information
longemen3000 committed Oct 28, 2023
1 parent ab1eec5 commit 18f8070
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
102 changes: 102 additions & 0 deletions base/abstractset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,31 @@ hasfastin(x) = hasfastin(typeof(x))

(a, b) = b a

"""
issubset(x)
Create a function that compares its argument to `x` using [`issubset`](@ref), i.e.
a function equivalent to `y -> issubset(y, x)`.
The returned function is of type `Base.Fix2{typeof(issubset)}`, which can be
used to implement specialized methods.
!!! compat "Julia 1.11"
This functionality requires at least Julia 1.11.
"""
issubset(a) = Fix2(issubset, a)

"""
⊇(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.
!!! compat "Julia 1.11"
This functionality requires at least Julia 1.11.
"""
(a) = Fix2(, a)
## strict subset comparison

function end
Expand Down Expand Up @@ -393,6 +418,31 @@ false
(a, b) = Set(a) Set(b)
(a, b) = b a

"""
⊋(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.
!!! compat "Julia 1.11"
This functionality requires at least Julia 1.11.
"""
(a) = Fix2(, a)
"""
⊊(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.
!!! compat "Julia 1.11"
This functionality requires at least Julia 1.11.
"""
(a) = Fix2(, a)

function end
function end
"""
Expand All @@ -417,6 +467,32 @@ false
(a, b) = !(a, b)
(a, b) = b a

"""
⊉(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.
!!! compat "Julia 1.11"
This functionality requires at least Julia 1.11.
"""
(a) = Fix2(, a)

"""
⊈(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.
!!! compat "Julia 1.11"
This functionality requires at least Julia 1.11.
"""
(a) = Fix2(, a)

## set equality comparison

"""
Expand Down Expand Up @@ -453,6 +529,19 @@ function issetequal(a, b)
return issetequal(Set(a), Set(b))
end

"""
issetequal(x)
Create a function that compares its argument to `x` using [`issetequal`](@ref), i.e.
a function equivalent to `y -> issetequal(y, x)`.
The returned function is of type `Base.Fix2{typeof(issetequal)}`, which can be
used to implement specialized methods.
!!! compat "Julia 1.11"
This functionality requires at least Julia 1.11.
"""
issetequal(a) = Fix2(issetequal, a)

## set disjoint comparison
"""
isdisjoint(a, b) -> Bool
Expand Down Expand Up @@ -499,6 +588,19 @@ function isdisjoint(a::AbstractRange{T}, b::AbstractRange{T}) where T
end
end

"""
isdisjoint(x)
Create a function that compares its argument to `x` using [`isdisjoint`](@ref), i.e.
a function equivalent to `y -> isdisjoint(y, x)`.
The returned function is of type `Base.Fix2{typeof(isdisjoint)}`, which can be
used to implement specialized methods.
!!! compat "Julia 1.11"
This functionality requires at least Julia 1.11.
"""
isdisjoint(a) = Fix2(isdisjoint, a)

_overlapping_range_isdisjoint(a::AbstractRange{T}, b::AbstractRange{T}) where T = invoke(isdisjoint, Tuple{Any,Any}, a, b)

function _overlapping_range_isdisjoint(a::AbstractRange{T}, b::AbstractRange{T}) where T<:Integer
Expand Down
13 changes: 13 additions & 0 deletions test/sets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,9 @@ end
@test issubset(intersect(l,r), r)
@test issubset(l, union(l,r))
@test issubset(r, union(l,r))
@test issubset(union(l,r))(r)
@test isdisjoint(l,l) == isempty(l)
@test isdisjoint(l)(l) == isempty(l)
@test isdisjoint(l,r) == isempty(intersect(l,r))
if S === Vector
@test sort(union(intersect(l,r),symdiff(l,r))) == sort(union(l,r))
Expand All @@ -381,6 +383,15 @@ end
@test (S([1,2]), S([1]))
@test !(S([1]), S([1]))
@test (S([1]), S([2]))

@test (S([1,2]))(S([1]))
@test (S([1,2]))(S([1]))
@test !(S([1]))(S([1]))
@test (S([2]))(S([1]))
@test (S([1]))(S([1,2]))
@test (S([1]))(S([1,2]))
@test !(S([1]))(S([1]))
@test (S([2]))(S([1]))
end
let s1 = S([1,2,3,4])
@test s1 !== symdiff(s1) == s1
Expand Down Expand Up @@ -859,6 +870,8 @@ end
@test !(B A)
@test !issetequal(A, B)
@test !issetequal(B, A)
@test !issetequal(B)(A)
@test !issetequal(A)(B)
for T = (Tuple, identity, Set, BitSet, Base.IdSet{Int})
@test issetequal(A, T(A))
@test issetequal(B, T(B))
Expand Down

0 comments on commit 18f8070

Please sign in to comment.