# 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:** 6\
**Page:** 3

<div class="post-metadata">

**Author:** ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)\
**Post date:** [November 11, 2020, 10:22am UTC](https://discourse.julialang.org/t/getting-the-last-element-of-an-iterator/49696/41 "2020-11-11T10:22:39Z")

</div>

Didn’t get `applicable` much more robust over the time? Because that tells whether there is `last`, after all.

---

<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, 11:09am UTC](https://discourse.julialang.org/t/getting-the-last-element-of-an-iterator/49696/42 "2020-11-11T11:09:14Z")

</div>

Types seem to be inferred, but comparing these two outputs:

```julia
mylast(a) = applicable(lastindex, a) ? a[end] : 0;
@code_native last([1,2,3])
@code_native mylast([1,2,3])

```

the second case adds the following operations:

```julia
	pushq	%rbx
	subq	$24, %rsp
	movq	%rdi, %rbx
	movabsq	$jl_system_image_data, %rax
	movq	%rax, -24(%rbp)
	movq	%rdi, -16(%rbp)
	movabsq	$jl_f_applicable, %rax
	leaq	-24(%rbp), %rsi
	xorl	%edi, %edi
	movl	$2, %edx
	callq	*%rax
	cmpb	$0, (%rax)
	je	L99

```

It seems too much for something that can often find itself in hot loops…

---

<div class="post-metadata">

**Author:** ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)\
**Post date:** [November 11, 2020, 12:44pm UTC](https://discourse.julialang.org/t/getting-the-last-element-of-an-iterator/49696/43 "2020-11-11T12:44:54Z")

</div>

You could make the decision at compile time

```julia
lastarg(_,x) = x
@generated mylast(a) = hasmethod(lastindex, (a,)) ? :(last(a)) : :(foldl(lastarg, a));

```

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [November 11, 2020, 1:06pm UTC](https://discourse.julialang.org/t/getting-the-last-element-of-an-iterator/49696/44 "2020-11-11T13:06:15Z")

</div>

That’s problematic, because `mylast` will not be updated if `lastindex` is redefined later. [GitHub - oxinabox/Tricks.jl: Cunning tricks though the julia compiler internals](https://github.com/oxinabox/Tricks.jl) can do this with backedges, but this should generally be avoided, since it can cause excessive invalidations. In this particular case, you can probably just use dispatch for this.

---

<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, 3:06pm UTC](https://discourse.julialang.org/t/getting-the-last-element-of-an-iterator/49696/45 "2020-11-11T15:06:56Z")

</div>

Indeed the [documentation](https://docs.julialang.org/en/v1/manual/metaprogramming/#Generated-functions) says explicitely:

> Generated functions must not _mutate_ or _observe_ any non-constant global state (including, for example, IO, locks, non-local dictionaries, or using [`hasmethod`](https://docs.julialang.org/en/v1/base/base/#Base.hasmethod)).

@simeonschaub how would you use dispatch here? Currently `last` is defined as

```julia
last(a) = a[end]

```

I could define this method only for `AbstractArray`, but that would not be backward compatible: the method would no longer be called for types that implement `getindex` and `lastindex` without deriving from `AbstractArray`…

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [November 12, 2021, 9:51pm UTC](https://discourse.julialang.org/t/getting-the-last-element-of-an-iterator/49696/46 "2021-11-12T21:51:31Z")

</div>

Incidentally I actually used `last` for some kind of fixed point iteration [here](https://discourse.julialang.org/t/trampolines/71351/6) and defining it without type constraint seemingly broke my Atom IDE 🙂

[Previous page](https://discourse.julialang.org/t/getting-the-last-element-of-an-iterator/49696.md?page=2)
