glimp

The intent of this module called Glimp, short for Gleam Imperative is to provide close alternatives to the traditional C features while, do while, for and switch but in a functional language. This will allow you to quickly port complex algorithms implemented in C or other imperative languages to Gleam. Of course, it would be best to reimplement these as functional algorithms. However, this is typically hard, error-prone and very time consuming. With this module, an initial make-it-work port becomes much easier. Once you get this initial port working, you can focus your energy on implementing this in a more functional way.

For examples of how to use this library see glimp_test.gleam in the test folder of the project.

Types

pub type CaseBlock(a, b) {
  CaseBlock(match: a, code: fn(a) -> #(b, Bool))
}

Constructors

  • CaseBlock(match: a, code: fn(a) -> #(b, Bool))
pub type LoopState(a) {
  LoopState(state: a, break: Bool)
}

Constructors

  • LoopState(state: a, break: Bool)

Functions

pub fn do_while(
  state state: a,
  code_to_run code_to_run: fn(a) -> LoopState(a),
  post_run_condition post_run_condition: fn(a) -> Bool,
) -> a
  1. IMPLEMENTING DO WHILE WITH BREAK In C, the syntax for do while is:

do { // code block to be executed } while (condition);

This corresponding do while function in Gleam is stack-safe and checks the condition based on the communicated state at the end of an iteration of the while loop. It therefore runs the code in the do while function AT LEAST ONCE. If the condition is true then the new state generated by the code is handed to the next iteration of the do while loop and used with the next run of the code. The code in the code block can throw a Break just like in C. If the condition is false or break is true then the new state is returned and the loop stops. Note that ending the loop returns the new state. This may or may not be desired. The alternative is returning the old state. Let me know if there is a desire for that feature.

pub fn for(
  state state: a,
  pre_run_condition pre_run_condition: fn(a) -> Bool,
  code_to_run code_to_run: fn(a) -> LoopState(a),
  increment_code post_run_code: fn(a) -> LoopState(a),
) -> a
  1. IMPLEMENTING FOR WITH BREAK In C, a forloop is defined as:

for (initialization; condition; increment) { // code block to be executed } The initialization is executed (one time) before the execution of the code block. The condition defines the condition for executing the code block. And the increment code is executed (every time) after the code block has run.

In Gleam, the for function checks the condition using the communicated state at the beginning of an iteration. If the condition is true then the code block handed to the for function is run. At the end of a run, a new state is generated that is used for the next iteration. If the condition is false or break is true then the code returns the final state and stops. Just as the while functions, this for function is stack-safe.

pub fn switch(
  expression expression: a,
  cases cases: List(CaseBlock(a, b)),
  default default: fn(a) -> b,
) -> Result(b, Nil)
pub fn while(
  state state: a,
  pre_run_condition pre_run_condition: fn(a) -> Bool,
  code_to_run code_to_run: fn(a) -> LoopState(a),
) -> a
  1. IMPLEMENTING WHILE WITH BREAK In C syntax, while is defined as:

while (condition) { // code block to be executed }

The while function in Gleam is stack-safe and checks the pre_run_condition based on the communicated state at the beginning of an iteration of the while loop. If the condition is true then the code in the code block handed to the while function is run and generates a new LoopState in preparation for the next iteration. The code in the code block can throw a Break just like in C.

Search Document