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

Add allequal #43354

Merged
merged 6 commits into from
Feb 11, 2022
Merged
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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ New language features
to enforce the involved function calls to be (or not to be) inlined. ([#41312])
* The default behavior of observing `@inbounds` declarations is now an option via `auto` in `--check-bounds=yes|no|auto` ([#41551])
* New function `eachsplit(str)` for iteratively performing `split(str)`.
* New function `allequal(itr)` for testing if all elements in an iterator are equal. ([#43354])
* `∀`, `∃`, and `∄` are now allowed as identifier characters ([#42314]).
* Support for Unicode 14.0.0 ([#43443]).
* `try`-blocks can now optionally have an `else`-block which is executed right after the main body only if
Expand Down
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@ export
# collections
all!,
all,
allequal,
allunique,
any!,
any,
Expand Down
38 changes: 36 additions & 2 deletions base/set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ as determined by [`isequal`](@ref), in the order that the first of each
set of equivalent elements originally appears. The element type of the
input is preserved.

See also: [`unique!`](@ref), [`allunique`](@ref).
See also: [`unique!`](@ref), [`allunique`](@ref), [`allequal`](@ref).

# Examples
```jldoctest
Expand Down Expand Up @@ -383,7 +383,7 @@ end

Return `true` if all values from `itr` are distinct when compared with [`isequal`](@ref).

See also: [`unique`](@ref), [`issorted`](@ref).
See also: [`unique`](@ref), [`issorted`](@ref), [`allequal`](@ref).

# Examples
```jldoctest
Expand Down Expand Up @@ -427,6 +427,40 @@ allunique(::Union{AbstractSet,AbstractDict}) = true

allunique(r::AbstractRange) = !iszero(step(r)) || length(r) <= 1

"""
allequal(itr) -> Bool

Return `true` if all values from `itr` are equal when compared with [`isequal`](@ref).

See also: [`unique`](@ref), [`allunique`](@ref).

!!! compat "Julia 1.8"
The `allequal` function requires at least Julia 1.8.

# Examples
```jldoctest
julia> allequal([])
true

julia> allequal([1])
true

julia> allequal([1, 1])
true

julia> allequal([1, 2])
false

julia> allequal(Dict(:a => 1, :b => 1))
false
```
"""
allequal(itr) = isempty(itr) ? true : all(isequal(first(itr)), itr)

allequal(c::Union{AbstractSet,AbstractDict}) = length(c) <= 1
CameronBieganek marked this conversation as resolved.
Show resolved Hide resolved
CameronBieganek marked this conversation as resolved.
Show resolved Hide resolved

allequal(r::AbstractRange) = iszero(step(r)) || length(r) <= 1

filter!(f, s::Set) = unsafe_filter!(f, s)

const hashs_seed = UInt === UInt64 ? 0x852ada37cfe8e0ce : 0xcfe8e0ce
Expand Down
1 change: 1 addition & 0 deletions doc/src/base/collections.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ Base.indexin
Base.unique
Base.unique!
Base.allunique
Base.allequal
Base.reduce(::Any, ::Any)
Base.foldl(::Any, ::Any)
Base.foldr(::Any, ::Any)
Expand Down
29 changes: 29 additions & 0 deletions test/sets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,35 @@ end
@test allunique(r) == invoke(allunique, Tuple{Any}, r)
end
end

@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

@testset "filter(f, ::$S)" for S = (Set, BitSet)
s = S([1,2,3,4])
@test s !== filter( isodd, s) == S([1,3])
Expand Down