# Approach to iterative functions in julia?

**URL:** <https://discourse.julialang.org/t/approach-to-iterative-functions-in-julia/112369>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [April 1, 2024, 6:43am UTC](https://discourse.julialang.org/t/approach-to-iterative-functions-in-julia/112369 "2024-04-01T06:43:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![corpserot](https://avatars.discourse-cdn.com/v4/letter/c/6de8d8/32.png) [@corpserot](https://discourse.julialang.org/u/corpserot)\
**Post date:** [April 1, 2024, 6:43am UTC](https://discourse.julialang.org/t/approach-to-iterative-functions-in-julia/112369/1 "2024-04-01T06:43:10Z")

</div>

Hello, I’ve just spent yesterday learning Julia and it’s been a blast! Previously, I used Python and Lua for quick projects. In those languages, I used iterative functions (or generators) but I’m having a hard time figuring out how to translate that idea to Julia.

Here’s an exercise I’m doing:

```julia
function find_all_contiguous(contrafinder::Function, a::Union{AbstractArray,AbstractString})
    firsti, lasti = firstindex(a), lastindex(a)
    i = firsti
    while i <= lasti
        cstop = contrafinder(a, i, lasti)
        if isa(cstop, Integer)
            # i want to yield here instead of returning on first match
            return i, clamp(cstop, firsti, lasti)
            i = cstop
        end
        i += 1
    end
end

function as_contrafinder(pred::Function)
    # a way to more succintly express this would also be nice to have
    return function (a::AbstractVector{<:Any}, start::Integer, stop::Integer)
        for i in start:stop
            if !pred(a[i])
                if i == start
                    return
                else
                    return i-1
                end
            end
        end
    end
end

# example use
# intent: from a randomly populated array, print the sequences of
# odds found and the sum of that sequence
data = rand(0:9, 1000)
for (start, stop) in find_all_contiguous(isodd |> as_contrafinder, data)
    seq = data[start:stop]
    println("sum of odd sequence $(seq) = $(sum(seq))")
end

```

Thank you in advance! ❤

---

<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:** [April 1, 2024, 7:01am UTC](https://discourse.julialang.org/t/approach-to-iterative-functions-in-julia/112369/2 "2024-04-01T07:01:38Z")

</div>

There are a number of packages that aim to provide generator functionality,

> **[GitHub - JuliaDynamics/ResumableFunctions.jl: C# style generators a.k.a....](https://github.com/JuliaDynamics/ResumableFunctions.jl)**
>
> C# style generators a.k.a. semi-coroutines for Julia. - JuliaDynamics/ResumableFunctions.jl

is one such package. See also this thread for additional modeling patterns, including making use of `Channel` and the julia native generator

> [@Yield in iterator](https://discourse.julialang.org/t/yield-in-iterator/106833):
>
> How can I use a “yield” inside a function like a generator/iterator? function results() yield 1 yield 2 yield 3 }

---

<div class="post-metadata">

**Author:** ![corpserot](https://avatars.discourse-cdn.com/v4/letter/c/6de8d8/32.png) [@corpserot](https://discourse.julialang.org/u/corpserot)\
**Post date:** [April 1, 2024, 7:33am UTC](https://discourse.julialang.org/t/approach-to-iterative-functions-in-julia/112369/3 "2024-04-01T07:33:21Z")

</div>

Thanks for the response! Implementing iterators natively in Julia seems very messy, so the package recommendation is much appreciated

---

<div class="post-metadata">

**Author:** ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)\
**Post date:** [April 1, 2024, 8:30am UTC](https://discourse.julialang.org/t/approach-to-iterative-functions-in-julia/112369/4 "2024-04-01T08:30:36Z")

</div>

> [@corpserot](#):
>
> Implementing iterators natively in Julia seems very messy, so the package recommendation is much appreciated

It’s really just to replace `yield` with `put!`:

```julia
squares = Channel{Int}() do ch
    for i in 1:10
        put!(ch, i^2)
    end
end

println(sum(squares))

```

And the standard iterators in julia are similar to python’s, except you define `Base.iterate` instead of ` __iter__ ` and ` __next__ `.

See [Interfaces · The Julia Language](https://docs.julialang.org/en/v1/manual/interfaces/#man-interface-iteration) and [Asynchronous Programming · The Julia Language](https://docs.julialang.org/en/v1/manual/asynchronous-programming/#Communicating-with-Channels)
