# Iterate sequentially over several collections

**URL:** <https://discourse.julialang.org/t/iterate-sequentially-over-several-collections/13687>\
**Category:** General Usage\
**Created:** [August 18, 2018, 9:46pm UTC](https://discourse.julialang.org/t/iterate-sequentially-over-several-collections/13687 "2018-08-18T21:46:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![pablosanjose](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablosanjose/32/7006_2.png) [@pablosanjose](https://discourse.julialang.org/u/pablosanjose)\
**Post date:** [August 18, 2018, 9:46pm UTC](https://discourse.julialang.org/t/iterate-sequentially-over-several-collections/13687/1 "2018-08-18T21:46:33Z")

</div>

I have two collections with the same `eltype`, say `c1 = 1:3` and `c2 = 5:7`. I would like to be able to write a generator over the joined collections, something like

```julia
(i for i in joincol(c1, c2))

```

so that this becomes a generator of `[1,2,3,5,6,7]` without allocating `[c1..., c2...]` itself

Is there some equivalent of the `joincol` function in Base, or some standard way to do it without writing a custom `iterate` method?

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [August 18, 2018, 9:56pm UTC](https://discourse.julialang.org/t/iterate-sequentially-over-several-collections/13687/2 "2018-08-18T21:56:13Z")

</div>

`Iterators.flatten((1:3, 5:7))`

---

<div class="post-metadata">

**Author:** ![pablosanjose](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablosanjose/32/7006_2.png) [@pablosanjose](https://discourse.julialang.org/u/pablosanjose)\
**Post date:** [August 18, 2018, 9:56pm UTC](https://discourse.julialang.org/t/iterate-sequentially-over-several-collections/13687/3 "2018-08-18T21:56:49Z")

</div>

Fantastic, thanks!

---

<div class="post-metadata">

**Author:** ![quinnj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quinnj/32/11_2.png) [@quinnj](https://discourse.julialang.org/u/quinnj)\
**Post date:** [August 18, 2018, 10:09pm UTC](https://discourse.julialang.org/t/iterate-sequentially-over-several-collections/13687/4 "2018-08-18T22:09:38Z")

</div>

Obviously I need to study up on the already implemented iterators in the `Iterators` module, but since I already took the last 5 minutes doing this, I’ll post it anyway to feel like it was worth it 😛

```julia
struct Chain{T}
    t::T # Tuple of iterators
end
Chain(x...) = Chain(x)
Base.IteratorSize(::Type{Chain{T}}) where {T} = all(x->Base.IteratorSize(x)==Base.HasLength() || typeof(Base.IteratorSize(x)) <: Base.HasShape, T.parameters) ? Base.HasLength() : Base.SizeUnknown()
Base.length(c::C) where {C <: Chain} = Base.IteratorSize(C) == Base.HasLength() ? sum(x->length(x), c.t) : missing

function Base.iterate(c::Chain)
    st = nothing
    itr = c.t[1]
    i = 1
    while st === nothing && i <= length(c.t)
        itr = c.t[i]
        st = iterate(itr)
        i += 1
    end
    st === nothing && return nothing
    val, s = st
    return val, (itr, s, i)
end

function Base.iterate(c::Chain, state)
    itr, s, i = state
    st = iterate(itr, s)
    while st === nothing && i <= length(c.t)
        itr = c.t[i]
        st = iterate(itr)
        i += 1
    end
    st === nothing && return nothing
    val, s = st
    return val, (itr, s, i)
end

```

It’s probably not quite perfect, but whatever, that’s what I came up w/ in 5 minutes.

---

<div class="post-metadata">

**Author:** ![pablosanjose](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablosanjose/32/7006_2.png) [@pablosanjose](https://discourse.julialang.org/u/pablosanjose)\
**Post date:** [August 18, 2018, 10:19pm UTC](https://discourse.julialang.org/t/iterate-sequentially-over-several-collections/13687/5 "2018-08-18T22:19:33Z")

</div>

Haha! This was the kind of thing I was going to attempt before I posted. Sorry for stealing 5 mins of your time for this. Github needs you. Go! 😜

---

<div class="post-metadata">

**Author:** ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)\
**Post date:** [August 19, 2018, 2:28am UTC](https://discourse.julialang.org/t/iterate-sequentially-over-several-collections/13687/6 "2018-08-19T02:28:40Z")

</div>

I needed and found flatten a week or so ago. It took a lot of digging to find, more than usual.

---

<div class="post-metadata">

**Author:** ![LeoK987](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leok987/32/4880_2.png) [@LeoK987](https://discourse.julialang.org/u/LeoK987)\
**Post date:** [August 19, 2018, 4:59am UTC](https://discourse.julialang.org/t/iterate-sequentially-over-several-collections/13687/7 "2018-08-19T04:59:42Z")

</div>

Can I ask why you wouldn’t want to do the following?

(i for i in [c1;c2])

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [August 19, 2018, 6:11am UTC](https://discourse.julialang.org/t/iterate-sequentially-over-several-collections/13687/8 "2018-08-19T06:11:45Z")

</div>

> [@LeoK987](#):
>
> [c1;c2]

This allocates an array. `flatten` is lazy so it doesn’t allocate, just generates elements from the first generator then the second.
