Continuing the discussion from SE-0231 — Optional iteration:
It's not just that some collections can't be reversed, though that's part of it.
The thing to bear in mind is that
reversed() needs to be implemented both generically and as efficiently as possible. We don't want every single collection type to have to implement it itself by hand. And whenever possible, it would want to be able to return a reversed version in constant time. For bidirectional collections you can implement a generic efficient constant-time (almost "for free") reversal by returning a generic wrapper that just wraps
Self and reverses the direction of iteration.
Many types are able to return
reversed, but not in constant time. Reversing a
String into another
String takes linear time. You could even write a generic linear-time
reversed on any
RangeReplaceableCollection. But putting
String inside a
ReversedCollection takes constant time.
And this cannot be done generically at all for unidirectional collections or sequences.
Range could in theory reverse itself in constant time (assuming you wrote it to count down from a greater lower bound). But how would you express that capability generically, on what protocol? And how would you reverse
UnfoldSequence generically? It can only really be done on a case-by-case basis.