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 Iterators.countfrom(::Any, ::Any) #37747

Merged
merged 5 commits into from
Sep 29, 2021
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 @@ -51,6 +51,7 @@ Standard library changes
constructing the range. ([#40382])
* TCP socket objects now expose `closewrite` functionality and support half-open mode usage ([#40783]).
* Intersect returns a result with the eltype of the type-promoted eltypes of the two inputs ([#41769]).
* `Iterators.countfrom` now accepts any type that defines `+`. ([#37747])

#### InteractiveUtils
* A new macro `@time_imports` for reporting any time spent importing packages and their dependencies ([#41612])
Expand Down
14 changes: 8 additions & 6 deletions base/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -592,8 +592,8 @@ IteratorSize(::Type{<:Rest{I}}) where {I} = rest_iteratorsize(IteratorSize(I))

# Count -- infinite counting

struct Count{S<:Number}
start::S
struct Count{T,S}
start::T
step::S
end

Expand All @@ -613,11 +613,13 @@ julia> for v in Iterators.countfrom(5, 2)
9
```
"""
countfrom(start::Number, step::Number) = Count(promote(start, step)...)
countfrom(start::Number) = Count(start, oneunit(start))
countfrom() = Count(1, 1)
countfrom(start::T, step::S) where {T,S} = Count{typeof(start+step),S}(start, step)
countfrom(start::Number, step::Number) = Count(promote(start, step)...)
countfrom(start) = Count(start, oneunit(start))
countfrom() = Count(1, 1)

eltype(::Type{Count{S}}) where {S} = S

eltype(::Type{Count{T,S}}) where {T,S} = T
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
eltype(::Type{Count{T,S}}) where {T,S} = T
eltype(::Type{<:Count{T}}) where {T} = T


iterate(it::Count, state=it.start) = (state, state + it.step)

Expand Down
12 changes: 11 additions & 1 deletion test/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Base.Iterators
using Random
using Base: IdentityUnitRange
using Dates: Date, Day

@test Base.IteratorSize(Any) isa Base.SizeUnknown

Expand Down Expand Up @@ -123,7 +124,7 @@ end

# countfrom
# ---------
let i = 0, k = 1
let i = 0, k = 1, l = 0
for j = countfrom(0, 2)
@test j == i*2
i += 1
Expand All @@ -134,6 +135,15 @@ let i = 0, k = 1
k += 1
k <= 10 || break
end
# test that `start` promotes to `typeof(start+step)`
for j = countfrom(Int[0, 0], Float64[1.0, 2.0])
@test j isa Vector{Float64}
@test j == l*[1, 2]
l += 1
l <= 10 || break
end
# test with `start` and `step` having different types
@test collect(take(countfrom(Date(2020,12,25), Day(1)), 12)) == range(Date(2020,12,25), step=Day(1), length=12)
end

# take
Expand Down