# Anonymous iterators?

**URL:** <https://discourse.julialang.org/t/anonymous-iterators/29426>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 2, 2019, 11:31pm UTC](https://discourse.julialang.org/t/anonymous-iterators/29426 "2019-10-02T23:31:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [October 2, 2019, 11:31pm UTC](https://discourse.julialang.org/t/anonymous-iterators/29426/1 "2019-10-02T23:31:45Z")

</div>

i was reading in the Discourse Slack when someone asked about some syntax equivalent to the `yield` keyword in python. in Julia, the syntax ´for i in iter´ is lowered to `iterate(iter::IterType)` and `iterate(iter::IterType,state)`, from the [docs](https://docs.julialang.org/en/v1/manual/interfaces/#man-interface-iteration-1):

> ```julia
> for i in iter # or "for i = iter"
> # body
> end
> 
> ```
> 
> is translated into:
> 
> ```julia
> next = iterate(iter)
> while next !== nothing
> (i, state) = next
> # body
> next = iterate(iter, state)
> end
> 
> ```

so the interface requires the existence of a type `IterType` and the definition of `iterate` over that type.  
The `yield` keyword does something weird that probably involves internal state, but is very convenient to do, and only requires the definition of a function. compare this (from [Stack Overflow](https://stackoverflow.com/questions/231767/what-does-the-yield-keyword-do)):

```julia
def squares(n):
    mylist = range(n)
   for i in mylist:
        yield i*i

```

to this (from Julia Docs):

```julia
struct Squares
       count::Int
end
Base.iterate(S::Squares, state=1) = state > S.count ? nothing : (state*state, state+1)

```

so, in the context of Julia, how can an anonymous iterator can be done?

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [October 2, 2019, 11:36pm UTC](https://discourse.julialang.org/t/anonymous-iterators/29426/2 "2019-10-02T23:36:13Z")

</div>

The stop condition in the python code is hitting other code that isn’t `yield`, whereas the stop condition of julia code is returning `nothing`. so an anonymous function has to have this in mind.

One idea, as all functions are of an specific type, is create a macro that registers the function as an iterator, but i lack the skills to do that right now 😋

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [October 2, 2019, 11:38pm UTC](https://discourse.julialang.org/t/anonymous-iterators/29426/3 "2019-10-02T23:38:37Z")

</div>

How about this:

```julia
squares(n) = Channel{Int}() do c
    for i in 1:n
        push!(c, i^2)
    end
end

```

and then

```julia
julia> gen = squares(5)
Channel{Int64}(sz_max:0,sz_curr:1)

julia> collect(gen)
5-element Array{Int64,1}:
  1
  4
  9
 16
 25

```

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [October 3, 2019, 12:13am UTC](https://discourse.julialang.org/t/anonymous-iterators/29426/7 "2019-10-03T00:13:09Z")

</div>

Julia has the same Iterator constructor that python, so this is possible too:

```julia
squares = n-> (i^2 for i in 1:n)

```

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [October 3, 2019, 12:30am UTC](https://discourse.julialang.org/t/anonymous-iterators/29426/8 "2019-10-03T00:30:06Z")

</div>

See also [https://github.com/BenLauwens/ResumableFunctions.jl](https://github.com/BenLauwens/ResumableFunctions.jl)  
for a way to define iterators with a `@yield` style
