Skip to content

Commit

Permalink
support isless for zero-dimensional AbstractArrays (#55772)
Browse files Browse the repository at this point in the history
Fixes #55771
  • Loading branch information
nsajko authored Oct 25, 2024
1 parent ec2e121 commit b38fde1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ New library features
* `RegexMatch` objects can now be used to construct `NamedTuple`s and `Dict`s ([#50988])
* `Lockable` is now exported ([#54595])
* New `ltruncate`, `rtruncate` and `ctruncate` functions for truncating strings to text width, accounting for char widths ([#55351])
* `isless` (and thus `cmp`, sorting, etc.) is now supported for zero-dimensional `AbstractArray`s ([#55772])

Standard library changes
------------------------
Expand Down
9 changes: 9 additions & 0 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3044,6 +3044,15 @@ function cmp(A::AbstractVector, B::AbstractVector)
return cmp(length(A), length(B))
end

"""
isless(A::AbstractArray{<:Any,0}, B::AbstractArray{<:Any,0})
Return `true` when the only element of `A` is less than the only element of `B`.
"""
function isless(A::AbstractArray{<:Any,0}, B::AbstractArray{<:Any,0})
isless(only(A), only(B))
end

"""
isless(A::AbstractVector, B::AbstractVector)
Expand Down
8 changes: 8 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1409,6 +1409,14 @@ end
end

@testset "lexicographic comparison" begin
@testset "zero-dimensional" begin
vals = (0, 0.0, 1, 1.0)
for l vals
for r vals
@test cmp(fill(l), fill(r)) == cmp(l, r)
end
end
end
@test cmp([1.0], [1]) == 0
@test cmp([1], [1.0]) == 0
@test cmp([1, 1], [1, 1]) == 0
Expand Down

0 comments on commit b38fde1

Please sign in to comment.