moresult
A collection of ergonomic utility functions for working with Result types.
This package provides helpful combinators and transformations to make error handling in Gleam more expressive and concise.
Values
pub fn and(
first: Result(a, e),
second: Result(b, e),
) -> Result(b, e)
Returns second if the first is Ok, otherwise returns the Error value of first.
pub fn either(
result: Result(a, e),
on_ok: fn(a) -> b,
on_error: fn(e) -> b,
) -> b
Case analysis for the Result type. If the value is Ok, apply the first function; if it is Error, apply the second function.
pub fn error(result: Result(a, e)) -> option.Option(e)
Converts Result(a, e) into an Option(e) and discarding the success value, if any.
pub fn errors(results: List(Result(a, e))) -> List(e)
Extracts from a list of Result all Error elements. All the Error elements extracts in order.
pub fn from_either(result: Result(a, a)) -> a
Pull the value out of an Result where both alternatives have the same type.
pub fn inspect(
result: Result(a, e),
func: fn(a) -> Nil,
) -> Result(a, e)
Calls a function with contained value if Ok. Returns the original result.
pub fn inspect_err(
result: Result(a, e),
func: fn(e) -> Nil,
) -> Result(a, e)
pub fn is_error_and(
result: Result(a, e),
predicate: fn(e) -> Bool,
) -> Bool
Returns true if the result is Error and the value inside of it matches a predicate.
pub fn is_ok_and(
result: Result(a, e),
predicate: fn(a) -> Bool,
) -> Bool
Returns true if the result is Ok and the value inside of it matches a predicate.
pub fn lazy_unwrap_error(
result: Result(a, e),
default: fn() -> e,
) -> e
Extracts the Error value from a result, evaluating the default function if the result is an Error.
pub fn map_or(
result: Result(a, e),
default: b,
fun: fn(a) -> b,
) -> b
Returns the provided default (if Error), or applies a function to the contained value (if Ok). Arguments passed to map_or are eagerly evaluated; if you are passing the result of a function call, it is recommended to use map_or_else, which is lazily evaluated.
pub fn map_or_else(
result: Result(a, e),
default: fn() -> b,
fun: fn(a) -> b,
) -> b
Maps a Result(a, e) to a by applying fallback function default to a contained Error value, or function fun to a contained Ok value.
pub fn ok(result: Result(a, e)) -> option.Option(a)
Converts Result(a, e) into an Option(a) and converting the error to None, if any.