Skip to content

Commit 82d1848

Browse files
authored
allequal(f, itr) and allunique(f, itr) (#818)
1 parent 7d79ace commit 82d1848

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "Compat"
22
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
3-
version = "4.12.0"
3+
version = "4.13.0"
44

55
[deps]
66
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ changes in `julia`.
7070

7171
## Supported features
7272

73+
* `allequal(f, itr)` and `allunique(f, itr)` methods. ([#47679]) (since Compat 4.13.0)
74+
7375
* `Iterators.cycle(itr, n)` is the lazy version of `repeat(vector, n)`. ([#47354]) (since Compat 4.13.0)
7476

7577
* `@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 3.47.0, 4.10.0)
@@ -181,3 +183,4 @@ Note that you should specify the correct minimum version for `Compat` in the
181183
[#47354]: https://github.com/JuliaLang/julia/issues/47354
182184
[#48038]: https://github.com/JuliaLang/julia/issues/48038
183185
[#50105]: https://github.com/JuliaLang/julia/issues/50105
186+
[#47679]: https://github.com/JuliaLang/julia/pull/47679

src/Compat.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,8 @@ if VERSION < v"1.8.0-DEV.1494" # 98e60ffb11ee431e462b092b48a31a1204bd263d
330330
allequal(itr) = isempty(itr) ? true : all(isequal(first(itr)), itr)
331331
allequal(c::Union{AbstractSet,AbstractDict}) = length(c) <= 1
332332
allequal(r::AbstractRange) = iszero(step(r)) || length(r) <= 1
333+
else
334+
import Base: allequal # extended below
333335
end
334336

335337
# https://github.com/JuliaLang/julia/commit/bdf9ead91e5a8dfd91643a17c1626032faada329
@@ -770,6 +772,27 @@ if VERSION < v"1.7.0-DEV.1187"
770772
export redirect_stdio
771773
end
772774

775+
# https://github.com/JuliaLang/julia/pull/47679
776+
if VERSION < v"1.11.0-DEV.1562"
777+
Base.allunique(f, xs) = allunique(Base.Generator(f, xs))
778+
function Base.allunique(f::F, t::Tuple) where {F}
779+
length(t) < 2 && return true
780+
length(t) < 32 || return Base._hashed_allunique(Base.Generator(f, t))
781+
return allunique(map(f, t))
782+
end
783+
784+
# allequal is either imported or defined above
785+
allequal(f, xs) = allequal(Base.Generator(f, xs))
786+
function allequal(f, xs::Tuple)
787+
length(xs) <= 1 && return true
788+
f1 = f(xs[1])
789+
for x in Base.tail(xs)
790+
isequal(f1, f(x)) || return false
791+
end
792+
return true
793+
end
794+
end
795+
773796
# https://github.com/JuliaLang/julia/pull/45052
774797
if VERSION < v"1.9.0-DEV.461"
775798
Base.VersionNumber(v::VersionNumber) = v

test/runtests.jl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,34 @@ end
729729
@test_throws LoadError @eval @compat publac @bar, foo
730730
end
731731

732+
# https://github.com/JuliaLang/julia/pull/47679
733+
@testset "allunique(f, xs)" begin
734+
@test allunique(sin, 1:3)
735+
@test !allunique(sin, [1,2,3,1])
736+
@test allunique(sin, (1, 2, pi, im)) # eltype Any
737+
@test allunique(abs2, 1:100)
738+
@test !allunique(abs, -10:10)
739+
@test allunique(abs2, Vector{Any}(1:100))
740+
# These cases don't call the function at all:
741+
@test allunique(error, [])
742+
@test_skip allunique(error, [1]) # depends on updated code in Base to work
743+
end
744+
@testset "allequal(f, xs)" begin
745+
@test allequal(abs2, [3, -3])
746+
@test allequal(x -> 1, rand(3))
747+
@test !allequal(x -> rand(), [1,1,1])
748+
# tuples
749+
@test allequal(abs2, (3, -3))
750+
@test allequal(x -> 1, Tuple(rand(3)))
751+
@test !allequal(x -> rand(), (1,1,1))
752+
# These cases don't call the function at all:
753+
@test allequal(error, [])
754+
@test allequal(error, ())
755+
@test allequal(error, (x for x in 1:3 if false))
756+
@test_skip allequal(error, [1]) # fixed not by new code but by upgrades to old code
757+
@test allequal(error, (1,))
758+
end
759+
732760
# https://github.com/JuliaLang/julia/pull/45052
733761
@testset "VersionNumber no-op constructor" begin
734762
v = VersionNumber("1.2.3")

0 commit comments

Comments
 (0)