Julia: reverse iteration protocol

Created on 2 Oct 2017  路  6Comments  路  Source: JuliaLang/julia

As has been mentioned a few times, e.g. in #4590, #6165, and #23969, it would be nice if there were a standard reverse iteration protocol that iterators could support (if possible). @StefanKarpinski suggested rstart, prev, and rdone as not-terrible possibilities.

You could then do e.g. for x in Reverse(itr) and Reverse (unlike reverse) would implement non-materializing reverse iteration by calling rstart etc. on itr.

collections design

Most helpful comment

Why not just have Reverse be a wrapper type and dispatch on start(::Reverse{...}).

All 6 comments

Why not just have Reverse be a wrapper type and dispatch on start(::Reverse{...}).

If Reverse would it just error on infinite iterators?

@oscardssmith, yes, you'd get an error (probably MethodError) for any iterator where reverse iteration is impossible/impractical/unimplemented.

I favor Keno's solution, since it also scales to different kinds of iterator orders. e.g, what if an iterator has efficient support for only iterating the even elements? Do we then need evenstart in Base?

Also, I don't remember the issue, but there is an attempt to remove as many of these 'combinatorial' (in this case, 'reverse' x 'start, done') function names in Base as possible in favor of relying on dispatch.

If we do this, that would likely simplify some stuff in my RevString excision, though we're essentially reintroducing a RevString-like type.

@ararslan, not quite, because Reverse will be a subtype of Any.

Was this page helpful?
0 / 5 - 0 ratings