# Getting the last element of an iterator

**URL:** https://discourse.julialang.org/t/getting-the-last-element-of-an-iterator/49696
**Category:** General Usage
**Tags:** question
**Created:** [November 6, 2020, 6:02pm UTC](https://discourse.julialang.org/t/getting-the-last-element-of-an-iterator/49696 "2020-11-06T18:02:17Z")
**Posts on this page:** 1
**Showing post:** 39

<div class="post-metadata">

### Author: ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)
#### Post date: [November 11, 2020, 8:38am UTC](https://discourse.julialang.org/t/getting-the-last-element-of-an-iterator/49696/39 "2020-11-11T08:38:48Z")

</div>

There’s a practical problem though in adding a generic `last` for iterators: I don’t see how to decide efficiently between calling `a[end]` (whenever possible) and calling the slow fallback.

There is no default trait for `getindex`/`lastindex` (no generic definition of `Base.IndexStyle` for example) and using `applicable(lastindex, a)` would incur an unacceptable performance penalty for something as basic as `last`…

---

_[View the full topic](https://discourse.julialang.org/t/getting-the-last-element-of-an-iterator/49696)._
