last
belongs to BidirectionalCollection
. Chunked
conforms to BidirectionalCollection
if its base is bidirectional. Can you upgrade or adjust the base type to make it bidirectional?
The alternative is to write an inefficient algorithm like this:
extension Collection {
func slowLast() -> Element? {
var lastIndex: Index?
for index in self.indices {
lastIndex = index
}
return lastIndex.map({ self[$0] })
}
}
[Edit in response to your edit:]
That will allocate a whole new array in addition to iterating the entire collection. It will be even less efficient than the extension above.
It is logically sound though, so if performance doesn’t matter in your context, then go right ahead and use it.