resulto (resulto v0.2.4)

View Source

Result represents the result of something that may succeed or not:

  • {ok, any()} means it was successful,
  • {error, any()} means it was not.

Borrowed from the elegant Gleam's [result] (https://hexdocs.pm/gleam_stdlib/gleam/result.html) module.

Summary

Types

A failure result with its associated reason

A successful result with its associated value

t()

Represents a value that is either a success (ok, {ok, Value}) or a failure ({error, Reason}).

Functions

Combines a list of results into a single result.

Creates a failed result.

Flattens nested results (e.g., {ok, {ok, Value}}) into a single-layer result.

Returns true if the result is an error.

Returns true if the result is an successful.

Returns the original result if it's ok, otherwise calls the provided function to return an alternative result.

Returns the value of the result if it's ok, otherwise calls the fallback function.

Applies a function to the value inside ok. Errors are returned unchanged.

Applies a function to the value inside error. Successful results are returned unchanged.

Creates a successful result.

Returns the first result if successful, otherwise the second.

Separates a list of results into a tuple with all successes and all errors. The lists are in reverse order of their appearance.

Returns the result if successful, otherwise raises the error.

Extracts the ok value or raises the error if not present.

Replaces the value inside a successful result.

Replaces the value inside a failed result.

Wraps a term in a result.

An alias for try/2.

An alias for try_both/2.

Returns the result if successful, otherwise throws the error.

Extracts the ok value or raises the error if not present.

Applies a function to the value inside a successful result, returning its result. Errors are returned unchanged.

Applies a function to the value inside a successful result. If it's an error, applis a recovery function instead.

Applies a function to the value inside an error, returning its result. Success values are returned unchanged.

Converts any error to {error, undefined}.

Returns the ok value or undefined if the result is an error.

Returns the ok value or the given default value if it's an error.

Returns either the ok or error value, whichever is present.

Returns the error value or the default if it's an ok.

Returns all the values inside ok results from a list of results.

Types

error()

-type error() :: error(any()).

A failure result with its associated reason

error(Error)

-type error(T) :: {error, T}.

ok()

-type ok() :: ok(any()).

A successful result with its associated value

ok(Value)

-type ok(T) :: {ok, T}.

t()

-type t() :: ok | ok() | error().

Represents a value that is either a success (ok, {ok, Value}) or a failure ({error, Reason}).

Functions

all(Results)

-spec all([t()]) -> t().

Combines a list of results into a single result.

If all elements in the list are ok then returns an ok holding the list of values. If any element is an error then returns the first error.

error(Error)

-spec error(Error :: any()) -> error().

Creates a failed result.

flatten/1

-spec flatten(t()) -> t().

Flattens nested results (e.g., {ok, {ok, Value}}) into a single-layer result.

is_error/1

-spec is_error(t()) -> boolean().

Returns true if the result is an error.

is_ok/1

-spec is_ok(t()) -> boolean().

Returns true if the result is an successful.

lazy_or(Result, Fun)

-spec lazy_or(Result :: t(), Fun :: fun(() -> t())) -> t().

Returns the original result if it's ok, otherwise calls the provided function to return an alternative result.

lazy_unwrap(Result, Fun)

-spec lazy_unwrap(Result :: t(), Fun :: fun(() -> t())) -> any().

Returns the value of the result if it's ok, otherwise calls the fallback function.

map/2

-spec map(Result :: t(), fun((any()) -> any())) -> error() | any().

Applies a function to the value inside ok. Errors are returned unchanged.

map_error/2

-spec map_error(Result :: t(), fun((any()) -> any())) -> error() | any().

Applies a function to the value inside error. Successful results are returned unchanged.

ok(Value)

-spec ok(Value :: any()) -> ok().

Creates a successful result.

or_else(First, Second)

-spec or_else(First :: t(), Second :: t()) -> t().

Returns the first result if successful, otherwise the second.

partition(Results)

-spec partition(Results :: [t()]) -> {[any()], [any()]} | no_return().

Separates a list of results into a tuple with all successes and all errors. The lists are in reverse order of their appearance.

raise_or/1

-spec raise_or(t()) -> ok() | no_return().

Returns the result if successful, otherwise raises the error.

raise_or_unwrap/1

-spec raise_or_unwrap(t()) -> any() | no_return().

Extracts the ok value or raises the error if not present.

replace(Result, Value)

-spec replace(Result :: t(), Value :: any()) -> t().

Replaces the value inside a successful result.

replace_error(Result, Error)

-spec replace_error(Result :: t(), Error :: any()) -> t().

Replaces the value inside a failed result.

result(Term)

-spec result(Term :: any()) -> t().

Wraps a term in a result.

If the term is already a result, it is returned as-is. If the term is ok, it returns ok; otherwise it wraps the term in {ok, Term}.

then(Result, Fun)

-spec then(Result :: t(), Fun :: fun((any()) -> t())) -> t() | no_return().

An alias for try/2.

then_both(Result, Fun, Fun)

-spec then_both(Result :: t(), Fun :: fun((any()) -> t()), Fun :: fun((any()) -> t())) ->
                   t() | no_return().

An alias for try_both/2.

then_recover(Result, Fun)

-spec then_recover(Result :: t(), Fun :: fun((any()) -> t())) -> t() | no_return().

An alias for try_recover/2.

throw_or/1

-spec throw_or(t()) -> ok() | no_return().

Returns the result if successful, otherwise throws the error.

throw_or_unwrap/1

-spec throw_or_unwrap(t()) -> any() | no_return().

Extracts the ok value or raises the error if not present.

'try'(Result, Fun)

-spec 'try'(Result :: t(), Fun :: fun((any()) -> t())) -> t() | no_return().

Applies a function to the value inside a successful result, returning its result. Errors are returned unchanged.

try_both(Result, Fun, Fun)

-spec try_both(Result :: t(), Fun :: fun((any()) -> t()), Fun :: fun((any()) -> t())) ->
                  t() | no_return().

Applies a function to the value inside a successful result. If it's an error, applis a recovery function instead.

try_recover(Result, Fun)

-spec try_recover(Result :: t(), Fun :: fun((any()) -> t())) -> t() | no_return().

Applies a function to the value inside an error, returning its result. Success values are returned unchanged.

undefined_error(Result)

-spec undefined_error(Result :: t()) -> ok() | error(undefined).

Converts any error to {error, undefined}.

unwrap(Result)

-spec unwrap(Result :: t()) -> any().

Returns the ok value or undefined if the result is an error.

unwrap(Result, Default)

-spec unwrap(Result :: t(), Default :: any()) -> any().

Returns the ok value or the given default value if it's an error.

unwrap_both(Result)

-spec unwrap_both(Result :: t()) -> any().

Returns either the ok or error value, whichever is present.

unwrap_error(Result, Default)

-spec unwrap_error(Result :: t(), Default :: any()) -> any().

Returns the error value or the default if it's an ok.

values(Results)

-spec values(Results :: [t()]) -> [any()].

Returns all the values inside ok results from a list of results.