given
This library attempts to make guards:
- Applicable to
Bool
,Result
andOption
types. - Ergonomic to use by providing ways to handle both branches early.
- Expressive by making it easy to read through function names and labels.
- Comprehendable by not having to negate the conditions.
- Safe to use by not accidentally running discarded branches much like
bool.lazy_guard
.
Functions
pub fn error_in(
result result: Result(a, b),
else_return consequence: fn(a) -> c,
otherwise alternative: fn(b) -> c,
) -> c
Examples
use error_value <- given_error_in(result, else_return: fn(ok_value) { "Ok" })
// …handle Error value here…
"Error"
pub fn given(
requirement: Bool,
return consequence: fn() -> a,
otherwise alternative: fn() -> a,
) -> a
Examples
let user_understood = case int.random(1) {
1 -> True
_ -> False
}
use <- given(user_understood, return: fn() { "Great!" })
// …else handle case where user did not understand here…
"Woof!"
pub fn none_in(
option option: Option(a),
else_return consequence: fn(a) -> b,
otherwise alternative: fn() -> b,
) -> b
Examples
use none_value <- given_none_in(option, else_return: fn(some_value) { "Some value" })
// …handle None value here…
"None"
pub fn not_given(
requirement: Bool,
return consequence: fn() -> a,
otherwise alternative: fn() -> a,
) -> a
Examples
let user_understood = case int.random(1) {
1 -> True
_ -> False
}
use <- not_given(user_understood, return: fn() { "Woof!" })
// …else handle case where user understood here…
"Great!"
pub fn ok_in(
result result: Result(a, b),
else_return alternative: fn(a) -> c,
otherwise consequence: fn(b) -> c,
) -> c
Examples
use ok_value <- given_ok_in(result, else_return: fn(error_value) { "Error" })
// …handle Ok value here…
"Ok"