How to get the "last" element of a Collection?

I use chunks(ofCount:) to break my array into groups, now I have a bunch of Collection's (whatever chunks produces). I can do:

c.dropLast(1)

but I can find no way to now get the last element, there is no .last? How to get the last element of a collection?

I'm doing this for now:

$0.reversed().first

it works (I know for sure there is at least one element). But seem kind wrong?

1 Like

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.

I got this error after using BidirectionalCollection:

Referencing property 'last' on 'ChunkedByCount' requires that 'Self' conform to 'RandomAccessCollection'

So I change to RandomAccessCollection and it works now. Why is it want RandomAccessCollection and BidirectionalCollection is not good enough?

Can the compiler give guidance to my original problem:

“You want to use .last? Upgrade to RandomAccessCollection”

1 Like

Because ChunkedByCount is declared that way, hinging its BidirectionalCollection conformance on its base’s RandomAccessCollection conformance. (Earlier I thought you were using the Chunked type, which is not constrained as tightly.)

Maybe. What exactly did the original error message say?

I didn't see .last in the auto complete list. So I didn't even try using .last. But had I just call .last and the error message clearly tells me what's going on:

Referencing property 'last' on 'ChunkedByCount' requires that 'Self' conform to 'RandomAccessCollection'

So it's my own fault (I think).

Edit: maybe the auto complete can be a bit more "helpful" and offer addition choices like .last and say "if 'Self' conform to "such and such"..."