# How to get the first state of a iterator?

**URL:** <https://discourse.julialang.org/t/how-to-get-the-first-state-of-a-iterator/34846>\
**Category:** General Usage\
**Created:** [February 19, 2020, 12:48pm UTC](https://discourse.julialang.org/t/how-to-get-the-first-state-of-a-iterator/34846 "2020-02-19T12:48:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![matthieu](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@matthieu](https://discourse.julialang.org/u/matthieu)\
**Post date:** [February 19, 2020, 12:48pm UTC](https://discourse.julialang.org/t/how-to-get-the-first-state-of-a-iterator/34846/1 "2020-02-19T12:48:59Z")

</div>

I need to create a vector with the states of an iterator for which the element satisfies a certain condition.  
However, to do that, I would need to know the first state of an iterator (i.e. the default argument of `iterator`), in case the first element satisfies the condition. Is there a way to get it?

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [February 19, 2020, 12:53pm UTC](https://discourse.julialang.org/t/how-to-get-the-first-state-of-a-iterator/34846/2 "2020-02-19T12:53:13Z")

</div>

Perhaps I misunderstand the question, but it sounds like you are looking for

| Required methods | | Brief description |
| --- | --- | --- |
| iterate(iter) | | Returns either a tuple of the first item and initial state or nothing if empty |

from [the docs](https://docs.julialang.org/en/v1/manual/interfaces/#man-interface-iteration-1).

Perhaps you could provide more detail on what you are trying to achieve?

---

<div class="post-metadata">

**Author:** ![matthieu](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@matthieu](https://discourse.julialang.org/u/matthieu)\
**Post date:** [February 19, 2020, 12:58pm UTC](https://discourse.julialang.org/t/how-to-get-the-first-state-of-a-iterator/34846/3 "2020-02-19T12:58:16Z")

</div>

Thanks but that’s not what I am looking for. I would like to create a function `g(f::Function, iter)` that returns for an arbitrary iterator `iter` the vector of states for which the corresponding element satisfies `f(x) = 0`

The returned vector by `g` must satisfy following:

```julia
for state in g(f, iter)
   f(iterate(iter, state)) == 0
end

```

For instance,

```julia
g(x -> x == 0, [0, 1, 0]) = [1, 3]

```

Note that, because the first element satisfies the condition, the function should return the first state of the iterator.

---

<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:** [February 19, 2020, 1:38pm UTC](https://discourse.julialang.org/t/how-to-get-the-first-state-of-a-iterator/34846/4 "2020-02-19T13:38:45Z")

</div>

> [@matthieu](#):
>
> The issue is that, in case the first element satisfies the condition, the function requires to know what the first state of the iterator is.

There is (semantically) no state for the first `iterate` call though? Some iterators might implement their `iterate` with a default value as the state but that is not required and not every iterator does this.

---

<div class="post-metadata">

**Author:** ![tkluck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkluck/32/15769_2.png) [@tkluck](https://discourse.julialang.org/u/tkluck)\
**Post date:** [February 19, 2020, 2:23pm UTC](https://discourse.julialang.org/t/how-to-get-the-first-state-of-a-iterator/34846/6 "2020-02-19T14:23:25Z")

</div>

> [@matthieu](#):
>
> I would like to create a function `g(f::Function, iter)` that returns for an arbitrary iterator `iter` the vector of states for which the corresponding element satisfies `f(x) = 0`

As @kristoffer.carlsson says, unfortunately your question has no answer. It’s true that many iterators are implemented through

```julia
iterate(::Iterator, state=....)

```

but many others are not.

What you could do is represent `state` by `(state,)` and the empty state by `()`. Example:

```julia
function g(f, iter)
    zerostates = []
    curstate = ()
    i = iterate(iter, curstate...)
    while !isnothing(i)
        val, state = i
        curstate = (state,)
        iszero(f(val)) && push!(zerostates, curstate)
        i = iterate(iter, curstate...)
    end
    return zerostates
end

```

But it has the downside that you now have a `Vector{Any}`. It’s probably as close as you’ll get, though.

---

<div class="post-metadata">

**Author:** ![matthieu](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@matthieu](https://discourse.julialang.org/u/matthieu)\
**Post date:** [February 19, 2020, 8:39pm UTC](https://discourse.julialang.org/t/how-to-get-the-first-state-of-a-iterator/34846/8 "2020-02-19T20:39:02Z")

</div>

Thanks everyone!
