Reputation: 261
Is there something comparable to Python's "next" for going through a Julia iterable? I can't seem to find anything in the documentation.
Upvotes: 3
Views: 1117
Reputation: 904
Here is the closest you can achieve, using Stateful
from Base.Iterators
module:
Julia
julia> it = Iterators.Stateful((1, 2));
julia> popfirst!(it)
1
julia> popfirst!(it)
2
julia> popfirst!(it)
ERROR: EOFError: read end of file
Stacktrace:
[1] popfirst!(s::Base.Iterators.Stateful{Tuple{Int64, Int64}, Any})
@ Base.Iterators ./iterators.jl:1355
[2] top-level scope
@ REPL[5]:1
Python
>>> it = iter((1, 2))
>>> next(it)
1
>>> next(it)
2
>>> next(it)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
Upvotes: 2
Reputation: 175
https://docs.julialang.org/en/v1/base/collections/
next = iterate(iter)
(i, state) = next
Alternatively, it appears peek
will get the first element, but won't iterate.
Upvotes: 2