# Implementing non-stateful iteration

**URL:** <https://discourse.julialang.org/t/implementing-non-stateful-iteration/2525>\
**Category:** General Usage\
**Created:** [March 7, 2017, 5:24pm UTC](https://discourse.julialang.org/t/implementing-non-stateful-iteration/2525 "2017-03-07T17:24:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![malmaud](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/malmaud/32/29_2.png) [@malmaud](https://discourse.julialang.org/u/malmaud)\
**Post date:** [March 7, 2017, 5:24pm UTC](https://discourse.julialang.org/t/implementing-non-stateful-iteration/2525/1 "2017-03-07T17:24:49Z")

</div>

I’m trying to wrap a C library where iteration is intrinsically non-stateful, as shown below.

```nohighlight
void *y = ...;
void *x = start_iteration(y);
while (x!=NULL) {
  do_something(x);
  x = get_next(y);
}

```

I was wondering if anyone has any advice on how to (elegantly) wrap this iteration pattern with the Julia iteration protocol, where there doesn’t seem to be a natural definition of `state` as in [http://docs.julialang.org/en/release-0.5/manual/interfaces/](http://docs.julialang.org/en/release-0.5/manual/interfaces/)

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [March 7, 2017, 5:32pm UTC](https://discourse.julialang.org/t/implementing-non-stateful-iteration/2525/2 "2017-03-07T17:32:04Z")

</div>

This iteration looks super stateful… In fact, `x` itself is the state.

---

<div class="post-metadata">

**Author:** ![malmaud](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/malmaud/32/29_2.png) [@malmaud](https://discourse.julialang.org/u/malmaud)\
**Post date:** [March 7, 2017, 5:44pm UTC](https://discourse.julialang.org/t/implementing-non-stateful-iteration/2525/3 "2017-03-07T17:44:52Z")

</div>

Oh woops, typo. It should be `x = get_next(y)`; the C library has some internal opaque state attached `y`.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [March 7, 2017, 5:50pm UTC](https://discourse.julialang.org/t/implementing-non-stateful-iteration/2525/4 "2017-03-07T17:50:15Z")

</div>

Then just use `nothing` as state, that’s what we do with files.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [March 7, 2017, 5:56pm UTC](https://discourse.julialang.org/t/implementing-non-stateful-iteration/2525/5 "2017-03-07T17:56:10Z")

</div>

A common pattern I’ve used is to stash the subsequent object as your state. In pseudo-code:

```julia
start(y) = next(y, start_iteration(y))[2]
next(y, state) = (do_something(state); (state, get_next(y)))
done(y, state) = state == NULL

```

---

<div class="post-metadata">

**Author:** ![malmaud](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/malmaud/32/29_2.png) [@malmaud](https://discourse.julialang.org/u/malmaud)\
**Post date:** [March 7, 2017, 7:27pm UTC](https://discourse.julialang.org/t/implementing-non-stateful-iteration/2525/6 "2017-03-07T19:27:37Z")

</div>

Ah that’s good. Thanks!
