reverse

Returns the source in reverse order.

Iterable<A> reverse<A>(Iterable<A> iterable) FxAsyncIterable<A> reverseAsync<A>(FxAsyncIterable<A> iterable) Fx<T> Fx.reverse() // chain FxAsync<T> FxAsync.reverse()

Lecture

reverse yields a source's elements last-to-first. Like takeRight and dropRight, there's no way to know what "last" means without first seeing everything, so reverse materializes the whole source into a list before it yields a single value. It's an operator for finite, bufferable sources — it will never terminate on an infinite one.

reverseAsync follows the same rule: it awaits every element of the upstream first, then replays them back to front. If you only need the tail of a sequence rather than a true reversal, prefer takeRight — it materializes too, but at least stops as soon as it has the values it needs to output in order.

Demo 1 · Basics

Demo 2 · Async (must buffer the whole source first)

Try it yourself

Exercise: reverse the history so the most recent visit comes first.

Related: takeRight — also materializes, only needs the tail · dropRight · sort — also materializes into a new list