Skip to content

Commit

Permalink
only call next() once per iteration in Enumerate. fixes #6284
Browse files Browse the repository at this point in the history
this definition is now inlined too.
  • Loading branch information
JeffBezanson committed Apr 4, 2014
1 parent 23e8275 commit 54acf04
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 2 additions & 1 deletion base/iterator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ enumerate(itr) = Enumerate(itr)
length(e::Enumerate) = length(e.itr)
start(e::Enumerate) = (1, start(e.itr))
function next(e::Enumerate, state)
(state[1],next(e.itr,state[2])[1]), (state[1]+1,next(e.itr,state[2])[2])
n = next(e.itr,state[2])
(state[1],n[1]), (state[1]+1,n[2])
end
done(e::Enumerate, state) = done(e.itr, state[2])

Expand Down
8 changes: 8 additions & 0 deletions test/functional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,11 @@ end
# zip and filter iterators
# issue #4718
@test collect(filter(x->x[1], zip([true, false, true, false],"abcd"))) == {(true,'a'),(true,'c')}

# enumerate (issue #6284)
let b = IOBuffer("1\n2\n3\n"), a = {}
for (i,x) in enumerate(eachline(b))
push!(a, (i,x))
end
@test a == {(1,"1\n"),(2,"2\n"),(3,"3\n")}
end

0 comments on commit 54acf04

Please sign in to comment.