Skip to Content
i documentation
Standard library referenceModules

std/result

Functions for the built-in type Result(A, E) { Ok(A) Error(E) }.

A Result is an error kept as a value. i also has errors as effects (^ E in a function type, i/fail): to_error and catch go from one form to the other.

is_ok

True for Ok.

is_ok = <A, E>(result : Result(A, E)) -> Bool {

is_error

True for Error.

is_error = <A, E>(result : Result(A, E)) -> Bool {

map

Changes the value of an Ok with a function. An Error stays.

result.map(Ok(1), (x) { x + 1 })  // Ok(2)
map = <A, E, B, N>(over : Result(A, E), with : (A) <- N -> B) -> Result(B, E) {

map_error

Changes the error of an Error with a function. An Ok stays.

map_error = <A, E, F, N>(over : Result(A, E), with : (E) <- N -> F) -> Result(

then

Calls a function that gives a result on the value of an Ok. An Error stays. This puts together steps that may each fail.

result.then(Ok(1), (x) { Ok(x + 1) })  // Ok(2)
then = <A, E, B, N>(

flatten

Takes away one level of Result.

flatten = <A, E>(result : Result(Result(A, E), E)) -> Result(A, E) {

unwrap

The value of an Ok, or or for an Error.

unwrap = <A, E>(result : Result(A, E), or : A) -> A {

lazy_unwrap

The value of an Ok, or the value of or() for an Error.

lazy_unwrap = <A, E, N>(result : Result(A, E), or : () <- N -> A) -> A {

unwrap_error

The error of an Error, or or for an Ok.

unwrap_error = <A, E>(result : Result(A, E), or : E) -> E {

unwrap_both

The value of an Ok or the error of an Error, when they have the same type.

unwrap_both = <A>(result : Result(A, A)) -> A {

or

The first result when it is Ok, and the second when not.

or = <A, E>(first : Result(A, E), second : Result(A, E)) -> Result(A, E) {

lazy_or

The first result when it is Ok, and the value of second() when not.

lazy_or = <A, E, N>(

all

Ok with all the values when every result is Ok, or the first Error.

result.all([Ok(1), Ok(2)])              // Ok([1, 2])
result.all([Ok(1), Error("a"), Error("b")])  // Error("a")
all = <A, E>(results : List(Result(A, E))) -> Result(List(A), E) {

partition

The values of the Ok results and the errors of the Error results, each in order.

result.partition([Ok(1), Error("a"), Ok(2)])  // ([1, 2], ["a"])
partition = <A, E>(results : List(Result(A, E))) -> (List(A), List(E)) {

values

The values of the Ok results, in order.

values = <A, E>(results : List(Result(A, E))) -> List(A) {

replace

Puts value in the place of the value of an Ok.

replace = <A, E, B>(result : Result(A, E), value : B) -> Result(B, E) {

replace_error

Puts error in the place of the error of an Error.

replace_error = <A, E, F>(result : Result(A, E), error : F) -> Result(A, F) {

recover

Calls a function on the error of an Error, which may give a new result. An Ok stays.

recover = <A, E, F, N>(

from_option

Ok with the value of a Some, or Error(error) for None.

from_option = <A, E>(option : Option(A), error : E) -> Result(A, E) {

to_option

Some with the value of an Ok, or None for an Error.

to_option = <A, E>(result : Result(A, E)) -> Option(A) {

to_error

The value of an Ok. For Error(x), the computation ends with the error x (§9.3), which catch or i/fail can take.

to_error = <A, E>(result : Result(A, E)) -> A { fail.from_result(result) }

catch

Does body, and gives Ok with its value, or Error with the error it ends with.

result.catch(() { fail.raise("no") })  // Error("no")
catch = <A, E, N>(body : () <- (Fail(E), ..N) -> A) -> Result(A, E) {