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

Deprecate sort for NTuple and other iterables #811

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ changes in `julia`.

* `@compat public foo, bar` marks `foo` and `bar` as public in Julia 1.11+ and is a no-op in Julia 1.10 and earlier. ([#50105]) (since Compat 4.10.0)

* `sort` for `NTuple` and other iterables. ([#46104]) (since Compat 4.9.0)

* `redirect_stdio`, for simple stream redirection. ([#37978]) (since Compat 4.8.0)

* `trunc`, `floor`, `ceil`, and `round` to `Bool`. ([#25085]) (since Compat 4.7.0)
Expand Down
36 changes: 0 additions & 36 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -705,42 +705,6 @@ if VERSION < v"1.7.0-DEV.1187"
export redirect_stdio
end

# https://github.com/JuliaLang/julia/pull/46104
if VERSION < v"1.10.0-DEV.1404"
using Base: Ordering, Forward, ord, lt, tail, copymutable, DEFAULT_STABLE, IteratorSize, HasShape, IsInfinite
function Base.sort(v; kws...)
size = IteratorSize(v)
size == HasShape{0}() && throw(ArgumentError("$v cannot be sorted"))
size == IsInfinite() && throw(ArgumentError("infinite iterator $v cannot be sorted"))
sort!(copymutable(v); kws...)
end
Base.sort(::AbstractString; kws...) =
throw(ArgumentError("sort(::AbstractString) is not supported"))
Base.sort(::Tuple; kws...) =
throw(ArgumentError("sort(::Tuple) is only supported for NTuples"))

function Base.sort(x::NTuple{N}; lt::Function=isless, by::Function=identity,
rev::Union{Bool,Nothing}=nothing, order::Ordering=Forward) where N
o = ord(lt,by,rev,order)
if N > 9
v = sort!(copymutable(x), DEFAULT_STABLE, o)
tuple((v[i] for i in 1:N)...)
else
_sort(x, o)
end
end
_sort(x::Union{NTuple{0}, NTuple{1}}, o::Ordering) = x
function _sort(x::NTuple, o::Ordering)
a, b = Base.IteratorsMD.split(x, Val(length(x)>>1))
merge(_sort(a, o), _sort(b, o), o)
end
merge(x::NTuple, y::NTuple{0}, o::Ordering) = x
merge(x::NTuple{0}, y::NTuple, o::Ordering) = y
merge(x::NTuple{0}, y::NTuple{0}, o::Ordering) = x # Method ambiguity
merge(x::NTuple, y::NTuple, o::Ordering) =
(lt(o, y[1], x[1]) ? (y[1], merge(x, tail(y), o)...) : (x[1], merge(tail(x), y, o)...))
end

include("deprecated.jl")

end # module Compat
45 changes: 45 additions & 0 deletions src/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,48 @@ Base.@deprecate_binding parseatom Meta.parseatom false
Base.@deprecate_binding parseall Meta.parseall false
import UUIDs
Base.@deprecate_binding uuid5 UUIDs.uuid5 true

# https://github.com/JuliaLang/julia/pull/46104
# reverted in https://github.com/JuliaLang/julia/pull/52010
if VERSION < v"1.10.0-DEV.1404" ||
(VERSION >= v"1.10-rc2" && VERSION < v"1.11.0-") ||
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assumes that JuliaLang/julia#52010 will actually be in v1.10-rc2.

VERSION >= v"1.11.0-DEV.924"
Comment on lines +21 to +23
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if VERSION < v"1.10.0-DEV.1404" ||
(VERSION >= v"1.10-rc2" && VERSION < v"1.11.0-") ||
VERSION >= v"1.11.0-DEV.924"
if VERSION < v"1.10.0-DEV.1404"

To stop adding methods to Base.sort in future Julia versions while keeping sort working on general iterables on Julia < 1.10 with Compat (where people might already be using it).

Ref. #811 (comment)

using Base: Ordering, Forward, ord, lt, tail, copymutable, DEFAULT_STABLE, IteratorSize, HasShape, IsInfinite
function Base.sort(v; kws...)
Base.depwarn("sorting arbitrary iterables is deprecated", :sort)
if v isa AbstractString
throw(ArgumentError("sort(::AbstractString) is not supported"))
end
if v isa NTuple
return _sort(v; kws...)
end
if v isa Tuple
throw(ArgumentError("sort(::Tuple) is only supported for NTuples"))
end
Comment on lines +25 to +35
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed the dispatch to only add this single method to Base.sort. If part of this functionality is re-added toBase in the future, it will likely be for more specific argument types and we then won't overwrite these new methods. Adding this method Base for any future Julia versions still makes me a bit uncomfortable.

size = IteratorSize(v)
size == HasShape{0}() && throw(ArgumentError("$v cannot be sorted"))
size == IsInfinite() && throw(ArgumentError("infinite iterator $v cannot be sorted"))
sort!(copymutable(v); kws...)
end

function _sort(x::NTuple{N}; lt::Function=isless, by::Function=identity,
rev::Union{Bool,Nothing}=nothing, order::Ordering=Forward) where N
o = ord(lt,by,rev,order)
if N > 9
v = sort!(copymutable(x), DEFAULT_STABLE, o)
tuple((v[i] for i in 1:N)...)
else
_sort(x, o)
end
end
_sort(x::Union{NTuple{0}, NTuple{1}}, o::Ordering) = x
function _sort(x::NTuple, o::Ordering)
a, b = Base.IteratorsMD.split(x, Val(length(x)>>1))
merge(_sort(a, o), _sort(b, o), o)
end
merge(x::NTuple, y::NTuple{0}, o::Ordering) = x
merge(x::NTuple{0}, y::NTuple, o::Ordering) = y
merge(x::NTuple{0}, y::NTuple{0}, o::Ordering) = x # Method ambiguity
merge(x::NTuple, y::NTuple, o::Ordering) =
(lt(o, y[1], x[1]) ? (y[1], merge(x, tail(y), o)...) : (x[1], merge(tail(x), y, o)...))
end
2 changes: 0 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -696,8 +696,6 @@ end
@testset "sort(iterable)" begin
function tuple_sort_test(x)
@test issorted(sort(x))
length(x) > 9 && return # length > 9 uses a vector fallback
@test 0 == @allocated sort(x)
Copy link
Member Author

@martinholters martinholters Nov 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These now allocate due to depwarn.

end
@testset "sort(::NTuple)" begin
@test sort((9,8,3,3,6,2,0,8)) == (0,2,3,3,6,8,8,9)
Expand Down