Result

GitHub   Edit on GitHub

Utilities for working with the Result data type.

The Result type is a special type of enum that exists to represent the possibility of a success case (with the Ok variant), or an error case (with the Err variant). Use a Result as the return type of a function that may return an error.

1
import Result from "result"

Create new Result values using Ok() and Err():

1
2
let success = Ok("Yay!")
let failure = Err("Something bad happened")

Values

Result.isOk

1
isOk : Result<t, e> -> Bool

Checks if the Result is the Ok variant.

Result.isErr

1
isErr : Result<t, e> -> Bool

Checks if the Result is the Err variant.

Result.toOption

1
toOption : Result<t, e> -> Option<t>

Converts the Result to an Option. The error is discarded and replaced with None.

Result.flatMap

1
flatMap : (t -> Result<t, e>, Result<t, e>) -> Result<t, e>

If the Result is Ok, applies the given function to the Ok value and returns the result. Returns the unmodified Err otherwise.

Result.flatMapErr

1
flatMapErr : (e -> Result<t, e>, Result<t, e>) -> Result<t, e>

If the Result is an Err, applies the given function to the Err value and returns the result. Returns the unmodified Ok value otherwise.

Result.map

1
map : (t -> u, Result<t, e>) -> Result<u, e>

If the Result is Ok(x), returns Ok(fn(x)). Returns the unmodified Err otherwise.

Result.mapErr

1
mapErr : (e -> Result<t, e>, Result<t, e>) -> Result<t, e>

If the Result is Err(x), returns Err(fn(x)). Returns the unmodified Ok value otherwise.

Result.mapWithDefault

1
mapWithDefault : (t -> u, u, Result<t, e>) -> u

If the Result is Ok(x), returns fn(x). Returns the provided default otherwise.

Result.mapWithDefaultFn

1
mapWithDefaultFn : (t -> u, e -> u, Result<t, e>) -> u

If the Result is Ok(x), returns fnOk(x). If the Result is Err(y), returns fnErr(y).

Result.or

1
or : (Result<t, e>, Result<t, e>) -> Result<t, e>

If the first Result is Ok, returns the first Result. Returns the second Result otherwise.

Result.and

1
and : (Result<t, e>, Result<t, e>) -> Result<t, e>

If the first Result is Err, returns the first Result. Returns the second Result otherwise.

Result.peek

1
peek : (t -> a, e -> b, Result<t, e>) -> Void

If the Result is Ok(x), applies the first function to x. If the Result is Err(y), applies the second function to y.

Result.peekOk

1
peekOk : (t -> a, Result<t, e>) -> Void

If the Result is Ok(x), applies the function to x.

Result.peekErr

1
peekErr : (e -> b, Result<t, e>) -> Void

If the Result is Err(y), applies the function to y.

This is a notification!