# Remove iterator in loop

**URL:** <https://discourse.julialang.org/t/remove-iterator-in-loop/2926>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 28, 2017, 1:41pm UTC](https://discourse.julialang.org/t/remove-iterator-in-loop/2926 "2017-03-28T13:41:35Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [March 28, 2017, 1:41pm UTC](https://discourse.julialang.org/t/remove-iterator-in-loop/2926/1 "2017-03-28T13:41:35Z")

</div>

I was wondering if there is a way to remove an iterator in a loop. For example,

> ```julia
> for obj in Objects
> if someCondition
> removeIterator(obj)
> end
> do other stuff...
> end
> 
> ```

There is a way to do this in [Java](http://stackoverflow.com/questions/223918/iterating-through-a-collection-avoiding-concurrentmodificationexception-when-re).

---

<div class="post-metadata">

**Author:** ![miguelraz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miguelraz/32/631_2.png) [@miguelraz](https://discourse.julialang.org/u/miguelraz)\
**Post date:** [March 28, 2017, 1:50pm UTC](https://discourse.julialang.org/t/remove-iterator-in-loop/2926/2 "2017-03-28T13:50:04Z")

</div>

Does using `deleteat!(A, index)` help?  
Gotta watch out for shifting lengths and index acces though.

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [March 28, 2017, 1:50pm UTC](https://discourse.julialang.org/t/remove-iterator-in-loop/2926/3 "2017-03-28T13:50:39Z")

</div>

or do you mean `continue`?

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [March 28, 2017, 1:53pm UTC](https://discourse.julialang.org/t/remove-iterator-in-loop/2926/4 "2017-03-28T13:53:21Z")

</div>

Not sure exactly what you mean to do - you could use `filter!` and then apply the other stuff?. If you don’t mind making a copy `[x^2 for x in randn(10) if x > 0]`. EDIT: I guess these responses aren’t actually helpful, sorry.

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [March 28, 2017, 3:25pm UTC](https://discourse.julialang.org/t/remove-iterator-in-loop/2926/5 "2017-03-28T15:25:50Z")

</div>

Hi all. Thanks for the responses. As @miguelraz noted, deleting the object will cause a shift in length and throw off alignment because it doesn’t actually update the underlying indexing.

My simulation involves interacting agents in a game theoretic context. Some agents are removed from the game and therefore should not interact with other agents, and should not contribute to the decisions of the remaining agents. So if I delete the 5th agent when agent 3 is making a decision, an error happens on the next iteration because the agent iterator is not updated. One way to hand this is to use checks throughout (if agent.interacting == true), but thats a little cumbersome. So I would be interested in something similar to the solution in the Java link involving a safe/adaptive remove function.

---

<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:** [March 28, 2017, 3:36pm UTC](https://discourse.julialang.org/t/remove-iterator-in-loop/2926/6 "2017-03-28T15:36:25Z")

</div>

Just write your own iterator.

```julia
for obj in ActiveAgentIterator(Objects)

```

and then you defined your `ActiveAgentIterator` with suitable iterator interface functions (most special here will be the `next` function that will look for the next active agent).

See [http://docs.julialang.org/en/stable/manual/interfaces/](http://docs.julialang.org/en/stable/manual/interfaces/)

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [March 28, 2017, 4:13pm UTC](https://discourse.julialang.org/t/remove-iterator-in-loop/2926/7 "2017-03-28T16:13:00Z")

</div>

Thanks @kristoffer.carlsson. This looks like a very elegant solution. If it only iterates agents that are interacting, it greatly simplifies my code and book keeping.

I’m having trouble figuring out how to specify a condition in the `next()` function. As you can see, what I tried does not skip over agent 5 even though it is set to false in the loop. Do you know how to do this? Thanks.

```julia

type Agent
  id::Int
  interacting::Bool
end

Base.start(::Agent) = 1
Base.next(S::Agent, state) = (Agent.interacting==true, state+1)
Base.done(S::Agent, state) = state > Agent.count
Base.eltype(::Type{Agent}) = Int 
Base.length(S::Agent) = S.count

Agents = [Agent(i,true) for i = 1:10]
for agent in Agents
  println([agent])
  Agents[5].interacting = false
end

```

---

<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:** [March 28, 2017, 5:08pm UTC](https://discourse.julialang.org/t/remove-iterator-in-loop/2926/8 "2017-03-28T17:08:23Z")

</div>

You are still just looping over a list of agents. You need a new type which wraps the array of agents and it is on this type you define the iterator interface. Please look at the link I posted earlier for some examples.

---

<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:** [March 28, 2017, 7:00pm UTC](https://discourse.julialang.org/t/remove-iterator-in-loop/2926/9 "2017-03-28T19:00:29Z")

</div>

You have a bit of an awkward case here that is covered by this issue: [https://github.com/JuliaLang/julia/issues/8149](https://github.com/JuliaLang/julia/issues/8149)

Here is something that seems to work:

```julia
type Agent
  id::Int
  interacting::Bool
end

type InteractingAgentIterator
    v::Vector{Agent}
    index::Int
end

interacting(agents::AbstractArray{Agent}) = InteractingAgentIterator(agents, 0)
Base.eltype(::Type{InteractingAgentIterator}) = Agent
Base.start(::InteractingAgentIterator) = Void # state not used

function Base.next(A::InteractingAgentIterator, i)
    return A.v[A.index], Void
end

function Base.done(A::InteractingAgentIterator, state)
    A.index += 1
    found_agent = false
    while A.index <= length(A.v)
        if A.v[A.index].interacting
            found_agent = true
            break
        end
        A.index += 1
    end
    return !found_agent
end

```

Testing it:

```julia
Agents = [Agent(i,true) for i = 1:10]
Agents[1].interacting = false
Agents[5].interacting = false
Agents[10].interacting = false
for agent in interacting(Agents)
    println("I am agent ", agent.id, " and I am ", !agent.interacting ? "not " : "", "interacting")
end

```

```julia
Prints:
I am agent 2 and I am interacting                                                                                                                                                  
I am agent 3 and I am interacting                                                                                                                                                  
I am agent 4 and I am interacting                                                                                                                                                  
I am agent 6 and I am interacting                                                                                                                                                  
I am agent 7 and I am interacting                                                                                                                                                  
I am agent 8 and I am interacting                                                                                                                                                  
I am agent 9 and I am interacting 

```

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [March 28, 2017, 7:13pm UTC](https://discourse.julialang.org/t/remove-iterator-in-loop/2926/10 "2017-03-28T19:13:27Z")

</div>

@kristoffer.carlsson Much appreciated. I did go through the link you sent to me, but couldn’t quite figure it out. I had no idea that the heavy lifting was in `Base.done`. This should really simplify my code. Thanks again!

---

<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:** [March 28, 2017, 7:15pm UTC](https://discourse.julialang.org/t/remove-iterator-in-loop/2926/11 "2017-03-28T19:15:41Z")

</div>

That’s the problem with the current iterator interface. If you have an iterator that you don’t know when it is done until you try, then you basically have to use the awkward way I showed.

---

<div class="post-metadata">

**Author:** ![ggggggggg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ggggggggg/32/265_2.png) [@ggggggggg](https://discourse.julialang.org/u/ggggggggg)\
**Post date:** [March 28, 2017, 7:46pm UTC](https://discourse.julialang.org/t/remove-iterator-in-loop/2926/12 "2017-03-28T19:46:40Z")

</div>

Here is a solution using `Filter` (which doesn’t appear to be documented)

```julia
type Agent
   id::Int
   interacting::Bool
 end

agents = [Agent(i,true) for i = 1:10];

agents[1].interacting = false
agents[5].interacting = false
agents[10].interacting = false

for agent in Filter(x->x.interacting,agents)
   println("I am agent ", agent.id, " and I am ", !agent.interacting ? "not " : "", "interacting")
end

```

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [March 28, 2017, 7:51pm UTC](https://discourse.julialang.org/t/remove-iterator-in-loop/2926/13 "2017-03-28T19:51:06Z")

</div>

Excellent. Thanks. Even mere mortals such as myself can make sense out of that.

> [@ggggggggg](#):
>
> type Agent  
> id::Int  
> interacting::Bool  
> end
> 
> agents = [Agent(i,true) for i = 1:10];
> 
> agents[1].interacting = false  
> agents[5].interacting = false  
> agents[10].interacting = false
> 
> for agent in Filter(x-\>x.interacting,agents)  
> println("I am agent ", agent.id, " and I am ", !agent.interacting ? "not " : “”, “interacting”)  
> end

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [March 28, 2017, 8:02pm UTC](https://discourse.julialang.org/t/remove-iterator-in-loop/2926/14 "2017-03-28T20:02:56Z")

</div>

Neat. There are surprisingly many undocumented gems in Julia still. The two solutions benchmark identically on my computer (I replaced the println with an array comprehension).

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [March 28, 2017, 8:19pm UTC](https://discourse.julialang.org/t/remove-iterator-in-loop/2926/15 "2017-03-28T20:19:04Z")

</div>

I have came across a few myself. Evidently, this particular issue is documented on [github](https://github.com/JuliaLang/julia/issues/16742).

---

<div class="post-metadata">

**Author:** ![miguelraz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miguelraz/32/631_2.png) [@miguelraz](https://discourse.julialang.org/u/miguelraz)\
**Post date:** [March 28, 2017, 9:19pm UTC](https://discourse.julialang.org/t/remove-iterator-in-loop/2926/16 "2017-03-28T21:19:00Z")

</div>

If you are interested, Jeff Bezanson’s PhD thesis (on his Github) has a section or two on an incrementally abstract agent iterator with the example of predatory-prey models.  
@Christopher_Fisher: [Found it. Section 3.3.1 - A Hierarchy of Dispatch](https://github.com/JeffBezanson/phdthesis/blob/master/main.pdf)

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [March 28, 2017, 9:50pm UTC](https://discourse.julialang.org/t/remove-iterator-in-loop/2926/17 "2017-03-28T21:50:23Z")

</div>

Thanks for the lead. That sounds potentially relevant to my project.

---

<div class="post-metadata">

**Author:** ![fengyang.wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fengyang.wang/32/104_2.png) [@fengyang.wang](https://discourse.julialang.org/u/fengyang.wang)\
**Post date:** [March 29, 2017, 5:10pm UTC](https://discourse.julialang.org/t/remove-iterator-in-loop/2926/18 "2017-03-29T17:10:45Z")

</div>

This is documented in 0.6 as `Base.Iterators.filter` (Note lowercase f).

On 0.5 and 0.6 one should use `Compat.Iterators.filter` instead of the undocumented implementation detail `Filter`.

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [March 29, 2017, 5:19pm UTC](https://discourse.julialang.org/t/remove-iterator-in-loop/2926/19 "2017-03-29T17:19:35Z")

</div>

Sorry, I can’t find it - I would have thougt it could be found on this page: [http://docs.julialang.org/en/latest/stdlib/iterators.html](http://docs.julialang.org/en/latest/stdlib/iterators.html) ?

---

<div class="post-metadata">

**Author:** ![fengyang.wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fengyang.wang/32/104_2.png) [@fengyang.wang](https://discourse.julialang.org/u/fengyang.wang)\
**Post date:** [March 29, 2017, 5:34pm UTC](https://discourse.julialang.org/t/remove-iterator-in-loop/2926/20 "2017-03-29T17:34:25Z")

</div>

You’re right; for some reason this function is not documented. My mistake.

[Next page](https://discourse.julialang.org/t/remove-iterator-in-loop/2926.md?page=2)
