From 140e76958a3e6b8352a61ade4f900de93071dafe Mon Sep 17 00:00:00 2001 From: MarcMush Date: Fri, 25 Sep 2020 15:28:27 +0200 Subject: [PATCH 1/4] add countfrom(::Any) --- base/iterators.jl | 14 ++++++++------ test/iterators.jl | 8 +++++++- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/base/iterators.jl b/base/iterators.jl index 9a3942836fb69..59f17f23326cf 100644 --- a/base/iterators.jl +++ b/base/iterators.jl @@ -587,8 +587,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 @@ -608,11 +608,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 iterate(it::Count, state=it.start) = (state, state + it.step) diff --git a/test/iterators.jl b/test/iterators.jl index b9bec84bf9a58..460dec2f52c62 100644 --- a/test/iterators.jl +++ b/test/iterators.jl @@ -122,7 +122,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 @@ -133,6 +133,12 @@ let i = 0, k = 1 k += 1 k <= 10 || break end + 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 end # take From 7df3cf6106a20d8c41f6ee0e69a8eca29f2b5ac4 Mon Sep 17 00:00:00 2001 From: Marc Ittel <35898736+MarcMush@users.noreply.github.com> Date: Mon, 30 Nov 2020 16:02:38 +0100 Subject: [PATCH 2/4] NEWS: add countfrom(::Any) --- NEWS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/NEWS.md b/NEWS.md index f05f2c669ab16..d9e8b128b01f1 100644 --- a/NEWS.md +++ b/NEWS.md @@ -114,6 +114,7 @@ Standard library changes * The `Pkg.Artifacts` module has been imported as a separate standard library. It is still available as `Pkg.Artifacts`, however starting from Julia v1.6+, packages may import simply `Artifacts` without importing all of `Pkg` alongside. ([#37320]) +* `Iterators.countfrom` now accepts any type that define `+`. ([#37747]) #### LinearAlgebra From ba168df72617ac47977da993a274b15fb2017a7f Mon Sep 17 00:00:00 2001 From: Marc Ittel <35898736+MarcMush@users.noreply.github.com> Date: Mon, 27 Sep 2021 18:17:58 +0200 Subject: [PATCH 3/4] countfrom test with different types --- test/iterators.jl | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test/iterators.jl b/test/iterators.jl index 47f1ceb0c0b87..010697f5ae4e2 100644 --- a/test/iterators.jl +++ b/test/iterators.jl @@ -3,6 +3,7 @@ using Base.Iterators using Random using Base: IdentityUnitRange +using Dates: Date, Day @test Base.IteratorSize(Any) isa Base.SizeUnknown @@ -134,12 +135,15 @@ let i = 0, k = 1, l = 0 k += 1 k <= 10 || break end - for j = countfrom(Int[0,0], Float64[1.0,2.0]) + # 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] + @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 From 20d3879416958e2b1f019df2f7c010128ff65b07 Mon Sep 17 00:00:00 2001 From: "Steven G. Johnson" Date: Mon, 27 Sep 2021 14:14:58 -0400 Subject: [PATCH 4/4] Update NEWS.md --- NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 3630ce17bd9c4..ffbc96a4d3d35 100644 --- a/NEWS.md +++ b/NEWS.md @@ -51,7 +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 define `+`. ([#37747]) +* `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])