Skip to content

Commit

Permalink
add allequal
Browse files Browse the repository at this point in the history
  • Loading branch information
mcabbott committed Feb 27, 2022
1 parent a2de107 commit 177f169
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Compat"
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
version = "3.41.0"
version = "3.42.0"

[deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ changes in `julia`.

## Supported features

* `allequal`, the opposite of `allunique` ([#43354]). (since Compat 3.42.0)

* `eachsplit` for iteratively performing split(str). ([#39245]). (since Compat 3.41.0)

* `ismutabletype(t::Type)` check whether a type is mutable (the field `mutable` of `DataType` was removed. [#39037]) (since Compat 3.40)
Expand Down Expand Up @@ -290,3 +292,4 @@ Note that you should specify the correct minimum version for `Compat` in the
[#42125]: https://github.com/JuliaLang/julia/pull/42125
[#41312]: https://github.com/JuliaLang/julia/pull/41312
[#41328]: https://github.com/JuliaLang/julia/pull/41328
[#43354]: https://github.com/JuliaLang/julia/pull/43354
10 changes: 10 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1314,6 +1314,16 @@ if VERSION < v"1.8.0-DEV.487"
eachsplit(str::AbstractString; limit::Integer=0, keepempty=false) =
eachsplit(str, isspace; limit=limit, keepempty=keepempty)
end

# https://github.com/JuliaLang/julia/pull/43354
if VERSION < v"1.8.0-DEV.1494" # 98e60ffb11ee431e462b092b48a31a1204bd263d
export allequal
allequal(itr) = isempty(itr) ? true : all(isequal(first(itr)), itr)
allequal(c::Union{AbstractSet,AbstractDict}) = length(c) <= 1
allequal(r::AbstractRange) = iszero(step(r)) || length(r) <= 1
end


include("iterators.jl")
include("deprecated.jl")

Expand Down
32 changes: 31 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1370,4 +1370,34 @@ so, these are the Base.split tests, but replacing split with eachsplit |> collec
eachsplit("α β γ", isspace) |> collect == rsplit("α β γ", isspace) == ["α","β","γ"]
@test eachsplit("ö.", ".") |> collect == rsplit("ö.", ".") == ["ö",""]
@test eachsplit("α β γ", "β") |> collect == rsplit("α β γ", "β") == ["α "," γ"]
end
end

# https://github.com/JuliaLang/julia/pull/43354
@testset "allequal" begin
@test allequal(Set())
@test allequal(Set(1))
@test !allequal(Set([1, 2]))
@test allequal(Dict())
@test allequal(Dict(:a => 1))
@test !allequal(Dict(:a => 1, :b => 2))
@test allequal([])
@test allequal([1])
@test allequal([1, 1])
@test !allequal([1, 1, 2])
@test allequal([:a, :a])
@test !allequal([:a, :b])
@test !allequal(1:2)
@test allequal(1:1)
@test !allequal(4.0:0.3:7.0)
@test allequal(4:-1:5) # empty range
@test !allequal(7:-1:1) # negative step
@test !allequal(Date(2018, 8, 7):Day(1):Date(2018, 8, 11)) # JuliaCon 2018
@test !allequal(DateTime(2018, 8, 7):Hour(1):DateTime(2018, 8, 11))
@test allequal(StepRangeLen(1.0, 0.0, 2))
@test !allequal(StepRangeLen(1.0, 1.0, 2))
@test allequal(LinRange(1, 1, 0))
@test allequal(LinRange(1, 1, 1))
@test allequal(LinRange(1, 1, 2))
@test !allequal(LinRange(1, 2, 2))
end

0 comments on commit 177f169

Please sign in to comment.