In a future version (1.6?) the following works:
julia> it = Iterators.product(1:3, 2:3)
Base.Iterators.ProductIterator{Tuple{UnitRange{Int64}, UnitRange{Int64}}}((1:3, 2:3))
julia> last(it, 1)
1-element Vector{Tuple{Int64, Int64}}:
(3, 3)
This was added in https://github.com/JuliaLang/julia/pull/34868
This works as long as the iterator implements reverse
. There was recently a discussion here about the difficulties of adding a generic last
method: Getting the last element of an iterator. I planned to submit a PR but as explained in that thread I couldn’t find an implementation that was performant and backward-compatible.
As a workaround @mschauer suggested foldl((_, y) -> y, itr)
, which also works fine here:
julia> foldl((_,y)->y, Iterators.product(1:3, 2:3))
(3, 3)