gextra/result
Functions
pub fn lazy_map_both(
over r: Result(a, b),
err err: fn(b) -> c,
val val: fn(a) -> d,
) -> Result(d, c)
Applies one of two functions depending on whether the result is Ok or Error.
If the result is Ok, the val
function is applied to the value.
If the result is Error, the err
function is applied to the error.
Example
let assert Ok(0) = lazy_map_both(
Ok(1),
fn(x) { x + 1 },
fn(x) { x - 1 },
)
let assert Error(2) = lazy_map_both(
Error(1),
fn(x) { x + 1 },
fn(x) { x - 1 },
)
pub fn lazy_try_both(
over r: Result(a, b),
err err: fn(b) -> c,
val val: fn(a) -> Result(d, c),
) -> Result(d, c)
Applies one of two functions depending on whether the result is Ok or Error.
If the result is Ok, the val
function is applied to the value.
If the result is Error, the err
function is applied to the error.
This function differs from lazy_map_both
in that the val
function returns a Result
.
Example
let assert Ok(0) = lazy_try_both(
Ok(1),
fn(a) { a - 1 },
fn(a) { Ok(a + 1) }
)
let assert Error(2) = lazy_try_both(
Error(1),
fn(a) { a - 1 },
fn(a) { Ok(a + 1)},
)
pub fn map_both(
over r: Result(a, b),
err err: c,
val val: fn(a) -> d,
) -> Result(d, c)
Maps a Result to another Result. If Ok, applies a function to the contained value. If Error, uses provided value.
Example
use x <- map_both(Ok(1), 2)
let assert Ok(1) = x
use y <- map_both(Error(1), 2)
// never reached
pub fn try_both(
over r: Result(a, b),
err err: c,
val val: fn(a) -> Result(d, c),
) -> Result(d, c)
If Ok, applies a function to the contained value which returns a result. If Error, uses provided value.
Example
use x <- try_both(Ok(1), 2)
let assert Ok(1) = x
use y <- try_both(Error(1), 2)
// never reached
pub fn try_lazy_map_both(
over r: Result(a, b),
err err: fn(b) -> c,
) -> fn(fn(a) -> Result(d, c)) -> Result(d, c)
pub fn try_map_both(
over r: Result(a, b),
err err: c,
) -> fn(fn(a) -> Result(d, c)) -> Result(d, c)
pub fn use_lazy_map_both(
over r: Result(a, b),
err err: fn(b) -> c,
) -> fn(fn(a) -> d) -> Result(d, c)
pub fn use_map_both(
over r: Result(a, b),
err err: c,
) -> fn(fn(a) -> d) -> Result(d, c)