Skip to content

Commit

Permalink
Iterators.cycle(iter, n) (#819)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcabbott authored Feb 16, 2024
1 parent a62d959 commit 9e88843
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ changes in `julia`.

## Supported features

* `Iterators.cycle(itr, n)` is the lazy version of `repeat(vector, n)`. ([#47354]) (since Compat 4.13.0)

* `@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)

* `redirect_stdio`, for simple stream redirection. ([#37978]) (since Compat 4.8.0)
Expand Down
5 changes: 5 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,11 @@ if VERSION < v"1.9.0-DEV.461"
Base.VersionNumber(v::VersionNumber) = v
end

# https://github.com/JuliaLang/julia/pull/47354
if VERSION < v"1.11.0-DEV.1579"
Iterators.cycle(xs, n::Integer) = Iterators.flatten(Iterators.repeated(xs, n))
end

include("deprecated.jl")

end # module Compat
18 changes: 18 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -734,3 +734,21 @@ end
v = VersionNumber("1.2.3")
@test VersionNumber(v) === v
end

# https://github.com/JuliaLang/julia/pull/47354
@testset "cycle(iter, n)" begin
using Base.Iterators: cycle
@test collect(cycle(0:3, 2)) == [0, 1, 2, 3, 0, 1, 2, 3]
@test collect(cycle(Iterators.filter(iseven, 1:4), 2)) == [2, 4, 2, 4]
# @test collect(take(cycle(countfrom(11), 3), 4)) == 11:14 # this iterator is defined in Base's tests

@test isempty(cycle(1:0)) == isempty(cycle(1:0, 3)) == true
@test isempty(cycle(1:5, 0))
@test isempty(cycle(Iterators.filter(iseven, 1:4), 0))

@test eltype(cycle(0:3, 2)) === Int
@test Base.IteratorEltype(cycle(0:3, 2)) == Base.HasEltype()

Base.haslength(cycle(0:3, 2)) == false # but not sure we should test these
Base.IteratorSize(cycle(0:3, 2)) == Base.SizeUnknown()
end

0 comments on commit 9e88843

Please sign in to comment.