### Gleam Given: Project Setup and Execution Source: https://hexdocs.pm/given/index Instructions for setting up and running the Gleam Given project examples. This includes cloning the repository, running the examples, and development commands. ```bash git clone https://github.com/inoas/gleam-given.git bin/run-examples ``` ```bash gleam run # Run the project gleam test # Run the tests ``` -------------------------------- ### Install Given Library Source: https://hexdocs.pm/given/index Instructions for adding the Given library to your Gleam project using the gleam add command. This is the primary method for integrating the library. ```gleam gleam add given@5 ``` -------------------------------- ### Gleam Given: Main Function Examples Source: https://hexdocs.pm/given/index The main function showcases various usage patterns of the Gleam Given library, including `given_that_example`, `given_any_example`, `given_all_example`, and many others, demonstrating conditional logic and early returns. ```gleam pub fn main() { given_that_example() |> echo // "💡 Bright!" given_any_example() |> echo // "At least admin or editor" given_all_example() |> echo // "Not both active and confirmed" given_not_example() |> echo // "👌 Access granted..." given_any_not_example() |> echo // "User has no special role!" given_all_not_example() |> echo // "Obsolete model detected." given_when_example() |> echo // "Indeed an Admin" given_when_not_example() |> echo // "Not an Admin" given_empty_example() |> echo // "Empty!" given_non_empty_example() |> echo // "Non-empty!" given_ok_example() |> echo // "Hello Joe, again!" given_any_ok_example() |> echo // "At least some OKs" given_all_ok_example() |> echo // "Some Errors" given_error_example() |> echo // "Memory exhausted, again!" given_any_error_example() |> echo // "Good" given_all_error_example() |> echo // "Meh" given_some_example() |> echo // "One More Penny" given_any_some_example() |> echo // "We found some Gold!" given_all_some_example() |> echo // "Nothing found..." given_none_example() |> echo // "None, e.g. Still nothing" given_any_none_example() |> echo // "The system detected Some-things." given_all_none_example() |> echo // "There is something out there..." } ``` -------------------------------- ### Given Library: Conditional Execution with 'when_not' Source: https://hexdocs.pm/given/given Illustrates the 'given.when_not' function, which executes code when a condition is false. It includes examples for providing an alternative return value when the condition is true. ```gleam import given let enabled = fn() { False } use <- given.when_not(enabled, else_return: fn() { "Indeed an Admin" }) // …handle case where user is not an Admin… "Not an Admin" ``` ```gleam import given let enabled = fn() { False } use <- given.when_not(enabled, return: fn() { "Not an Admin" }) // …handle case where user is an Admin… "Indeed an Admin" ``` -------------------------------- ### Handle None Option Source: https://hexdocs.pm/given/given Demonstrates how to handle a Gleam Option type when it is None, providing an alternative execution path. ```gleam import given import gleam/option.{None} let option = None use <- given.none(in: option, else_return: fn(some_value) { "Some value" }) // …handle None here… "None" ``` ```gleam import given.{none as given_none_in} import gleam/option.{None} let option = None use <- given_none_in(option, else_return: fn(some_value) { "Some value" }) // …handle None here… "None" ``` -------------------------------- ### Handle all `Ok` results with `given.all_ok` Source: https://hexdocs.pm/given/index Shows `given.all_ok` for verifying if all results in a list are `Ok`. It provides `_oks` and `_errors` for handling, with an `else_return` for cases with any errors. ```gleam import given pub fn given_all_ok_example() { let results = [Ok("Great"), Error("Bad")] use _oks <- given.all_ok(in: results, else_return: fn(_oks, _errors) { "Some Errors" }) // …handle all OKs here… "All OKs" } ``` -------------------------------- ### Handle `None` values with `given.none` Source: https://hexdocs.pm/given/index Demonstrates `given.none` for executing code when an option is `Option.None`. It takes an option and an `else_return` function for `Option.Some` cases. ```gleam import given import gleam/option.{None, Some} pub fn given_none_example() { let an_option = None use <- given.none(in: an_option, else_return: fn(_some_value) { "Some value" }) // …handle None here… "None, e.g. Still nothing!" } ``` -------------------------------- ### Handle all `None` values with `given.all_none` Source: https://hexdocs.pm/given/index Shows `given.all_none` for verifying if all options in a list are `Option.None`. It provides `_somes` and `_nones_count` for handling, with an `else_return` for cases with any `Some` values. ```gleam import given import gleam/option.{None, Some} pub fn given_all_none_example() { let options = [Some("One"), None] ``` -------------------------------- ### Handle all `Some` values with `given.all_some` Source: https://hexdocs.pm/given/index Shows `given.all_some` for verifying if all options in a list are `Option.Some`. It provides `_somes` and `_nones_count` for handling, with an `else_return` for cases with any `None` values. ```gleam import given import gleam/option.{None, Some} pub fn given_all_some_example() { let options = [Some("One"), None] use _somes <- given.all_some( in: options, else_return: fn(_somes, _nones_count) { "Nothing found..." }, ) // …handle all Some values here… "There is Gold everywhere!" } ``` -------------------------------- ### Given Library: Conditional Execution with 'when' Source: https://hexdocs.pm/given/given Demonstrates the 'given.when' function for executing code based on a condition. It shows how to provide an alternative return value when the condition is false. ```gleam import given let enabled = fn() { False } use <- given.when(enabled, else_return: fn() { "Not an Admin" }) // …handle case where user is an Admin… "Indeed an Admin" ``` ```gleam import given let enabled = fn() { False } use <- given.when(enabled, return: fn() { "Indeed an Admin" }) // …handle case where user is not an Admin… "Not an Admin" ``` -------------------------------- ### Handle `Ok` results with `given.ok` Source: https://hexdocs.pm/given/index Demonstrates `given.ok` for extracting the value from an `Ok` result. It takes a result and an `else_return` function for `Error` cases. ```gleam import given pub fn given_ok_example() { let a_result = Ok("Hello Joe, again!") use ok_value <- given.ok(in: a_result, else_return: fn(_error_value) { "Error value" }) // …handle Ok value here… ok_value } ``` -------------------------------- ### Handle `Some` values with `given.some` Source: https://hexdocs.pm/given/index Demonstrates `given.some` for extracting the value from an `Option.Some`. It takes an option and an `else_return` function for `Option.None` cases. ```gleam import given import gleam/option.{None, Some} pub fn given_some_example() { let an_option = Some("One More Penny") use some_value <- given.some(in: an_option, else_return: fn() { "Woof!" }) // …handle Some value here… some_value } ``` -------------------------------- ### Handle Some Option Source: https://hexdocs.pm/given/given Executes a consequence when a Gleam Option is Some, providing an alternative if it is None. ```gleam import given import gleam/option.{Some} let option = Some("One") use some_value <- given.some(in: option, else_return: fn() { "None" }) // …handle Some value here… "Some value" ``` ```gleam import given.{some as given_some_in} import gleam/option.{Some} let option = Some("One") use some_value <- given_some_in(option, else_return: fn() { "None" }) // …handle Some value here… "Some value" ``` -------------------------------- ### Handle at least one `None` value with `given.any_none` Source: https://hexdocs.pm/given/index Explains `given.any_none` for checking if at least one `Option.None` exists in a list of options. It provides `_somes` and `_none_count` for handling, with an `else_return` for the case where all are `Some`. ```gleam import given import gleam/option.{None, Some} pub fn given_any_none_example() { let options = [Some("One"), None] use _somes, _none_count <- given.any_none( in: options, else_return: fn(_somes) { "Only Nones Here!" }, ) // …handle at least some None values here… "The system detected Some-things." } ``` -------------------------------- ### Handle at least one `Some` value with `given.any_some` Source: https://hexdocs.pm/given/index Explains `given.any_some` for checking if at least one `Option.Some` exists in a list of options. It provides `_somes` and `_nones_count` for handling, with an `else_return` for the case where all are `None`. ```gleam import given import gleam/option.{None, Some} pub fn given_any_some_example() { let options = [Some("One"), None] use _somes, _nones_count <- given.any_some( in: options, else_return: fn(_nones_count) { "Just rocks here, move on..." }, ) // …handle at least some None values here… "We found some Gold!" } ``` -------------------------------- ### Handle at least one `Ok` result with `given.any_ok` Source: https://hexdocs.pm/given/index Explains `given.any_ok` for checking if at least one `Ok` result exists in a list of results. It provides `_oks` and `_errors` for handling, with an `else_return` for the case where all are errors. ```gleam import given pub fn given_any_ok_example() { let results = [Ok("Great"), Error("Bad")] use _oks, _errors <- given.any_ok(in: results, else_return: fn(_errors) { "All Errors" }) // …handle at least some OKs here… "At least some OKs" } ``` -------------------------------- ### Gleam Given: Handling All None Values Source: https://hexdocs.pm/given/index Demonstrates how to use `given.all_none` to handle cases where all elements in a collection are None. It provides a fallback function to execute when this condition is met. ```gleam use <- given.all_none(in: options, else_return: fn(_somes, _nones_count) { "There is something out there..." }) ``` -------------------------------- ### Execute code when a condition is true with `given.when` Source: https://hexdocs.pm/given/index Shows how `given.when` executes a block when a condition (represented by a function returning a boolean) is true. It includes an `else_return` for the false case. ```gleam import given pub fn given_when_example() { let enabled = fn() { True } use <- given.when(enabled, else_return: fn() { "Not an Admin" }) // …handle case where user is an Admin… "Indeed an Admin" } ``` -------------------------------- ### Handle empty lists with `given.empty` Source: https://hexdocs.pm/given/index Illustrates `given.empty` for checking if a list is empty. It takes a list and a return function. The return function is executed if the list is empty. ```gleam import given pub fn given_empty_example() { let list = [] use <- given.empty(list, return: fn() { "Empty!" }) // …handle case where user is non-empty… "Non-empty!" } ``` -------------------------------- ### Handle all `Error` results with `given.all_error` Source: https://hexdocs.pm/given/index Shows `given.all_error` for verifying if all results in a list are `Error`. It provides `_oks` and `_errors` for handling, with an `else_return` for cases with any `Ok` results. ```gleam import given pub fn given_all_error_example() { let results = [Ok("Nice"), Error("Meh")] use _errors <- given.all_error(in: results, else_return: fn(_oks, _errors) { "Meh" }) // …handle all errors here… "Nice" } ``` -------------------------------- ### Handle True conditions with `given.that` Source: https://hexdocs.pm/given/index Demonstrates how to use `given.that` to execute a block of code when a condition is true. It takes a boolean condition and a return function. If the condition is true, the return function is executed. ```gleam import given pub fn given_that_example() { let user_understood = True use <- given.that(user_understood, return: fn() { "💡 Bright!" }) // …else handle case where user did not understand here… "🤯 Woof!" } ``` -------------------------------- ### Given.all_none - Handle list of Options Source: https://hexdocs.pm/given/given Checks if all elements in a list of Options are `None`. If all are `None`, it executes the consequence function. Otherwise, it executes the alternative function, passing the `Some` values and the count of `None` values. ```gleam import given let options = [Some("One"), None] use <- given.all_none(in: options, else_return: fn(_somes, _nones_count) { "Some are Some" }) // …handle all None values here… "All are None" ``` -------------------------------- ### any_some Function Source: https://hexdocs.pm/given/given Checks if any of the provided options are `Some`. Executes the consequence function with a list of `Some` values and a count of `None` values if any option is `Some`, otherwise executes the alternative function with the count of `None` values. It takes a list of Options and two functions as arguments. ```gleam pub fn any_some( in optns: List(option.Option(a)), else_return alternative: fn(Int) -> b, return consequence: fn(List(a), Int) -> b, ) -> b ``` -------------------------------- ### Handle at least one `Error` result with `given.any_error` Source: https://hexdocs.pm/given/index Explains `given.any_error` for checking if at least one `Error` result exists in a list of results. It provides `_oks` and `_errors` for handling, with an `else_return` for the case where all are `Ok`. ```gleam import given pub fn given_any_error_example() { let results = [Ok("Good"), Error("Bad")] use _oks, _errors <- given.any_error(in: results, else_return: fn(_oks) { "Bad" }) // …handle at least some Errors here… "Good" } ``` -------------------------------- ### Check if any condition is true with `given.any` Source: https://hexdocs.pm/given/index Shows how `given.any` checks if at least one boolean in a list is true. It takes a list of booleans and a return function. If any element is true, the return function is called. ```gleam import given pub fn given_any_example() { let is_admin = False let is_editor = True use <- given.any([is_admin, is_editor], return: fn() { "At least admin or editor" }) // …else handle case where user has no special role… "Got nothing to say 🤷‍♂️" } ``` -------------------------------- ### any_ok Function Source: https://hexdocs.pm/given/given Checks if any of the provided results are `Ok`. Executes the consequence function with lists of `Ok` and `Error` values if any result is `Ok`, otherwise executes the alternative function with a list of all `Error` values. It takes a list of Results and two functions as arguments. ```gleam pub fn any_ok( in rslts: List(Result(a, e)), else_return alternative: fn(List(e)) -> b, return consequence: fn(List(a), List(e)) -> b, ) -> b ``` -------------------------------- ### Check if all conditions are true with `given.all` Source: https://hexdocs.pm/given/index Explains `given.all`, which verifies if all booleans in a list are true. It requires a list of booleans and a return function. The return function executes only if all elements in the list are true. ```gleam import given pub fn given_all_example() { let is_active = True let is_confirmed = False use <- given.all([is_active, is_confirmed], return: fn() { "Ready, steady, go!" }) // …else handle case where user is not both active and confirmed… "Not both active and confirmed" } ``` -------------------------------- ### Check if any condition is false with `given.any_not` Source: https://hexdocs.pm/given/index Demonstrates `given.any_not`, which checks if at least one boolean in a list is false. It takes a list of booleans and a return function. The return function is executed if any element is false. ```gleam import given pub fn given_any_not_example() { let is_admin = False let is_editor = True use <- given.any_not([is_admin, is_editor], return: fn() { "At least either Admin or Editor!" }) // …else handle case where user no special role… "User has no special role!" } ``` -------------------------------- ### Handle non-empty lists with `given.non_empty` Source: https://hexdocs.pm/given/index Shows `given.non_empty` for checking if a list contains elements. It takes a list and a return function. The return function is executed if the list is not empty. ```gleam import given pub fn given_non_empty_example() { let list = [1] use <- given.non_empty(list, return: fn() { "Non-empty!" }) // …handle case where user is empty… "Empty!" } ``` -------------------------------- ### Handle Ok Result Source: https://hexdocs.pm/given/given Processes a Gleam Result type, executing a consequence if it's an Ok value, or an alternative if it's an Error. ```gleam import given let result = Ok("Great") use ok_value <- given.ok(in: result, else_return: fn(error_value) { "Error" }) // …handle Ok value here… "Ok" ``` ```gleam import given.{ok as given_ok_in} let result = Ok("Great") use ok_value <- given_ok_in(result, else_return: fn(error_value) { "Error" }) // …handle Ok value here… "Ok" ``` -------------------------------- ### any_none - Gleam Source: https://hexdocs.pm/given/given Checks if any element in a list of Options is None. If any are None, it executes a consequence function. Otherwise, it executes an alternative function with the Some values and a count of None values. This is useful for determining if any optional values are missing. ```gleam pub fn any_none( in optns: List([option.Option](https://hexdocs.pm/gleam_stdlib/0.61.0/gleam/option.html#Option "gleam/option.{type Option}")(a)), else_return alternative: fn(List(a)) -> b, return consequence: fn(List(a), Int) -> b, ) -> b ``` -------------------------------- ### Handle `Error` results with `given.error` Source: https://hexdocs.pm/given/index Demonstrates `given.error` for extracting the value from an `Error` result. It takes a result and an `else_return` function for `Ok` cases. ```gleam import given pub fn given_error_example() { let a_result = Error("Memory exhausted, again!") use error_value <- given.error(in: a_result, else_return: fn(_ok_value) { "Ok value" }) // …handle Error value here… error_value } ``` -------------------------------- ### Execute code when a condition is false with `given.when_not` Source: https://hexdocs.pm/given/index Demonstrates `given.when_not`, which executes code when a condition (represented by a function returning a boolean) is false. It includes an `else_return` for the true case. ```gleam import given pub fn given_when_not_example() { let enabled = fn() { False } use <- given.when_not(enabled, else_return: fn() { "Indeed an Admin" }) // …handle case where user is not an Admin… "Not an Admin" } ``` -------------------------------- ### all_some - Gleam Source: https://hexdocs.pm/given/given Checks if all elements in a list of Options are Some. If all are Some, it executes a consequence function with the Some values. Otherwise, it executes an alternative function with the Some values and a count of None values. This is useful for handling optional data structures. ```gleam pub fn all_some( in optns: List([option.Option](https://hexdocs.pm/gleam_stdlib/0.61.0/gleam/option.html#Option "gleam/option.{type Option}")(a)), else_return alternative: fn(List(a), Int) -> b, return consequence: fn(List(a)) -> b, ) -> b ``` -------------------------------- ### Handle False conditions with `given.not` Source: https://hexdocs.pm/given/index Illustrates the use of `given.not` to execute code when a condition is false. It accepts a boolean condition and a return function. If the condition is false, the return function is executed. ```gleam import given pub fn given_not_example() { // Fetch this from some database let has_admin_role = True use <- given.not(has_admin_role, return: fn() { "✋ Denied!" }) // …else handle case where they are admin here… "👌 Access granted..." } ``` -------------------------------- ### all_ok - Gleam Source: https://hexdocs.pm/given/given Checks if all elements in a list of Results are Ok. If all are Ok, it executes a consequence function with the Ok values. Otherwise, it executes an alternative function with both Ok and Error values. This is useful for processing lists where errors might occur. ```gleam pub fn all_ok( in rslts: List(Result(a, e)), else_return alternative: fn(List(a), List(e)) -> b, return consequence: fn(List(a)) -> b, ) -> b ``` -------------------------------- ### Check if all conditions are false with `given.all_not` Source: https://hexdocs.pm/given/index Explains `given.all_not`, which verifies if all booleans in a list are false. It requires a list of booleans and a return function. The return function executes only if all elements in the list are false. ```gleam import given pub fn given_all_not_example() { let is_human = False let is_robot = False use <- given.all_not([is_human, is_robot], return: fn() { "Obsolete model detected." }) // …else handle case where user is neither active nor confirmed… "I am a Cylon!" } ``` -------------------------------- ### none Function Source: https://hexdocs.pm/given/given Checks if a given option is `None`. Executes the consequence function if the option is `None`, otherwise executes the alternative function with the contained value. It takes an Option and two functions as arguments. ```gleam pub fn none( in optn: option.Option(a), else_return alternative: fn(a) -> b, return consequence: fn() -> b, ) -> b ``` -------------------------------- ### any - Gleam Source: https://hexdocs.pm/given/given Checks if any condition in a list of booleans is true. Executes a consequence function if at least one condition is true, otherwise executes an alternative function. This is useful for checking if any of multiple criteria are met. ```gleam pub fn any( are_true_in requirements: List(Bool), return consequence: fn() -> b, else_return alternative: fn() -> b, ) -> b ``` -------------------------------- ### Given.all - Check if all conditions are true Source: https://hexdocs.pm/given/given Checks if all of the conditions in the provided list are true. If all are true, it executes the consequence function; otherwise, it executes the alternative function. This is useful for validating multiple conditions before proceeding. ```gleam import given let is_active = True let is_confirmed = True use <- given.all([is_active, is_confirmed], return: fn() { "Great!" }) // …else handle case where user is not both active and confirmed… "Woof!" ``` ```gleam import given let is_active = True let is_confirmed = True use <- given.all(are_true_in: [is_active, is_confirmed], return: fn() { "Great!" }) // …else handle case where user is not both active and confirmed… "Woof!" ``` -------------------------------- ### Given Library API: when_not Function Signature Source: https://hexdocs.pm/given/given Provides the function signature for 'given.when_not', detailing its parameters and return type. This function checks if a condition is false and executes consequences accordingly. ```APIDOC pub fn when_not( the_case condition: fn() -> Bool, else_return alternative: fn() -> b, return consequence: fn() -> b, ) -> b -- Checks if the condition function returns `False` and runs the consequence if it is, else runs the alternative. -- Use to lazily evaluate a complex condition and return early if they fail. ``` -------------------------------- ### Conditional Execution with Function (when) Source: https://hexdocs.pm/given/given Executes a consequence if a provided function returns true, otherwise executes an alternative. Allows for lazy evaluation of conditions. ```gleam import given let condition = fn() { True } use <- given.when(the_case: condition, else_return: fn() { "Alternative" }, return: fn() { "Consequence" }) // …handle result here… "Result" ``` -------------------------------- ### any_not Function Source: https://hexdocs.pm/given/given Checks if any of the provided boolean conditions are false. Executes the consequence function if any condition is false, otherwise executes the alternative function. It takes a list of booleans and two functions as arguments. ```gleam pub fn any_not( are_true_in requirements: List(Bool), return consequence: fn() -> b, else_return alternative: fn() -> b, ) -> b ``` -------------------------------- ### all_not - Gleam Source: https://hexdocs.pm/given/given Checks if all conditions in a list are false. Executes a consequence function if all are false, otherwise executes an alternative function. Useful for validating multiple conditions before proceeding. ```gleam pub fn all_not( are_true_in requirements: List(Bool), return consequence: fn() -> b, else_return alternative: fn() -> b, ) -> b ``` -------------------------------- ### Given.all_error - Handle list of Results Source: https://hexdocs.pm/given/given Checks if all elements in a list of Results are of the `Error` variant. If all are `Error`, it executes the consequence function, passing the list of error values. Otherwise, it executes the alternative function, passing both `Ok` and `Error` values. ```gleam import given let results = [Ok("Great"), Error("Bad")] use _errors <- given.all_error(in: results, else_return: fn(_oks, _errors) { "Only some Errors" }) // …handle all errors here… "All Errors" ``` -------------------------------- ### empty Function Source: https://hexdocs.pm/given/given Checks if a given list is empty. Executes the consequence function if the list is empty, otherwise executes the alternative function. It takes a list and two functions as arguments. ```gleam pub fn empty( list list: List(a), else_return alternative: fn() -> b, return consequence: fn() -> b, ) -> b ``` -------------------------------- ### any_error - Gleam Source: https://hexdocs.pm/given/given Checks if any element in a list of Results is an Error. If any are Error, it executes a consequence function with both Ok and Error values. Otherwise, it executes an alternative function with only the Ok values. This is useful for identifying the presence of errors in a list of operations. ```gleam pub fn any_error( in rslts: List(Result(a, e)), else_return alternative: fn(List(a)) -> b, return consequence: fn(List(a), List(e)) -> b, ) -> b ``` -------------------------------- ### non_empty Function Source: https://hexdocs.pm/given/given Checks if a given list is non-empty. Executes the consequence function if the list is non-empty, otherwise executes the alternative function. It takes a list and two functions as arguments. ```gleam pub fn non_empty( list list: List(a), else_return alternative: fn() -> b, return consequence: fn() -> b, ) -> b ``` -------------------------------- ### Check for True Condition (that) Source: https://hexdocs.pm/given/given Executes a consequence if a boolean condition is true, otherwise executes an alternative. Useful for asserting positive conditions. ```gleam import given let user_understood = True use <- given.that(user_understood, return: fn() { "Great!" }) // …else handle case where user did not understand here… "Woof!" ``` ```gleam import given.{that as given} let user_understood = True use <- given(user_understood, return: fn() { "Great!" }) // …else handle case where user did not understand here… "Woof!" ``` -------------------------------- ### error Function Source: https://hexdocs.pm/given/given Checks if a given result is an `Error`. Executes the consequence function with the error value if the result is an `Error`, otherwise executes the alternative function with the success value. It takes a Result and two functions as arguments. ```gleam pub fn error( in rslt: Result(a, e), else_return alternative: fn(a) -> b, return consequence: fn(e) -> b, ) -> b ``` -------------------------------- ### Check for False Condition (not) Source: https://hexdocs.pm/given/given Executes a consequence if a boolean condition is false, otherwise executes an alternative. Useful for asserting negative conditions. ```gleam import given let user_understood = True use <- given.not(user_understood, return: fn() { "Woof!" }) // …else handle case where user understood here… "Great!" ``` ```gleam import given let user_understood = True use <- given.not(the_case: user_understood, return: fn() { "Woof!" }) // …else handle case where user understood here… "Great!" ``` ```gleam import given.{not as not_given} let user_understood = True use <- not_given(user_understood, return: fn() { "Woof!" }) // …else handle case where user understood here… "Great!" ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.