NHD*_*aly 3 indexing generator julia
愚蠢的问题:你如何N从 julia 中获取第一个元素Generator?
例如,我有一个生成奇数的生成器:
julia> odds = (x for x in 1:2:typemax(Int))
Base.Generator{StepRange{Int64,Int64},var"##69#70"}(var"##69#70"(), 1:2:9223372036854775807)
Run Code Online (Sandbox Code Playgroud)
我怎样才能得到前10个奇数?我尝试过这样的事情,但都没有成功:
julia> first(odds, 10)
ERROR: MethodError: no method matching first(::Base.Generator{StepRange{Int64,Int64},var"##69#70"}, ::Int64)
julia> head(odds, 10)
ERROR: UndefVarError: head not defined
julia> take!(odds, 10)
ERROR: MethodError: no method matching take!(::Base.Generator{StepRange{Int64,Int64},var"##69#70"}, ::Int64)
Run Code Online (Sandbox Code Playgroud)
在methodswith似乎并不看好之一:
julia> methodswith(Base.Generator)
[1] axes(g::Base.Generator) in Base at generator.jl:52
[2] collect(itr::Base.Generator) in Base at array.jl:615
[3] iterate(g::Base.Generator, s...) in Base at generator.jl:43
[4] length(g::Base.Generator) in Base at generator.jl:50
[5] ndims(g::Base.Generator) in Base at generator.jl:53
[6] size(g::Base.Generator) in Base at generator.jl:51
Run Code Online (Sandbox Code Playgroud)