Iterators
这个库里我找不到map,本来是想这样操作的
collect(Iterators.take(map(x->x+1,Iterators.countfrom(1,1)),10))
对无限流Iterators.countfrom(1,1)
中的每一个元素都加1,然后从这个流中取出10个,并且这个操作是惰性的,
但是对于map没有这种方法定义,他不接受迭代器为参数,有没有其他替代方法
Iterators
这个库里我找不到map,本来是想这样操作的
collect(Iterators.take(map(x->x+1,Iterators.countfrom(1,1)),10))
对无限流Iterators.countfrom(1,1)
中的每一个元素都加1,然后从这个流中取出10个,并且这个操作是惰性的,
但是对于map没有这种方法定义,他不接受迭代器为参数,有没有其他替代方法
help?> Base.Generator
Generator(f, iter)
Given a function f and an iterator iter, construct an iterator that yields the values of f applied to the elements of iter. The syntax f(x) for x
in iter [if cond(x)::Bool] is syntax for constructing an instance of this type. The [if cond(x)::Bool] expression is optional and acts as a
"guard", effectively filtering out values where the condition is false.
julia> g = (abs2(x) for x in 1:5 if x != 3);
julia> for x in g
println(x)
end
1
4
16
25
julia> collect(g)
4-element Array{Int64,1}:
1
4
16
25
Thank you,这个能用
julia> g = Base.Generator(x->x+1,Iterators.countfrom(1,1))
Base.Generator{Base.Iterators.Count{Int64},var"#99#100"}(var"#99#100"(), Base.Iterators.Count{Int64}(1, 1))
julia> collect(Iterators.take(g,10))
10-element Array{Int64,1}:
2
3
4
5
6
7
8
9
10
11