Module Try

Try<e, a> carries either .ok a or .err e.

  • .ok a means a successful result.
  • .err e means an error has occurred.
type Try<e, a> = either {
  .err e,
  .ok a,
}

A value that is either .ok a or .err e.

dec Try.Filter : <e, a: box>[Try<e, a>] [box e, box [a] Bool] Try<e, a>

Keeps a successful value only if it satisfies the predicate, otherwise replaces it with the provided error.

dec Try.FlatMap : <e, a>[Try<e, a>] <b>[box [a] Try<e, b>] Try<e, b>

Transforms the .ok value with a computation that may itself fail.

dec Try.Map : <e, a>[Try<e, a>] <b>[box [a] b] Try<e, b>

Transforms the .ok value, if present.

dec Try.MapErr : <e1, a>[Try<e1, a>] <e2>[box [e1] e2] Try<e2, a>

Transforms the .err value, if present.

dec Try.Ok : <a>[Try<either {}, a>] a

Extracts the .ok branch from a Try<either {}, a>.

dec Try.ToList : <e: box, a>[Try<e, a>] List<a>

Converts .ok value to a singleton list and .err _ to an empty list.

dec Try.ToOption : <e: box, a>[Try<e, a>] Option<a>

Converts .ok value to .some value and .err _ to .none!.