Stream: helpdesk (published)

Topic: Reverse enumerate


view this post on Zulip Adrian Hill (Oct 12 2021 at 14:43):

I was wondering why the following doesn't work "out of the box":

julia> for (i, c) in reverse(enumerate(["a", "b", "c"]))
           print(i, c)
       end
ERROR: MethodError: no method matching reverse(::Base.Iterators.Enumerate{Vector{String}})
Closest candidates are:
  reverse(::Tuple) at tuple.jl:488
  reverse(::Pair{A, B}) where {A, B} at pair.jl:48
  reverse(::Union{SubString{String}, String}) at strings/substring.jl:166
  ...
Stacktrace:
 [1] top-level scope
   @ ./REPL[44]:1

julia> using Base.Iterators: Reverse

julia> for (i, c) in Reverse(enumerate(["a", "b", "c"]))
           print(i, c)
       end
3c2b1a

I am on 1.7.0-rc1. Is this worth opening an issue or making a PR?

view this post on Zulip Fredrik Ekre (Oct 12 2021 at 14:46):

You can use Iterators.reverse


Last updated: Dec 28 2024 at 04:38 UTC