scls
scls

Reputation: 17607

repeat a Julia iterator indefinitely

I'm looking for a way to repeat indefinitely a sequence. Something comparable to

julia> repeat(1:3, outer=2)
9-element Array{Int64,1}:
 1
 2
 3
 1
 2
 3

but with outer being infinite and with result being an iterator (not an Array)

I tried

for i in repeatedly([1:3])
    @show i
end

with repeatedly for IterTools but it raises an error.

Upvotes: 2

Views: 834

Answers (1)

Dan Getz
Dan Getz

Reputation: 18217

On version 0.6 and up, you can use Base.Iterators.cycle. For example:

julia> using Base.Iterators

julia> collect(take(cycle(1:3),10))
10-element Array{Int64,1}:
 1
 2
 3
 1
 2
 3
 1
 2
 3
 1

Upvotes: 5

Related Questions