cycle
Module to conveniently run function in loop
Use-case for this are:
- Maintain a state while sharing resources easily
- Run operation forever
- Limit cycles a loop can go through
cycle.start(with: #([], 0), run: fn(state) {
let #(is, i) = state
case i < 5 {
True -> {
let i = i + 1
cycle.continue(#(list.prepend(is, i), i))
}
_ -> cycle.stop(is)
}
})
Types
Values
pub fn safely_start(
with state: state,
max_cycle max: Int,
run function: fn(state) -> Next(state, result),
) -> Result(result, Nil)
Start a cycle with guard on max cycle, returns like normal start, otherwise Error(Nil) if n-th cycle has reached the max.
pub fn start(
with state: state,
run function: fn(state) -> Next(state, result),
) -> result
Start a cycle, returns a result if function tell the cycler to stop.