Getting the last element of an iterator

The most general method for iterators that should be O(1) is

first(Iterators.reverse(iterator))

which works for any iterator type that has implemented reverse iteration.

However, currently eachline has no implementation of reverse-order iteration, so this throws a MethodError. In principle, it would be straightforward to implement reverse-order eachline for files, but it doesn’t seem possible to do efficiently for pipes.

(There is an Iterators.last(iterator, n) iterator that uses this reverse approach.)

9 Likes