无限期地重复Julia迭代器

问题描述 投票:2回答:1

我正在寻找一种无限重复序列的方法。与之相媲美的东西

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

但外部是无限的,结果是迭代器(不是数组)

我试过了

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

反复为IterTools但它引起了错误。

iterator generator julia
1个回答
4
投票

在版本0.6及更高版本,您可以使用Base.Iterators.cycle。例如:

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
© www.soinside.com 2019 - 2024. All rights reserved.