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.

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 Result

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 default_to(
  ro: Result(option.Option(a), e),
  default: a,
) -> Result(a, e)

Get value or return default.

Examples

default_to(Ok(Some(42)), 0)
// -> Ok(42)
default_to(Ok(None), 0)
// -> Ok(0)
default_to(Error("e"), 0)
// -> Error("e")
pub fn default_with(
  ro: Result(option.Option(a), e),
  f: fn() -> a,
) -> Result(a, e)

Get value or compute default for None cases.

Examples

default_with(Ok(Some(42)), fn() { 0 })
// -> Ok(42)
default_with(Ok(None), fn() { 100 })
// -> Ok(100)
default_with(Error("e"), fn() { 0 })
// -> Error("e")
pub fn error(e: e) -> Result(option.Option(a), e)

Create an error Result.

Examples

error("not found")
// -> Error("not found")
pub fn is_error(ro: Result(option.Option(a), e)) -> Bool

Check if Result

Examples

is_error(Error("e"))
// -> True
is_error(Ok(Some(42)))
// -> False
pub fn is_none(ro: Result(option.Option(a), e)) -> Bool

Check if Result

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 Result

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 Result

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 Result

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 Result

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 Result

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 Result

Examples

to_result(Ok(Some(42)), 0)
// -> Ok(42)
to_result(Ok(None), 0)
// -> Ok(0)
to_result(Error("e"), 0)
// -> Error("e")
Search Document