Iteration utilities
Base.Iterators.zip
— Function.zip(iters...)
For a set of iterable objects, returns an iterable of tuples, where the i
th tuple contains the i
th component of each input iterable.
Note that zip
is its own inverse: collect(zip(zip(a...)...)) == collect(a)
.
julia> a = 1:5
1:5
julia> b = ["e","d","b","c","a"]
5-element Array{String,1}:
"e"
"d"
"b"
"c"
"a"
julia> c = zip(a,b)
Base.Iterators.Zip2{UnitRange{Int64},Array{String,1}}(1:5, String["e", "d", "b", "c", "a"])
julia> length(c)
5
julia> first(c)
(1, "e")
Base.Iterators.enumerate
— Function.enumerate(iter)
An iterator that yields (i, x)
where i
is a counter starting at 1, and x
is the i
th value from the given iterator. It's useful when you need not only the values x
over which you are iterating, but also the number of iterations so far. Note that i
may not be valid for indexing iter
; it's also possible that x != iter[i]
, if iter
has indices that do not start at 1. See the enumerate(IndexLinear(), iter)
method if you want to ensure that i
is an index.
julia> a = ["a", "b", "c"];
julia> for (index, value) in enumerate(a)
println("$index $value")
end
1 a
2 b
3 c
enumerate(IndexLinear(), A)
enumerate(IndexCartesian(), A)
enumerate(IndexStyle(A), A)
An iterator that accesses each element of the array A
, returning (i, x)
, where i
is the index for the element and x = A[i]
. This is similar to enumerate(A)
, except i
will always be a valid index for A
.
Specifying IndexLinear()
ensures that i
will be an integer; specifying IndexCartesian()
ensures that i
will be a CartesianIndex
; specifying IndexStyle(A)
chooses whichever has been defined as the native indexing style for array A
.
julia> A = ["a" "d"; "b" "e"; "c" "f"];
julia> for (index, value) in enumerate(IndexStyle(A), A)
println("$index $value")
end
1 a
2 b
3 c
4 d
5 e
6 f
julia> S = view(A, 1:2, :);
julia> for (index, value) in enumerate(IndexStyle(S), S)
println("$index $value")
end
CartesianIndex{2}((1, 1)) a
CartesianIndex{2}((2, 1)) b
CartesianIndex{2}((1, 2)) d
CartesianIndex{2}((2, 2)) e
Note that enumerate(A)
returns i
as a counter (always starting at 1), whereas enumerate(IndexLinear(), A)
returns i
as an index (starting at the first linear index of A
, which may or may not be 1).
See also: IndexStyle
, indices
.
Base.Iterators.rest
— Function.rest(iter, state)
An iterator that yields the same elements as iter
, but starting at the given state
.
Base.Iterators.countfrom
— Function.countfrom(start=1, step=1)
An iterator that counts forever, starting at start
and incrementing by step
.
Base.Iterators.take
— Function.take(iter, n)
An iterator that generates at most the first n
elements of iter
.
julia> a = 1:2:11
1:2:11
julia> collect(a)
6-element Array{Int64,1}:
1
3
5
7
9
11
julia> collect(Iterators.take(a,3))
3-element Array{Int64,1}:
1
3
5
Base.Iterators.drop
— Function.drop(iter, n)
An iterator that generates all but the first n
elements of iter
.
julia> a = 1:2:11
1:2:11
julia> collect(a)
6-element Array{Int64,1}:
1
3
5
7
9
11
julia> collect(Iterators.drop(a,4))
2-element Array{Int64,1}:
9
11
Base.Iterators.cycle
— Function.cycle(iter)
An iterator that cycles through iter
forever.
Base.Iterators.repeated
— Function.repeated(x[, n::Int])
An iterator that generates the value x
forever. If n
is specified, generates x
that many times (equivalent to take(repeated(x), n)
).
julia> a = Iterators.repeated([1 2], 4);
julia> collect(a)
4-element Array{Array{Int64,2},1}:
[1 2]
[1 2]
[1 2]
[1 2]
Base.Iterators.product
— Function.product(iters...)
Returns an iterator over the product of several iterators. Each generated element is a tuple whose i
th element comes from the i
th argument iterator. The first iterator changes the fastest. Example:
julia> collect(Iterators.product(1:2,3:5))
2×3 Array{Tuple{Int64,Int64},2}:
(1, 3) (1, 4) (1, 5)
(2, 3) (2, 4) (2, 5)
Base.Iterators.flatten
— Function.flatten(iter)
Given an iterator that yields iterators, return an iterator that yields the elements of those iterators. Put differently, the elements of the argument iterator are concatenated. Example:
julia> collect(Iterators.flatten((1:2, 8:9)))
4-element Array{Int64,1}:
1
2
8
9
Base.Iterators.partition
— Function.partition(collection, n)
Iterate over a collection n
elements at a time.
julia> collect(Iterators.partition([1,2,3,4,5], 2))
3-element Array{Array{Int64,1},1}:
[1, 2]
[3, 4]
[5]