Skip to content

Commit baa85f4

Browse files
authored
See also axes from eachindex's docstring, etc. (JuliaLang#45356)
* mention pairs and axes in eachindex's docstring
1 parent 99e8953 commit baa85f4

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

base/abstractarray.jl

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ julia> A = fill(1, (5,6,7));
5555
5656
julia> axes(A, 2)
5757
Base.OneTo(6)
58+
59+
julia> axes(A, 4) == 1:1 # all dimensions d > ndims(A) have size 1
60+
true
5861
```
5962
6063
# Usage note
@@ -322,17 +325,21 @@ if all inputs have fast linear indexing, a [`CartesianIndices`](@ref)
322325
otherwise).
323326
If the arrays have different sizes and/or dimensionalities, a `DimensionMismatch` exception
324327
will be thrown.
328+
329+
See also [`pairs`](@ref)`(A)` to iterate over indices and values together,
330+
and [`axes`](@ref)`(A, 2)` for valid indices along one dimension.
331+
325332
# Examples
326333
```jldoctest
327-
julia> A = [1 2; 3 4];
334+
julia> A = [10 20; 30 40];
328335
329336
julia> for i in eachindex(A) # linear indexing
330-
println(i)
337+
println("A[", i, "] == ", A[i])
331338
end
332-
1
333-
2
334-
3
335-
4
339+
A[1] == 10
340+
A[2] == 30
341+
A[3] == 20
342+
A[4] == 40
336343
337344
julia> for i in eachindex(view(A, 1:2, 1:1)) # Cartesian indexing
338345
println(i)

base/iterators.jl

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,12 @@ end
162162
An iterator that yields `(i, x)` where `i` is a counter starting at 1,
163163
and `x` is the `i`th value from the given iterator. It's useful when
164164
you need not only the values `x` over which you are iterating, but
165-
also the number of iterations so far. Note that `i` may not be valid
166-
for indexing `iter`; it's also possible that `x != iter[i]`, if `iter`
167-
has indices that do not start at 1. See the `pairs(IndexLinear(),
168-
iter)` method if you want to ensure that `i` is an index.
165+
also the number of iterations so far.
166+
167+
Note that `i` may not be valid for indexing `iter`, or may index a
168+
different element. This will happen if `iter` has indices that do not
169+
start at 1, and may happen for strings, dictionaries, etc.
170+
See the `pairs(IndexLinear(), iter)` method if you want to ensure that `i` is an index.
169171
170172
# Examples
171173
```jldoctest
@@ -177,6 +179,18 @@ julia> for (index, value) in enumerate(a)
177179
1 a
178180
2 b
179181
3 c
182+
183+
julia> str = "naïve";
184+
185+
julia> for (i, val) in enumerate(str)
186+
print("i = ", i, ", val = ", val, ", ")
187+
try @show(str[i]) catch e println(e) end
188+
end
189+
i = 1, val = n, str[i] = 'n'
190+
i = 2, val = a, str[i] = 'a'
191+
i = 3, val = ï, str[i] = 'ï'
192+
i = 4, val = v, StringIndexError("naïve", 4)
193+
i = 5, val = e, str[i] = 'v'
180194
```
181195
"""
182196
enumerate(iter) = Enumerate(iter)

0 commit comments

Comments
 (0)