迭代实用程序
# *`基地。迭代器。有状态的`*-类型
Stateful(itr)
有几种不同的方式来思考这个迭代器包装器:
-
它提供了一个围绕迭代器及其迭代状态的可变包装器。
-
它把一个类似迭代器的抽象变成一个
频道-像抽象。 -
它是一个迭代器,每当产生一个项目时,它就会突变为自己的rest迭代器。
有状态的 提供常规迭代器接口。 像其他可变迭代器(例如 基地。频道),如果迭代提前停止(例如由 休息在一 为[循环,通过继续迭代同一个迭代器对象,可以从同一个点恢复迭代(相比之下,一个不可变的迭代器将从一开始重新启动)。
*例子*
julia> a = Iterators.Stateful("abcdef");
julia> isempty(a)
false
julia> popfirst!(a)
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)
julia> collect(Iterators.take(a, 3))
3-element Vector{Char}:
'b': ASCII/Unicode U+0062 (category Ll: Letter, lowercase)
'c': ASCII/Unicode U+0063 (category Ll: Letter, lowercase)
'd': ASCII/Unicode U+0064 (category Ll: Letter, lowercase)
julia> collect(a)
2-element Vector{Char}:
'e': ASCII/Unicode U+0065 (category Ll: Letter, lowercase)
'f': ASCII/Unicode U+0066 (category Ll: Letter, lowercase)
julia> Iterators.reset!(a); popfirst!(a)
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)
julia> Iterators.reset!(a, "hello"); popfirst!(a)
'h': ASCII/Unicode U+0068 (category Ll: Letter, lowercase)
julia> a = Iterators.Stateful([1,1,1,2,3,4]);
julia> for x in a; x == 1 || break; end
julia> peek(a)
3
julia> sum(a) # Sum the remaining elements
7
# *`基地。迭代器。拉链`*-函数
zip(iters...)
在同一时间运行多个迭代器,直到其中任何一个被耗尽。 的值类型 拉链 迭代器是其子iterators的值的元组。
|
注 |
|
注 |
*例子*
julia> a = 1:5
1:5
julia> b = ["e","d","b","c","a"]
5-element Vector{String}:
"e"
"d"
"b"
"c"
"a"
julia> c = zip(a,b)
zip(1:5, ["e", "d", "b", "c", "a"])
julia> length(c)
5
朱莉娅>第一(c)
(1,"e")
# *`基地。迭代器。枚举`*-函数
enumerate(iter)
产生的迭代器 (i,x) 哪里 i 是从1开始的计数器,并且 x 是 i来自给定迭代器的th值。 当您不仅需要值时,它很有用 x 您正在迭代的内容,以及迄今为止的迭代次数。
请注意 i 可能不适用于索引 伊特尔,或者可以索引不同的元素。 如果发生这种情况 伊特尔 具有不从1开始的索引,并且可能发生于字符串,字典等。 查看 对(IndexLinear(),iter) 方法如果你想确保 i 是索引。
*例子*
julia> a = ["a", "b", "c"];
julia> for (index, value) in enumerate(a)
println("$index $value")
end
1 a
2 b
3 c
julia> str = "naïve";
julia> for (i, val) in enumerate(str)
print("i = ", i, ", val = ", val, ", ")
try @show(str[i]) catch e println(e) end
end
i = 1, val = n, str[i] = 'n'
i = 2, val = a, str[i] = 'a'
i = 3, val = ï, str[i] = 'ï'
i = 4, val = v, StringIndexError("naïve", 4)
i = 5, val = e, str[i] = 'v'
# *`基地。迭代器。计数,计数`*-函数
countfrom(start=1, step=1)
一个永远计数的迭代器,从 开始 并通过增加 步骤.
*例子*
julia> for v in Iterators.countfrom(5, 2)
v > 10 && break
println(v)
end
5
7
9
# *`基地。迭代器。拿手机`*-函数
takewhile(pred, iter)
从…生成元素的迭代器 伊特尔 只要谓语 预审 是真的,之后,删除每个元素。
|
兼容性
Julia1.4此功能至少需要Julia1.4。 |
*例子*
julia> s = collect(1:5)
5-element Vector{Int64}:
1
2
3
4
5
julia> collect(Iterators.takewhile(<(3),s))
2-element Vector{Int64}:
1
2
# *`基地。迭代器。下降,下降`*-函数
drop(iter, n)
生成除第一个以外的所有迭代器 n 的元素 伊特尔.
*例子*
julia> a = 1:2:11
1:2:11
julia> collect(a)
6-element Vector{Int64}:
1
3
5
7
9
11
julia> collect(Iterators.drop(a,4))
2-element Vector{Int64}:
9
11
# *`基地。迭代器。[医]下降`*-函数
dropwhile(pred, iter)
从…中删除元素的迭代器 伊特尔 只要谓语 预审 是true,之后返回每个元素。
|
兼容性
Julia1.4此功能至少需要Julia1.4。 |
*例子*
julia> s = collect(1:5)
5-element Vector{Int64}:
1
2
3
4
5
julia> collect(Iterators.dropwhile(<(3),s))
3-element Vector{Int64}:
3
4
5
# *`基地。迭代器。周期`*-函数
cycle(iter[, n::Int])
循环遍历的迭代器 伊特尔 永遠!. 如果 n 被指定,然后它循环通过 伊特尔 很多次。 何时 伊特尔 是空的,也是空的 单车(iter) 和 周期(iter,n).
|
兼容性
Julia1.11方法 |
*例子*
julia> for (i, v) in enumerate(Iterators.cycle("hello"))
print(v)
i > 10 && break
end
hellohelloh
julia> foreach(print, Iterators.cycle(['j', 'u', 'l', 'i', 'a'], 3))
juliajuliajulia
julia> repeat([1,2,3], 4) == collect(Iterators.cycle([1,2,3], 4))
true
julia> fill([1,2,3], 4) == collect(Iterators.repeated([1,2,3], 4))
true
# *`基地。迭代器。重复`*-函数
repeated(x[, n::Int])
生成值的迭代器 x 永遠!. 如果 n 被指定,生成 x 那很多次(相当于 取(重复(x),n)).
*例子*
julia> a = Iterators.repeated([1 2], 4);
julia> collect(a)
4-element Vector{Matrix{Int64}}:
[1 2]
[1 2]
[1 2]
[1 2]
julia> ans == fill([1 2], 4)
true
julia> Iterators.cycle([1 2], 4) |> collect |> println
[1, 2, 1, 2, 1, 2, 1, 2]
# *`基地。迭代器。产品`*-函数
product(iters...)
返回多个迭代器的乘积的迭代器。 每个生成的元素都是一个元组,其 ith元素来自 ith参数迭代器。 第一个迭代器变化最快。
*例子*
julia> collect(Iterators.product(1:2, 3:5))
2×3 Matrix{Tuple{Int64, Int64}}:
(1, 3) (1, 4) (1, 5)
(2, 3) (2, 4) (2, 5)
julia> ans == [(x,y) for x in 1:2, y in 3:5] # collects a generator involving Iterators.product
true
# *`基地。迭代器。展平`*-函数
flatten(iter)
给定一个产生迭代器的迭代器,返回一个产生这些迭代器的元素的迭代器。 换句话说,参数迭代器的元素是串联的。
*例子*
julia> collect(Iterators.flatten((1:2, 8:9)))
4-element Vector{Int64}:
1
2
8
9
julia> [(x,y) for x in 0:1 for y in 'a':'c'] # collects generators involving Iterators.flatten
6-element Vector{Tuple{Int64, Char}}:
(0, 'a')
(0, 'b')
(0, 'c')
(1, 'a')
(1, 'b')
(1, 'c')
# *`基地。迭代器。平面地图`*-函数
Iterators.flatmap(f, iterators...)
相当于 flatten(map(f,iterators...)).
|
兼容性
Julia1.9这个功能是在Julia1.9中添加的。 |
*例子*
julia> Iterators.flatmap(n -> -n:2:n, 1:3) |> collect
9-element Vector{Int64}:
-1
1
-2
0
2
-3
-1
1
3
julia> stack(n -> -n:2:n, 1:3)
ERROR: DimensionMismatch: stack expects uniform slices, got axes(x) == (1:3,) while first had (1:2,)
[...]
julia> Iterators.flatmap(n -> (-n, 10n), 1:2) |> collect
4-element Vector{Int64}:
-1
10
-2
20
julia> ans == vec(stack(n -> (-n, 10n), 1:2))
true
# *`基地。迭代器。分区/分区`*-函数
partition(collection, n)
迭代一个集合 n 一次的元素。
*例子*
julia> collect(Iterators.partition([1,2,3,4,5], 2))
3-element Vector{SubArray{Int64, 1, Vector{Int64}, Tuple{UnitRange{Int64}}, true}}:
[1, 2]
[3, 4]
[5]
# *`基地。迭代器。地图`*-函数
Iterators.map(f, iterators...)
创建惰性映射。 这是编写的另一种语法 (f(args...)对于zip中的args(迭代器。..)).
|
兼容性
Julia1.6此功能至少需要Julia1.6。 |
*例子*
julia> collect(Iterators.map(x -> x^2, 1:3))
3-element Vector{Int64}:
1
4
9
# *`基地。迭代器。过滤器`*-函数
Iterators.filter(flt, itr)
给定谓词函数 flt 和一个可迭代对象 itr,返回一个可迭代对象,该对象在迭代时产生元素 x 的 itr 那就满足了 flt(x). 保留原始迭代器的顺序。
这个函数是_lazy_;也就是说,它保证返回 时间及用途 额外空间,以及 flt 不会被调用 过滤器. 致电 flt 将在迭代返回的可迭代对象时进行。 这些调用不会被缓存,重复调用时会进行重复调用。
|
警告从返回的迭代器上的后续_lazy_转换 |
见 基地。过滤器对于数组过滤的热切实现。
*例子*
julia> f = Iterators.filter(isodd, [1, 2, 3, 4, 5])
Base.Iterators.Filter{typeof(isodd), Vector{Int64}}(isodd, [1, 2, 3, 4, 5])
julia> foreach(println, f)
1
3
5
julia> [x for x in [1, 2, 3, 4, 5] if isodd(x)] # collects a generator over Iterators.filter
3-element Vector{Int64}:
1
3
5
# *`基地。迭代器。累积`*-函数
Iterators.accumulate(f, itr; [init])
给定一个2参数函数 f 和一个迭代器 itr,返回连续应用的新迭代器 f 到前一个值和下一个元素的 itr.
这实际上是一个懒惰的版本 基地。累积.
|
兼容性
Julia1.5关键字参数 |
*例子*
julia> a = Iterators.accumulate(+, [1,2,3,4]);
julia> foreach(println, a)
1
3
6
10
julia> b = Iterators.accumulate(/, (2, 5, 2, 5); init = 100);
julia> collect(b)
4-element Vector{Float64}:
50.0
10.0
5.0
1.0
# *`基地。迭代器。反向`*-函数
Iterators.reverse(itr)
给定一个迭代器 itr,则 反向(itr) 是同一个集合的迭代器,但顺序相反。 这个迭代器是"懒惰的",因为它不会复制集合以反转它;请参阅 基地。反向渴望实现。
(默认情况下,这将返回 迭代器。反向 对象包装 itr,这是可迭代的,如果相应的 迭代方法定义,但一些 itr 类型可能实现更专业化 迭代器。反向 行为。)
并非所有迭代器类型 T 支持逆序迭代。 如果 T 不,然后迭代 迭代器。反向(itr::T) 会抛出一个 方法;方法因为失踪 迭代 的方法 迭代器。反向{T}. (要实现这些方法,原始迭代器 itr::T 可以从一个 r::迭代器。反向{T} 反对 r.itr;更一般地说,人们可以使用 迭代器。反向(r).)
*例子*
julia> foreach(println, Iterators.reverse(1:5))
5
4
3
2
1
# *`基地。迭代器。只有`*-函数
only(x)
返回集合的唯一元素 x,或抛出一个 [医争论者]如果集合有零个或多个元素。
|
兼容性
Julia1.4此方法至少需要Julia1.4。 |
*例子*
julia> only(["a"])
"a"
julia> only("a")
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)
julia> only(())
ERROR: ArgumentError: Tuple contains 0 elements, must contain exactly 1 element
Stacktrace:
[...]
julia> only(('a', 'b'))
ERROR: ArgumentError: Tuple contains 2 elements, must contain exactly 1 element
Stacktrace:
[...]
# *`基地。迭代器。果皮`*-函数
peel(iter)
返回第一个元素和剩余元素的迭代器。
如果迭代器为空返回 什么都没有 (像 迭代).
|
兼容性
如果迭代器为空,Julia1.7之前的版本会抛出BoundsError。 |
*例子*
julia> (a, rest) = Iterators.peel("abc");
julia> a
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)
julia> collect(rest)
2-element Vector{Char}:
'b': ASCII/Unicode U+0062 (category Ll: Letter, lowercase)
'c': ASCII/Unicode U+0063 (category Ll: Letter, lowercase)