Skip to content

Commit a0a0a84

Browse files
committed
Add eachindex() for AbstractStrings and Associative
There's no reason not to allow this. Fixes #11649.
1 parent c147ecf commit a0a0a84

File tree

5 files changed

+24
-1
lines changed

5 files changed

+24
-1
lines changed

base/dict.jl

+1
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ in(k, v::KeyIterator) = !is(get(v.dict, k, secret_table_token),
192192
secret_table_token)
193193

194194
keys(a::Associative) = KeyIterator(a)
195+
eachindex(a::Associative) = KeyIterator(a)
195196
values(a::Associative) = ValueIterator(a)
196197

197198
function copy(a::Associative)

base/string.jl

+10
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,16 @@ function chr2ind(s::AbstractString, i::Integer)
181181
end
182182
end
183183

184+
immutable EachStringIndex{I}
185+
s::I
186+
end
187+
eachindex(s::AbstractString) = EachStringIndex(s)
188+
189+
length(e::EachStringIndex) = length(e.s)
190+
start(e::EachStringIndex) = start(e.s)
191+
next(e::EachStringIndex, state) = (state, nextind(e.s, state))
192+
done(e::EachStringIndex, state) = done(e.s, state)
193+
184194
typealias Chars Union{Char,AbstractVector{Char},Set{Char}}
185195

186196
function search(s::AbstractString, c::Chars, i::Integer)

doc/stdlib/arrays.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ Basic functions
3333

3434
.. function:: eachindex(A...)
3535

36-
Creates an iterable object for visiting each index of an AbstractArray ``A`` in an efficient manner. For array types that have opted into fast linear indexing (like ``Array``), this is simply the range ``1:length(A)``. For other array types, this returns a specialized Cartesian range to efficiently index into the array with indices specified for every dimension. Example for a sparse 2-d array::
36+
Creates an iterable object for visiting each index of an AbstractArray ``A`` in an efficient manner. For array types that have opted into fast linear indexing (like ``Array``), this is simply the range ``1:length(A)``. For other array types, this returns a specialized Cartesian range to efficiently index into the array with indices specified for every dimension. For other iterables, including strings and dictionaries, this returns an iterator object supporting arbitrary index types (e.g. unevenly spaced or non-integer indices).
37+
38+
Example for a sparse 2-d array::
3739

3840
julia> A = sprand(2, 3, 0.5)
3941
2x3 sparse matrix with 4 Float64 entries:

test/dict.jl

+5
Original file line numberDiff line numberDiff line change
@@ -333,3 +333,8 @@ let d = Dict{Int,Int}()
333333
end
334334
@test length(d) == 1
335335
end
336+
337+
# iteration
338+
d = Dict('a'=>1, 'b'=>1, 'c'=> 3)
339+
@test [d[k] for k in keys(d)] == [d[k] for k in eachindex(d)] ==
340+
[v for (k, v) in d] == [d[x[1]] for (i, x) in enumerate(d)]

test/strings.jl

+5
Original file line numberDiff line numberDiff line change
@@ -1892,3 +1892,8 @@ end
18921892
@test_throws BoundsError Base.checkstring(b"abcdef", 3, 0)
18931893
@test_throws BoundsError Base.checkstring(b"abcdef", 3, 7)
18941894
@test_throws ArgumentError Base.checkstring(b"abcdef", 3, 1)
1895+
1896+
# iteration
1897+
@test [c for c in "ḟøøƀäṙ"] == ['', 'ø', 'ø', 'ƀ', 'ä', '']
1898+
@test [i for i in eachindex("ḟøøƀäṙ")] == [1, 4, 6, 8, 10, 12]
1899+
@test [x for x in enumerate("ḟøøƀäṙ")] == [(1, ''), (2, 'ø'), (3, 'ø'), (4, 'ƀ'), (5, 'ä'), (6, '')]

0 commit comments

Comments
 (0)