# Implementing iterative algorithms using Base.Iterators concepts

**URL:** <https://discourse.julialang.org/t/implementing-iterative-algorithms-using-base-iterators-concepts/62575>\
**Category:** General Usage\
**Created:** [June 8, 2021, 12:44pm UTC](https://discourse.julialang.org/t/implementing-iterative-algorithms-using-base-iterators-concepts/62575 "2021-06-08T12:44:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![imcinerney](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/imcinerney/32/9882_2.png) [@imcinerney](https://discourse.julialang.org/u/imcinerney)\
**Post date:** [June 8, 2021, 12:44pm UTC](https://discourse.julialang.org/t/implementing-iterative-algorithms-using-base-iterators-concepts/62575/1 "2021-06-08T12:44:29Z")

</div>

I was noticing that some packages implement their iterative algorithms using the `Base.Iterators` methods by defining the algorithm iteration inside an `iterate` function and then using that to iterate it. Others implement explicit for/while loops to iterate the algorithm.

That got me wondering, is there a benefit to using the `Base.Iterators` approach over writing out explicit loops for the algorithm?

---

<div class="post-metadata">

**Author:** ![DrChainsaw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drchainsaw/32/8497_2.png) [@DrChainsaw](https://discourse.julialang.org/u/DrChainsaw)\
**Post date:** [June 8, 2021, 4:16pm UTC](https://discourse.julialang.org/t/implementing-iterative-algorithms-using-base-iterators-concepts/62575/2 "2021-06-08T16:16:54Z")

</div>

Implementing `Base.iterate` is what enables the `for` syntax. I’m not sure what you mean by explicit for/while loops, but I’m thinking you refer to when indexing is possible, like looping over an array using something like `for i in 1:length(array)`.

One advantage of `iterate` is that you don’t have to support indexing which often is more complicated than iteration. Sometimes it also just makes alot of sense from an API point of view.

Contrived example with an utterly pointless iterator which represents an infinite sequence of random numbers just to show an example when indexing is not needed:

```julia
julia> struct RandSeq
       seed::Int
       end

julia> import Random

julia> Base.iterate(itr::RandSeq, state=Random.MersenneTwister(itr.seed)) = return randn(state), state

julia> randseq = RandSeq(1)
RandSeq(1)

julia> for v in Iterators.Take(randseq, 5)
       @show v
       end
v = 0.2972879845354616
v = 0.3823959677906078
v = -0.5976344767282311
v = -0.01044524463737564
v = -0.839026854388764

julia> for v in Iterators.Take(randseq, 5)
       @show v
       end
v = 0.2972879845354616
v = 0.3823959677906078
v = -0.5976344767282311
v = -0.01044524463737564
v = -0.839026854388764

```

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [June 8, 2021, 4:19pm UTC](https://discourse.julialang.org/t/implementing-iterative-algorithms-using-base-iterators-concepts/62575/3 "2021-06-08T16:19:46Z")

</div>

An interesting blog post on the subject  
[https://lostella.github.io/2018/07/25/iterative-methods-done-right.html](https://lostella.github.io/2018/07/25/iterative-methods-done-right.html)

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [June 10, 2021, 4:24am UTC](https://discourse.julialang.org/t/implementing-iterative-algorithms-using-base-iterators-concepts/62575/4 "2021-06-10T04:24:24Z")

</div>

that’s really. are there performance penalties at all?
