internal/loop

Module to conveniently run function in loop

Use-case for this are:

loop.with(with: #([], 0), run: fn(state) {
  let #(is, i) = state
  case i < 5 {
    True -> {
      let i = i + 1
      loop.Continue(#(list.prepend(is, i), i))
    }
    _ -> loop.Break(is)
  }
})

This is an altered version of https://hex.pm/packages/cycle

Types

Message to the executer if the loop should continue or break with a result.

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

Constructors

  • Continue(state)

    Continue the loop with new state.

  • Break(result)

    Break the loop with a result.

Values

pub fn inf(init state: state, run f: fn(state) -> state) -> Nil
pub fn try_inf(
  init state: state,
  run f: fn(state) -> Result(state, err),
) -> Result(Nil, err)
pub fn try_with(
  init state: state,
  run function: fn(state) -> Result(Next(state, result), err),
) -> Result(result, err)

Start a fallible loop, returns a result when the function successfully returns Break or an Error if it fails.

pub fn with(
  init state: state,
  run function: fn(state) -> Next(state, result),
) -> result

Start a loop, returns a result when the function returns Break.

✨ Search Document