Skip to content

Commit

Permalink
Merge pull request #11708 from JuliaLang/nl/eachindex
Browse files Browse the repository at this point in the history
Add eachindex() for AbstractString and Associative
  • Loading branch information
nalimilan committed Jun 21, 2015
2 parents cb77503 + 9ef327d commit a94da23
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions base/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ in(k, v::KeyIterator) = !is(get(v.dict, k, secret_table_token),
secret_table_token)

keys(a::Associative) = KeyIterator(a)
eachindex(a::Associative) = KeyIterator(a)
values(a::Associative) = ValueIterator(a)

function copy(a::Associative)
Expand Down
11 changes: 11 additions & 0 deletions base/string.jl
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,17 @@ function chr2ind(s::AbstractString, i::Integer)
end
end

immutable EachStringIndex{T<:AbstractString}
s::T
end
eachindex(s::AbstractString) = EachStringIndex(s)

length(e::EachStringIndex) = length(e.s)
start(e::EachStringIndex) = start(e.s)
next(e::EachStringIndex, state) = (state, nextind(e.s, state))
done(e::EachStringIndex, state) = done(e.s, state)
eltype(e::EachStringIndex) = Int

typealias Chars Union{Char,AbstractVector{Char},Set{Char}}

function search(s::AbstractString, c::Chars, i::Integer)
Expand Down
4 changes: 3 additions & 1 deletion doc/stdlib/arrays.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ Basic functions

.. function:: eachindex(A...)

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::
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).

Example for a sparse 2-d array::

julia> A = sprand(2, 3, 0.5)
2x3 sparse matrix with 4 Float64 entries:
Expand Down
5 changes: 5 additions & 0 deletions test/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,8 @@ let d = Dict{Int,Int}()
end
@test length(d) == 1
end

# iteration
d = Dict('a'=>1, 'b'=>1, 'c'=> 3)
@test [d[k] for k in keys(d)] == [d[k] for k in eachindex(d)] ==
[v for (k, v) in d] == [d[x[1]] for (i, x) in enumerate(d)]
5 changes: 5 additions & 0 deletions test/strings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1892,3 +1892,8 @@ end
@test_throws BoundsError Base.checkstring(b"abcdef", 3, 0)
@test_throws BoundsError Base.checkstring(b"abcdef", 3, 7)
@test_throws ArgumentError Base.checkstring(b"abcdef", 3, 1)

# iteration
@test [c for c in "ḟøøƀäṙ"] == ['', 'ø', 'ø', 'ƀ', 'ä', '']
@test [i for i in eachindex("ḟøøƀäṙ")] == [1, 4, 6, 8, 10, 12]
@test [x for x in enumerate("ḟøøƀäṙ")] == [(1, ''), (2, 'ø'), (3, 'ø'), (4, 'ƀ'), (5, 'ä'), (6, '')]

0 comments on commit a94da23

Please sign in to comment.