Is there a version of "enumerate" in the reverse order?

I would like to enumerate an iterator with known length in reverse order. Something like:

julia> for t in enumerate(["a","b","c"], reverse=true); println(t); end
(3, "c")
(2, "b")
(1, "a")

Is there something builtin? For the moment, I build a vector and reverse it and iterate and change the index with “length - i” but it’s bad because I do not benefit from it being an iterator.

julia> for t in Iterators.reverse(enumerate(["a","b","c"])); println(t); end
(3, "c")
(2, "b")
(1, "a")
10 Likes

love it :heart:

I just stumbled on this:

julia> reverse(enumerate([1,2,3]))
ERROR: MethodError: no method matching reverse(::Base.Iterators.Enumerate{Vector{Int64}})

Closest candidates are:
  reverse(::Tuple)
   @ Base tuple.jl:562
  reverse(::Pair{A, B}) where {A, B}
   @ Base pair.jl:53
  reverse(::NamedTuple)
   @ Base namedtuple.jl:324
  ...

Stacktrace:
 [1] top-level scope
   @ REPL[1]:1

julia> Iterators.reverse(enumerate([1,2,3]))
Base.Iterators.Reverse{Base.Iterators.Enumerate{Vector{Int64}}}(enumerate([1, 2, 3]))

Is there a reason/point-of-conflict for Iterators.reverse not being exported and I have to specify or could this be changed?