moresult
A collection of ergonomic utility functions for working with Result types.
Inspired by Rust’s Result module, 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 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 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.