# Spiraling outwards iterator

**URL:** <https://discourse.julialang.org/t/spiraling-outwards-iterator/27938>\
**Category:** General Usage\
**Created:** [August 24, 2019, 3:39pm UTC](https://discourse.julialang.org/t/spiraling-outwards-iterator/27938 "2019-08-24T15:39:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rapus95](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rapus95/32/3773_2.png) [@rapus95](https://discourse.julialang.org/u/rapus95)\
**Post date:** [August 24, 2019, 3:39pm UTC](https://discourse.julialang.org/t/spiraling-outwards-iterator/27938/1 "2019-08-24T15:39:53Z")

</div>

Hey, I was looking for an iterator that spirals outward a given point. By that I stumbled over a [Rosetta Code Challenge](https://rosettacode.org/wiki/Spiral_matrix#Julia) seeing that it is outdated (old iterator protocol) and that it goes inward clockwise. Thus it didn’t help. I’m telling here nevertheless because someone might want to challenge himself.

Now, using the algorithm from a [StackOverflow question](https://stackoverflow.com/a/33639875/2510426) I came up with my own spiraling counter-clockward outwards iterator:

```julia
struct SpiralIterator
    center::CartesianIndex{2} #EDIT: {2} added according to suggestion of @jlperla 
end

function Base.iterate(iter::SpiralIterator, state=(0,0,-1,1))#x,y,d,m
    x, y, d, m = state
    2 * x * d < m && return (iter.center+CartesianIndex(x,y), (x+d, y, d, m))
    2 * y * d < m && return (iter.center+CartesianIndex(x,y), (x, y+d, d, m))
    return Base.iterate(iter, (x, y, -d, m+1))
end

Base.IteratorSize(::SpiralIterator) = Base.IsInfinite()

```

I wanted to ask if that’s idiomatic Julia and if you see anything to tweak (especially because the first condition is evaluated twice as often as the second one).

---

<div class="post-metadata">

**Author:** ![jlperla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlperla/32/34332_2.png) [@jlperla](https://discourse.julialang.org/u/jlperla)\
**Post date:** [August 24, 2019, 5:37pm UTC](https://discourse.julialang.org/t/spiraling-outwards-iterator/27938/2 "2019-08-24T17:37:45Z")

</div>

I don’t think that `center::CartesianIndex` would lead to a concrete type in your structure. If not, you will need to make it parametric with `center::T` and `T <: CartesianIndex` or something along those lines

---

<div class="post-metadata">

**Author:** ![rapus95](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rapus95/32/3773_2.png) [@rapus95](https://discourse.julialang.org/u/rapus95)\
**Post date:** [August 27, 2019, 9:06pm UTC](https://discourse.julialang.org/t/spiraling-outwards-iterator/27938/3 "2019-08-27T21:06:19Z")

</div>

What about CartesianIndex{2} as the algorithm only works for 2d anyway?

---

<div class="post-metadata">

**Author:** ![jlperla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlperla/32/34332_2.png) [@jlperla](https://discourse.julialang.org/u/jlperla)\
**Post date:** [August 28, 2019, 2:50am UTC](https://discourse.julialang.org/t/spiraling-outwards-iterator/27938/4 "2019-08-28T02:50:08Z")

</div>

I think that is right. If it only works with `{2}` then you shouldn’t make it generic.

Note that a good way to check if things are concrete is:

```julia
julia> typeof(CartesianIndex)
UnionAll

julia> typeof(CartesianIndex{2})
DataType

```

The `UnionAll` means there are some parametric parameter, and hence will not be a concrete type if used in a `struct`. `DataType` means A-OK, and other things usually mean it is an abstract type.
