rectify/option
Option utilities for Rectify
Additional helpers for Gleam’s Option type that complement
the standard library.
Values
pub fn choose_somes(opts: List(option.Option(a))) -> List(a)
Extract all Some values from a list of options.
Examples
choose_somes([Some(1), None, Some(2), None, Some(3)])
// -> [1, 2, 3]
pub fn default_to(opt: option.Option(a), default: a) -> a
Get the value from an option or return a default.
Examples
default_to(Some(42), 0)
// -> 42
default_to(None, 0)
// -> 0
pub fn default_with(opt: option.Option(a), f: fn() -> a) -> a
Get the value from an option or compute a default.
Examples
default_with(Some(42), fn() { 0 })
// -> 42
default_with(None, fn() { expensive_computation() })
// -> result of expensive_computation()
pub fn first_some(
opts: List(option.Option(a)),
) -> option.Option(a)
Returns the first Some value, or None if all are None.
Examples
first_some([None, Some(2), Some(3)])
// -> Some(2)
first_some([None, None])
// -> None
pub fn is_none(opt: option.Option(a)) -> Bool
Check if an option is None.
Examples
is_none(None)
// -> True
is_none(Some(42))
// -> False
pub fn is_some(opt: option.Option(a)) -> Bool
Check if an option is Some.
Examples
is_some(Some(42))
// -> True
is_some(None)
// -> False
pub fn map2(
opt1: option.Option(a),
opt2: option.Option(b),
f: fn(a, b) -> c,
) -> option.Option(c)
Map over two options, returning None if either is None.
Examples
map2(Some(2), Some(3), fn(a, b) { a + b })
// -> Some(5)
map2(Some(2), None, fn(a, b) { a + b })
// -> None
pub fn map3(
opt1: option.Option(a),
opt2: option.Option(b),
opt3: option.Option(c),
f: fn(a, b, c) -> d,
) -> option.Option(d)
Map over three options.
Examples
map3(Some(1), Some(2), Some(3), fn(a, b, c) { a + b + c })
// -> Some(6)
map3(Some(1), None, Some(3), fn(a, b, c) { a + b + c })
// -> None
pub fn map4(
opt1: option.Option(a),
opt2: option.Option(b),
opt3: option.Option(c),
opt4: option.Option(d),
f: fn(a, b, c, d) -> g,
) -> option.Option(g)
Map over four options.
Examples
map4(Some(1), Some(2), Some(3), Some(4), fn(a, b, c, d) { a + b + c + d })
// -> Some(10)
map4(Some(1), Some(2), None, Some(4), fn(a, b, c, d) { a + b + c + d })
// -> None
pub fn map5(
opt1: option.Option(a),
opt2: option.Option(b),
opt3: option.Option(c),
opt4: option.Option(d),
opt5: option.Option(e),
f: fn(a, b, c, d, e) -> h,
) -> option.Option(h)
Map over five options.
Examples
map5(Some(1), Some(2), Some(3), Some(4), Some(5), fn(a, b, c, d, e) { a + b + c + d + e })
// -> Some(15)
map5(Some(1), None, Some(3), Some(4), Some(5), fn(a, b, c, d, e) { a + b + c + d + e })
// -> None
pub fn of_result(result: Result(a, e)) -> option.Option(a)
Convert a Result to an Option, discarding the error.
Examples
of_result(Ok(42))
// -> Some(42)
of_result(Error("oops"))
// -> None
pub fn to_result(opt: option.Option(a), error: e) -> Result(a, e)
Convert an Option to a Result with a custom error.
Examples
to_result(Some(42), "not found")
// -> Ok(42)
to_result(None, "not found")
// -> Error("not found")