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 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 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.
Ok(value) becomes Some(value), Error(error) becomes None.
Examples
of_result(Ok(42))
// -> Some(42)
of_result(Error("oops"))
// -> None
pub fn sequence(
options: List(option.Option(a)),
) -> option.Option(List(a))
Convert a list of Options into an Option of a list. If all options are Some, returns Some with the list of values. If any option is None, returns None.
This is the special case of traverse where the function is identity.
Examples
sequence([Some(1), Some(2), Some(3)])
// -> Some([1, 2, 3])
sequence([Some(1), None, Some(3)])
// -> None
sequence([])
// -> Some([])
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")
pub fn traverse(
items: List(a),
f: fn(a) -> option.Option(b),
) -> option.Option(List(b))
Apply a function that returns an Option to each element of a list, collecting the results. If all results are Some, returns Some with the list of values. If any result is None, returns None.
This is useful for operations like “parse all these strings, fail if any parse fails” or “look up all these keys, fail if any key is missing”.
Examples
import gleam/int
traverse(["1", "2", "3"], int.parse)
// -> Some([1, 2, 3])
import gleam/int
traverse(["1", "bad", "3"], int.parse)
// -> None
import gleam/dict
let users = dict.from_list([#(1, "Alice"), #(2, "Bob")])
traverse([1, 2], fn(id) { dict.get(users, id) })
// -> Some(["Alice", "Bob"])
pub fn unwrap_lazy(opt: option.Option(a), f: fn() -> a) -> a
Get the value from an option or compute a default lazily. Great when generating defaults lazily or when the value is expensive to compute.
Examples
unwrap_lazy(Some(42), fn() { 0 })
// -> 42
unwrap_lazy(None, fn() { expensive_computation() })
// -> result of expensive_computation()
pub fn zip(
opt1: option.Option(a),
opt2: option.Option(b),
) -> option.Option(#(a, b))
Combine two options into a tuple.
This is syntactic sugar for map2(opt1, opt2, fn(a, b) { #(a, b) }).
Examples
zip(Some(1), Some(2))
// -> Some(#(1, 2))
zip(Some(1), None)
// -> None
pub fn zip3(
opt1: option.Option(a),
opt2: option.Option(b),
opt3: option.Option(c),
) -> option.Option(#(a, b, c))
Combine three options into a tuple.
This is syntactic sugar for map3(opt1, opt2, opt3, fn(a, b, c) { #(a, b, c) }).
Examples
zip3(Some(1), Some(2), Some(3))
// -> Some(#(1, 2, 3))
zip3(Some(1), None, Some(3))
// -> None