resulto (resulto v0.2.4)
View SourceResult 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
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
.
An alias for try_recover/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
A failure result with its associated reason
-type error(T) :: {error, T}.
A successful result with its associated value
-type ok(T) :: {ok, 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.
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
.
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.
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}
.
An alias for try/2
.
-spec then_both(Result :: t(), Fun :: fun((any()) -> t()), Fun :: fun((any()) -> t())) -> t() | no_return().
An alias for try_both/2
.
An alias for try_recover/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.
-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.
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.