# Markov chain as an iterable

**URL:** <https://discourse.julialang.org/t/markov-chain-as-an-iterable/13495>\
**Category:** General Usage\
**Tags:** question\
**Created:** [August 15, 2018, 12:18pm UTC](https://discourse.julialang.org/t/markov-chain-as-an-iterable/13495 "2018-08-15T12:18:59Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 15, 2018, 12:18pm UTC](https://discourse.julialang.org/t/markov-chain-as-an-iterable/13495/1 "2018-08-15T12:18:59Z")

</div>

I want to provide an iteration interface for a Markov chain, eg MWE:

```julia
using Random

struct RandomWalk{G, T}
    σ::T
    rng::G
end

RandomWalk(σ) = RandomWalk(σ, Random.GLOBAL_RNG)

Base.iterate(rw::RandomWalk, x = 0.0) = x, x + rw.σ*randn()
Base.IteratorSize(::RandomWalk) = Base.IsInfinite()
collect(Iterators.take(RandomWalk(0.2), 10))

```

The only question is how to specify the initial state. The only solution I could come up with is a wrapper type

```julia
struct RandomWalkInit{R <: RandomWalk,S}
    rw::R
    x₀::S
end

Base.iterate(rwi::RandomWalkInit, x = rwi.x₀) = Base.iterate(rwi.rw, x)
Base.IteratorSize(::RandomWalkInit) = Base.IsInfinite()
collect(Iterators.take(RandomWalkInit(RandomWalk(0.2), -3.0), 10))

```

but I wonder if there is anything simpler — the above just introduces a type for delegating code.
