# Iterators on Julia 0.7 (and announcing the Invenia blog)

**URL:** <https://discourse.julialang.org/t/iterators-on-julia-0-7-and-announcing-the-invenia-blog/12217>\
**Category:** Community\
**Tags:** blog\
**Created:** [July 6, 2018, 5:09pm UTC](https://discourse.julialang.org/t/iterators-on-julia-0-7-and-announcing-the-invenia-blog/12217 "2018-07-06T17:09:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![iamed2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iamed2/32/215082_2.png) [@iamed2](https://discourse.julialang.org/u/iamed2)\
**Post date:** [July 6, 2018, 5:09pm UTC](https://discourse.julialang.org/t/iterators-on-julia-0-7-and-announcing-the-invenia-blog/12217/1 "2018-07-06T17:09:28Z")

</div>

In this post I talk about the new interface and share some tips and useful patterns for dealing with iteration in Julia 0.7:

> **[Writing Iterators in Julia 0.7](https://invenia.github.io/blog/2018/07/06/iteratorsinjulia07/)**
>
> With the alpha release of version 0.7, Julia has simplified its iteration interface. This was a huge undertaking which mostly fell to the prolific Keno Fischer, who wrote an entirely new optimizer for the language to accomplish it! As the most active...

It’s been shared on Hacker News, if you feel like upvoting.

This also serves as the announcement of the Invenia technical blog, which will see more posts about Julia, machine learning, and electrical grids. Check out Eric Perim’s two-part post on our SyntheticGrids.jl package:

> **[SyntheticGrids.jl: Part 1](https://invenia.github.io/blog/2018/06/27/syntheticgrids-part-1/)**
>
> Background

> **[SyntheticGrids.jl: Part 2](https://invenia.github.io/blog/2018/06/27/syntheticgrids-part-2/)**
>
> Usage

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [July 10, 2018, 11:07am UTC](https://discourse.julialang.org/t/iterators-on-julia-0-7-and-announcing-the-invenia-blog/12217/2 "2018-07-10T11:07:32Z")

</div>

Nice post! I learned a couple of good tips from it.

In your “Slurping and Splatting” section, given that the docstring for `iterate` does not discuss multiple state arguments, I do wonder a bit about the wisdom of creating a varargs `iterate`—in my initial glance at the code, I wondered if it could be like `getindex` and it might be of variable lengths maxing out at `iter.interval`.

Here’s an alternative, where I pass an extra `count` element indicating how many items are left:

```julia
function iterate(it::TakeNth)
    xs_iter = @ifsomething iterate(it.xs)
    return iterate(it, (it.interval-1, xs_iter)) # we already consumed 1 item, so count=it.interval-1
end

function iterate(it::TakeNth, (count, xs_iter))
    for i = 1:count
        xs_iter = @ifsomething iterate(it.xs, xs_iter[2])
    end
    return (xs_iter[1], (it.interval, xs_iter)) # next time, start over at the full count = it.interval
end

```

Admittedly it’s slightly more complicated, but aside from not making a questionable method this does have benefits:

master:

```julia
julia> using IterTools

julia> tn2 = takenth(10:20, 3)
IterTools.TakeNth{UnitRange{Int64}}(10:20, 0x0000000000000003)

julia> returnstrue(x) = true
returnstrue (generic function with 1 method)

julia> count(returnstrue, tn2)
3

julia> using BenchmarkTools

julia> @btime count($returnstrue, $tn2)
  273.126 ns (12 allocations: 352 bytes)
3

```

This implementation:

```julia

julia> @btime count($returnstrue, $tn2)
  37.008 ns (1 allocation: 32 bytes)
3

```

Also, one small correction regarding `Base.tail`:

> No matter the size of the input tuple, it will always return at least an empty tuple.

That’s true except for an empty tuple, which triggers an error. (which is a good thing)
