midas/continuation

Work with effects as values using continuations.

Types

t is the final return type of the whole computation. a is the type of the value currently inside the monad.

pub type Continuation(t, a) =
  fn(fn(a) -> t) -> t

A task is a continuation of a result.

pub type Task(t, a, b) =
  fn(fn(Result(a, b)) -> t) -> t

Values

pub fn done(value: a) -> fn(fn(Result(a, b)) -> t) -> t

A task has completed successfully

pub fn each(
  over list: List(a),
  with f: fn(a) -> fn(fn(b) -> t) -> t,
) -> fn(fn(List(b)) -> t) -> t

Apply a function to each value in a list in order

pub fn fail(reason: b) -> fn(fn(Result(a, b)) -> t) -> t

A task is done with an error

pub fn fold(
  over list: List(a),
  from initial: acc,
  with f: fn(acc, a) -> fn(fn(acc) -> t) -> t,
) -> fn(fn(acc) -> t) -> t

Iterate over a list of items, and apply each to the given function f.

pub fn return(value: a) -> fn(fn(a) -> t) -> t

Create a continuation that returns the given value when called.

Often called pure.

pub fn then(
  cont: fn(fn(a) -> t) -> t,
  next: fn(a) -> fn(fn(b) -> t) -> t,
) -> fn(fn(b) -> t) -> t

compose two a continuation with a new function that returns another continuation

This is monad bind.

pub fn try(
  result: Result(a, b),
  then: fn(a) -> fn(fn(Result(c, b)) -> t) -> t,
) -> fn(fn(Result(c, b)) -> t) -> t

Work with results that are not continuations.

pub fn try_each(
  over list: List(a),
  with f: fn(a) -> fn(fn(Result(b, c)) -> t) -> t,
) -> fn(fn(Result(List(b), c)) -> t) -> t

Apply the given function to each item in a list, returning at first error.

pub fn try_or(
  result: Result(a, b),
  handle: fn(b) -> c,
  then: fn(a) -> fn(fn(c) -> t) -> t,
) -> fn(fn(c) -> t) -> t

Work with results that are not continuations.

pub fn try_then(
  cont: fn(fn(Result(a, e)) -> t) -> t,
  next: fn(a) -> fn(fn(Result(b, e)) -> t) -> t,
) -> fn(fn(Result(b, e)) -> t) -> t
Search Document