# 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:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![gabomgp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gabomgp/32/1918_2.png) [@gabomgp](https://discourse.julialang.org/u/gabomgp)\
**Post date:** [August 21, 2019, 10:20pm UTC](https://discourse.julialang.org/t/length-from-iterator/27821/1 "2019-08-21T22:20:08Z")

</div>

I’m searching for a way to get the lenght from a iterator. The iterator can be consumed, is not problem. So, I’m find Query.jl, IterTools.jl and the iterator methods from base, but i’m not finding something to calculate the length from a iterator.

What i’m missing?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [August 21, 2019, 10:26pm UTC](https://discourse.julialang.org/t/length-from-iterator/27821/2 "2019-08-21T22:26:45Z")

</div>

Not all iterators have lengths. Some do, and if they do you can find it using `length(itr)`.

---

<div class="post-metadata">

**Author:** ![gabomgp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gabomgp/32/1918_2.png) [@gabomgp](https://discourse.julialang.org/u/gabomgp)\
**Post date:** [August 21, 2019, 10:28pm UTC](https://discourse.julialang.org/t/length-from-iterator/27821/3 "2019-08-21T22:28:37Z")

</div>

Is not a equivalent to C# Enumerable.Count?:

> **[Enumerable.Count Method (System.Linq)](https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.count?view=netframework-4.8)**
>
> Returns the number of elements in a sequence.

In this method, is not important the type of the source, simply the enumerator (iterator) is consumed and the element are counted.

---

<div class="post-metadata">

**Author:** ![gabomgp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gabomgp/32/1918_2.png) [@gabomgp](https://discourse.julialang.org/u/gabomgp)\
**Post date:** [August 21, 2019, 10:32pm UTC](https://discourse.julialang.org/t/length-from-iterator/27821/4 "2019-08-21T22:32:59Z")

</div>

and… are there a package or functions from base to get aggregates from a iterator (consuming it), as:

- sum
- count
- mean  
etc?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [August 21, 2019, 10:36pm UTC](https://discourse.julialang.org/t/length-from-iterator/27821/5 "2019-08-21T22:36:39Z")

</div>

`sum` and other functions will take in iterators, but some iterators such as `x=countfrom(1)` are infinite length since iterators are lazy.

---

<div class="post-metadata">

**Author:** ![gabomgp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gabomgp/32/1918_2.png) [@gabomgp](https://discourse.julialang.org/u/gabomgp)\
**Post date:** [August 21, 2019, 10:41pm UTC](https://discourse.julialang.org/t/length-from-iterator/27821/6 "2019-08-21T22:41:14Z")

</div>

Thanks,

and for lenght in particular, is posible to use it with any iterator, and the function counts the elements consuming the iterator?

---

<div class="post-metadata">

**Author:** ![gabomgp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gabomgp/32/1918_2.png) [@gabomgp](https://discourse.julialang.org/u/gabomgp)\
**Post date:** [August 21, 2019, 10:47pm UTC](https://discourse.julialang.org/t/length-from-iterator/27821/7 "2019-08-21T22:47:14Z")

</div>

this response confuse me:

[https://github.com/JuliaLang/julia/issues/13772#issuecomment-151126890](https://github.com/JuliaLang/julia/issues/13772#issuecomment-151126890)

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [August 21, 2019, 10:53pm UTC](https://discourse.julialang.org/t/length-from-iterator/27821/8 "2019-08-21T22:53:58Z")

</div>

> [@gabomgp](#):
>
> and for lenght in particular, is posible to use it with any iterator, and the function counts the elements consuming the iterator?

is not a fast operation, but you can define your own counter:

```julia
function mylength(iter)
n=0
    for i in iter  
        n+=1
    end
return n
end

```

the arguments in the github are valid in my opinion, what happens if an iterator has a side effect? what if it is a non-terminating one?. if an custom iterator type has a length, is necessary to define it, in my opinion

---

<div class="post-metadata">

**Author:** ![gabomgp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gabomgp/32/1918_2.png) [@gabomgp](https://discourse.julialang.org/u/gabomgp)\
**Post date:** [August 21, 2019, 11:08pm UTC](https://discourse.julialang.org/t/length-from-iterator/27821/9 "2019-08-21T23:08:46Z")

</div>

Yeah, but in my experience counting elements from a iterator (consuming it) is a very common operation and if that has side effects its something normal. I suppose it is a habit.

---

<div class="post-metadata">

**Author:** ![gabomgp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gabomgp/32/1918_2.png) [@gabomgp](https://discourse.julialang.org/u/gabomgp)\
**Post date:** [August 21, 2019, 11:43pm UTC](https://discourse.julialang.org/t/length-from-iterator/27821/10 "2019-08-21T23:43:53Z")

</div>

and, in my opinion length is a aggregate as any other. so, if sum (a aggregate) works with iterators (consuming it), why not length?

---

<div class="post-metadata">

**Author:** ![o314](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/o314/32/252_2.png) [@o314](https://discourse.julialang.org/u/o314)\
**Post date:** [August 22, 2019, 12:00am UTC](https://discourse.julialang.org/t/length-from-iterator/27821/11 "2019-08-22T00:00:53Z")

</div>

You could use

```julia
ilength(itr) = foldl((a,_)->a+1,itr)
@test ilength(1:10) == 10

```

function was named ilen, not len since dispatching on itr is not well type-defined.  
may be have a look to `count`. IterTools.jl package too  
o(n) complexity expected.

I am proponent to move Iterators in stdlib adding some missing funcs like takewhile, dropwhile, compress, imap, ilength from IterTools

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [August 22, 2019, 12:00am UTC](https://discourse.julialang.org/t/length-from-iterator/27821/12 "2019-08-22T00:00:57Z")

</div>

Just wondering, when is it useful to get the length of an iteratior, but not any of the elements?

---

<div class="post-metadata">

**Author:** ![gabomgp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gabomgp/32/1918_2.png) [@gabomgp](https://discourse.julialang.org/u/gabomgp)\
**Post date:** [August 22, 2019, 12:07am UTC](https://discourse.julialang.org/t/length-from-iterator/27821/13 "2019-08-22T00:07:01Z")

</div>

I imagine someting equivalent to this in C#:

```
var list= new[] {1,2,3,4,5}
return list.Where(it=>it%2==0).Count()

```

I imagine in Julia it’s easy to do this type of calculations in streamming, without creating DataFrames, when i’m doing explorations over the data.

PD: It’s only a example, the real thing its much more complicated and with very large CSV’s, so, DataFrames is not a option. And i can cache in a dataframe a small set of data to do the develop loop fast, but in production i want to stream the data.

---

<div class="post-metadata">

**Author:** ![rapus95](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rapus95/32/3773_2.png) [@rapus95](https://discourse.julialang.org/u/rapus95)\
**Post date:** [August 22, 2019, 1:09am UTC](https://discourse.julialang.org/t/length-from-iterator/27821/14 "2019-08-22T01:09:24Z")

</div>

I would set up that example in Julia as follows:

```julia
list=1:5
return count(iseven,list)

```

And in case of a quite large file I’d go that way:

```julia
#file.txt is a large textfile consisting of string lines
count(x->length(x)<40, eachline("file.txt"))

```

`eachline` is an iterator.

---

<div class="post-metadata">

**Author:** ![gabomgp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gabomgp/32/1918_2.png) [@gabomgp](https://discourse.julialang.org/u/gabomgp)\
**Post date:** [August 22, 2019, 1:12am UTC](https://discourse.julialang.org/t/length-from-iterator/27821/16 "2019-08-22T01:12:18Z")

</div>

This solution is very good, thanks you.

---

<div class="post-metadata">

**Author:** ![rapus95](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rapus95/32/3773_2.png) [@rapus95](https://discourse.julialang.org/u/rapus95)\
**Post date:** [August 22, 2019, 1:13am UTC](https://discourse.julialang.org/t/length-from-iterator/27821/17 "2019-08-22T01:13:47Z")

</div>

You’re welcome 🙂

Btw do you still need a function to calculate the length of an iterator by consuming it (if necessary)? And if yes, what would you like it to return if it is known to have infinite length?

---

<div class="post-metadata">

**Author:** ![gabomgp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gabomgp/32/1918_2.png) [@gabomgp](https://discourse.julialang.org/u/gabomgp)\
**Post date:** [August 22, 2019, 1:16am UTC](https://discourse.julialang.org/t/length-from-iterator/27821/18 "2019-08-22T01:16:03Z")

</div>

As in C#, the simple response, the function doesn’t returns and stay in a infinite loop. It’s the behavior that i expected.

---

<div class="post-metadata">

**Author:** ![gabomgp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gabomgp/32/1918_2.png) [@gabomgp](https://discourse.julialang.org/u/gabomgp)\
**Post date:** [August 22, 2019, 1:16am UTC](https://discourse.julialang.org/t/length-from-iterator/27821/19 "2019-08-22T01:16:34Z")

</div>

But, for my specifc use case, your solution is good.

---

<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')

```

---

<div class="post-metadata">

**Author:** ![traktofon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/traktofon/32/591_2.png) [@traktofon](https://discourse.julialang.org/u/traktofon)\
**Post date:** [August 22, 2019, 11:14am UTC](https://discourse.julialang.org/t/length-from-iterator/27821/21 "2019-08-22T11:14:42Z")

</div>

According to [Interfaces · The Julia Language](https://docs.julialang.org/en/v1/manual/interfaces/) you can try to query whether the iterator `itr` has a length or not with `Base.IteratorSize(itr)`. If this returns a `Base.HasLength` or a `Base.HasShape`, then the `itr` should provide a `length` method for querying the number of items.

Though I have to admit that this may not be reliable:

1. `Base.IteratorSize` is marked as optional. Moreover, there’s a fallback method that returns `Base.HasLength()` even though there may not be a `length` method. Example: `struct A; end; a=A(); Base.IteratorSize(a)` returns `Base.HasLength()` but `length(a)` errors (of course).
2. These methods need to be qualified with `Base.` as they are not exported, hinting that they may not be considered official interfaces, although they are documented in the interfaces section of the manual.

[Next page](https://discourse.julialang.org/t/length-from-iterator/27821.md?page=2)
