# Lazy Iteration

**URL:** <https://discourse.julialang.org/t/lazy-iteration/22933>\
**Category:** General Usage\
**Created:** [April 8, 2019, 7:22pm UTC](https://discourse.julialang.org/t/lazy-iteration/22933 "2019-04-08T19:22:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![srb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/srb/32/6812_2.png) [@srb](https://discourse.julialang.org/u/srb)\
**Post date:** [April 8, 2019, 7:22pm UTC](https://discourse.julialang.org/t/lazy-iteration/22933/1 "2019-04-08T19:22:37Z")

</div>

I have defined a certain type that can hold arbitrarily many values, and I have already implemented an `iterate()` method for it. However, it seems to me that this method allocates all of the iterators elements in memory since it made me implement a `Base.length(MyType)` method in order for it to work.

Is there a way in Julia to implement an Iterator that does not allocate its objects in memory until they are needed? Given that I already have an iterator that calculates elements based on the state of it.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [April 8, 2019, 7:24pm UTC](https://discourse.julialang.org/t/lazy-iteration/22933/2 "2019-04-08T19:24:36Z")

</div>

> [@srb](#):
>
> However, it seems to me that this method allocates all of the iterators elements in memory since it made me implement a `Base.length(MyType)` method in order for it to work.

If `length` can be easily computed it allows various optimizations (like preallocation) when the iterator is materialized (like with `collect`). But it will not hold everything in memory when just iterating over it. Each item is retrieved sequentially with a call to `iterate`.

---

<div class="post-metadata">

**Author:** ![srb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/srb/32/6812_2.png) [@srb](https://discourse.julialang.org/u/srb)\
**Post date:** [April 8, 2019, 7:34pm UTC](https://discourse.julialang.org/t/lazy-iteration/22933/3 "2019-04-08T19:34:09Z")

</div>

I tried printing my type in a for loop and it appears this is correct. However, do you know if the `cycle` and `drop` methods cause the iterator to materialize? (This is what caused me to ask the question in the first place, as it was the methods I was using)

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [April 8, 2019, 7:35pm UTC](https://discourse.julialang.org/t/lazy-iteration/22933/4 "2019-04-08T19:35:20Z")

</div>

> [@srb](#):
>
> However, do you know if the `cycle` and `drop` methods cause the iterator to materialize?

They do not, they are “wrapper” iterators, implemented by calling `iterate` on the iterator they wrap.

---

<div class="post-metadata">

**Author:** ![Orbots](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/orbots/32/3392_2.png) [@Orbots](https://discourse.julialang.org/u/Orbots)\
**Post date:** [April 8, 2019, 8:17pm UTC](https://discourse.julialang.org/t/lazy-iteration/22933/5 "2019-04-08T20:17:08Z")

</div>

I’ve been using `Base.SizeUnknown()` for iterators where it’s just as expensive to calculate length as to iterate over the entire collection.

```
Base.IteratorSize(::Type{OneRing}) = Base.SizeUnknown()

```
