Skip to content

Commit

Permalink
Add eachindex() fallback for arbitrary iterables
Browse files Browse the repository at this point in the history
Especially useful for strings, but also for any iterable
which would use unevenly spaced or non-integer indices.
Fixes #11649.
  • Loading branch information
nalimilan committed Jun 14, 2015
1 parent 1356899 commit 9fb0dae
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
12 changes: 12 additions & 0 deletions base/iterator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@

isempty(itr) = done(itr, start(itr))

# eachindex

immutable EachIndex{I}
itr::I
end
eachindex(itr) = EachIndex(itr)

length(e::EachIndex) = length(e.itr)
start(e::EachIndex) = start(e.itr)
next(e::EachIndex, state) = (state, next(e.itr, state)[2])
done(e::EachIndex, state) = done(e.itr, state)

# enumerate

immutable Enumerate{I}
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, 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
1 change: 1 addition & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,7 @@ let a36 = boo32_64()
end
@test isequal([1,2,3], [a for (a,b) in enumerate(2:4)])
@test isequal([2,3,4], [b for (a,b) in enumerate(2:4)])
@test isequal([1,2,3], [i for i in eachindex(2:4)])

@test_throws DomainError (10.^[-1])[1] == 0.1
@test (10.^[-1.])[1] == 0.1
Expand Down
4 changes: 4 additions & 0 deletions test/strings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1732,3 +1732,7 @@ d = UTF32String(c)
c[1] = 'A'
@test d=="A"

# 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 9fb0dae

Please sign in to comment.