# Length from iterator?

**URL:** <https://discourse.julialang.org/t/length-from-iterator/27821>\
**Category:** New to Julia\
**Tags:** iterators\
**Created:** [August 21, 2019, 10:20pm UTC](https://discourse.julialang.org/t/length-from-iterator/27821 "2019-08-21T22:20:07Z")\
**Posts on this page:** 1\
**Showing post:** 20

<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:** [August 22, 2019, 9:46am UTC](https://discourse.julialang.org/t/length-from-iterator/27821/20 "2019-08-22T09:46:41Z")

</div>

Sometimes I found it useful to get the length and the last iterate, e.g. if the iterator does some numerical work until a convergence criteria holds.

So a generally useful function is:

```julia
    lastiterate(itr) = foldl((x, y) -> y, itr)

```

And then

```julia
julia> lastiterate('A':'Z')
'Z'

```

and

```julia
julia> lastiterate(enumerate('A':'Z'))
(26, 'Z')

```

---

_[View the full topic](https://discourse.julialang.org/t/length-from-iterator/27821)._
