# Split iteration to head + tail

**URL:** <https://discourse.julialang.org/t/split-iteration-to-head-tail/16406>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 16, 2018, 1:49pm UTC](https://discourse.julialang.org/t/split-iteration-to-head-tail/16406 "2018-10-16T13:49:21Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)\
**Post date:** [October 16, 2018, 2:23pm UTC](https://discourse.julialang.org/t/split-iteration-to-head-tail/16406/2 "2018-10-16T14:23:10Z")

</div>

Happy to have a run here 😃

That’s pretty much why I asked for [Skipping parts of a for loop in the first iteration](https://discourse.julialang.org/t/skipping-parts-of-a-for-loop-in-the-first-iteration/16252)

With that

```julia
       """
       Return the run lengths of `==` elements in the iterable itr.
       """
       function runlengths(itr)
           lengths = Int[]
           local runcount = 1
           @unroll1 for elt in itr
               if $first
                   lastelt = elt
               else 
                   if elt == lastelt
                       runcount += 1
                   else
                       push!(lengths, runcount)
                       lastelt = elt
                       runcount = 1
                   end
               end
           end
           push!(lengths, runcount)
           lengths
       end

```

The macro there is not well tested yet though, just had to `esc` some more expression to make the example run, see  
[https://gist.github.com/mschauer/9265bd5b70c9abf1391d4ef541d53eca](https://gist.github.com/mschauer/9265bd5b70c9abf1391d4ef541d53eca)

---

_[View the full topic](https://discourse.julialang.org/t/split-iteration-to-head-tail/16406)._
