rectify/result_option
ResultOption utilities for Rectify
Helpers for working with Result(Option(a), e) — a common pattern
for operations that can fail AND may not return a value.
We refer to this as “ResultOption” for brevity, but it’s not a
distinct type — just the composed Result(Option(a), e) pattern.
Instead of nested pattern matching, use these combinators.
Values
pub fn bind(
ro: Result(option.Option(a), e),
f: fn(a) -> Result(option.Option(b), e),
) -> Result(option.Option(b), e)
Bind over ResultOption.
Examples
bind(Ok(Some(5)), fn(n) { Ok(Some(n * 2)) })
// -> Ok(Some(10))
bind(Ok(None), fn(n) { Ok(Some(n * 2)) })
// -> Ok(None)
pub fn error(e: e) -> Result(option.Option(a), e)
Create an error Result.
Examples
error("not found")
// -> Error("not found")
pub fn is_none(ro: Result(option.Option(a), e)) -> Bool
Check if ResultOption contains None.
Examples
is_none(Ok(None))
// -> True
is_none(Ok(Some(42)))
// -> False
is_none(Error("e"))
// -> False
pub fn is_some(ro: Result(option.Option(a), e)) -> Bool
Check if ResultOption contains Some value.
Examples
is_some(Ok(Some(42)))
// -> True
is_some(Ok(None))
// -> False
is_some(Error("e"))
// -> False
pub fn map(
ro: Result(option.Option(a), e),
f: fn(a) -> b,
) -> Result(option.Option(b), e)
Map over the value inside ResultOption, if both succeed.
Examples
map(Ok(Some(5)), fn(n) { n * 2 })
// -> Ok(Some(10))
map(Ok(None), fn(n) { n * 2 })
// -> Ok(None)
map(Error("e"), fn(n) { n * 2 })
// -> Error("e")
pub fn none() -> Result(option.Option(a), e)
Create a Result containing None.
Examples
none()
// -> Ok(None)
pub fn of_option(
opt: option.Option(a),
) -> Result(option.Option(a), e)
Convert Option to ResultOption, wrapping in Ok.
Examples
of_option(Some(42))
// -> Ok(Some(42))
of_option(None)
// -> Ok(None)
pub fn of_result(
result: Result(a, e),
) -> Result(option.Option(a), e)
Convert Result to ResultOption, wrapping success in Some.
Examples
of_result(Ok(42))
// -> Ok(Some(42))
of_result(Error("e"))
// -> Error("e")
pub fn some(a: a) -> Result(option.Option(a), e)
Create a Result containing Some value.
Examples
some(42)
// -> Ok(Some(42))
pub fn to_option(
ro: Result(option.Option(a), e),
) -> option.Option(a)
Convert ResultOption to Option(a), losing error information.
Examples
to_option(Ok(Some(42)))
// -> Some(42)
to_option(Ok(None))
// -> None
to_option(Error("e"))
// -> None
pub fn to_result(
ro: Result(option.Option(a), e),
default: a,
) -> Result(a, e)
Convert ResultOption to a plain Result, with a default for None.
Examples
to_result(Ok(Some(42)), 0)
// -> Ok(42)
to_result(Ok(None), 0)
// -> Ok(0)
to_result(Error("e"), 0)
// -> Error("e")
pub fn unwrap_option(
ro: Result(option.Option(a), e),
default: a,
) -> Result(a, e)
Unwrap the Option inside Result, using a default for None.
Returns the value if Some, the default if None, or preserves Error.
Examples
unwrap_option(Ok(Some(42)), 0)
// -> Ok(42)
unwrap_option(Ok(None), 0)
// -> Ok(0)
unwrap_option(Error("e"), 0)
// -> Error("e")
pub fn unwrap_option_lazy(
ro: Result(option.Option(a), e),
f: fn() -> a,
) -> Result(a, e)
Unwrap the Option inside Result, computing a default lazily for None.
Returns the value if Some, computes default if None, or preserves Error.
Examples
unwrap_option_lazy(Ok(Some(42)), fn() { 0 })
// -> Ok(42)
unwrap_option_lazy(Ok(None), fn() { 100 })
// -> Ok(100)
unwrap_option_lazy(Error("e"), fn() { 0 })
// -> Error("e")