From 6a28ec02bbf791cc414cf8255a0a0e738d86b951 Mon Sep 17 00:00:00 2001 From: Neven Sajko Date: Sat, 14 Sep 2024 02:20:51 +0200 Subject: [PATCH 1/2] support `isless` for zero-dimensional `AbstractArray`s Fixes #55771 --- NEWS.md | 1 + base/abstractarray.jl | 9 +++++++++ test/arrayops.jl | 8 ++++++++ 3 files changed, 18 insertions(+) diff --git a/NEWS.md b/NEWS.md index c12cc3c64300c..ee9baa8fcaf05 100644 --- a/NEWS.md +++ b/NEWS.md @@ -103,6 +103,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 ([#XXXXX]) Standard library changes ------------------------ diff --git a/base/abstractarray.jl b/base/abstractarray.jl index 754ab20660ab8..fd08ecc844201 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -3047,6 +3047,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) diff --git a/test/arrayops.jl b/test/arrayops.jl index 333b68e287c4c..0e365d8690f9c 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -1404,6 +1404,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 From 23c282009c210eb8a360b29504bd0f26b1897714 Mon Sep 17 00:00:00 2001 From: Neven Sajko Date: Sat, 14 Sep 2024 02:24:26 +0200 Subject: [PATCH 2/2] update PR # in NEWS --- NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index ee9baa8fcaf05..0e63ec4d0afa8 100644 --- a/NEWS.md +++ b/NEWS.md @@ -103,7 +103,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 ([#XXXXX]) +* `isless` (and thus `cmp`, sorting, etc.) is now supported for zero-dimensional `AbstractArray`s ([#55772]) Standard library changes ------------------------