gloop

This module called Gloop introduces stack-safe, tall-call recursive while loops in Gleam. The intent is to remove the additional mental burden recursion causes in Gleam.

For an example of how to use this library see gloop_test.gleam in the test folder.

Functions

pub fn do_while(
  state state: a,
  code_to_run code_to_run: fn(a) -> a,
  post_run_condition post_run_condition: fn(a) -> Bool,
) -> a

This do while function checks the condition using the communicated state at the end of an iteration. It therefore runs the code given to the do while function AT LEAST once. Just as the other while function, it is stack-safe.

pub fn while(
  state state: a,
  pre_run_condition pre_run_condition: fn(a) -> Bool,
  code_to_run code_to_run: fn(a) -> a,
) -> a

This while function is stack-safe and checks the condition based on the communicated state at the beginning of an iteration. If the condition is true then the code handed to the while function is run and generates a new state in preparation for the next iteration. If the condition is false then the code returns the final state and stops. As long as the condition is true, the while loop will continue without blowing up the stack.

Search Document