Getting the last element of an iterator

I also don’t understand this focus on the complexity guarantee… It seems very peculiar to last, and generally doesn’t feel Julian to forbid implementation of generally useful methods like this.

@Henrique_Becker please consider the possibility that the documentation was poorly worded and misunderstood. Maybe as @apo383 suggests it was never the intention to forbid last in the O(n) case.

That would also explain the inconsistency with lastindex which says nothing about O(1). Consider again the LinkedList case. It’s not even necessary to inherit AbstractArray:

import Base: getindex, lastindex
struct LinkedList end
getindex(::LinkedList, ::Int) = ...
lastindex(::LinkedList) = ...

Reasonable code, no inheritance, and it gives a “slow” last.

Also look at this excerpt from HISTORY.md:

New function ncodeunits(s::AbstractString) gives the number of code units in a string. The generic definition is constant time but calls lastindex(s) which may be inefficient.

This all suggests that this weird constraint in the documentation of last was never meant as a constraint.

1 Like