cycle

Module to conveniently run function in loop

Use-case for this are:

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

Message to the cycler if the cycle should continue or stop with a result.

pub type Next(state, result) {
  Continue(state)
  Stop(result)
}

Constructors

  • Continue(state)

    Continue the cycle with new state.

  • Stop(result)

    Stop the cycle with a result.

Values

pub fn continue(state: state) -> Next(state, result)

Convenient shortcut to Continue.

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.

pub fn stop(result: result) -> Next(state, result)

Convenient shortcut to Stop.

Search Document