-module(given). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([that/3, any/3, all/3, 'not'/3, not_any/3, not_all/3, 'when'/3, when_not/3, empty/3, non_empty/3, ok/3, any_ok/3, all_ok/3, error/3, any_error/3, all_error/3, some/3, any_some/3, all_some/3, none/3, any_none/3, all_none/3]). -if(?OTP_RELEASE >= 27). -define(MODULEDOC(Str), -moduledoc(Str)). -define(DOC(Str), -doc(Str)). -else. -define(MODULEDOC(Str), -compile([])). -define(DOC(Str), -compile([])). -endif. ?MODULEDOC( " This library attempts to make guards:\n" "\n" " - Applicable to `Bool`, `Result`, `Option` and `List` types.\n" " - Ergonomic to use by providing ways to handle both branches early.\n" " - Expressive by making it easy to read through function names and labels.\n" " - Comprehendable by not having to negate the conditions.\n" " - Safe to use by not accidentally running discarded branches much like\n" " `bool.lazy_guard`.\n" "\n" ). -file("src/given.gleam", 41). ?DOC( " Checks if the condition is true and runs the consequence if it is, else\n" " runs the alternative.\n" "\n" " ## Examples\n" "\n" " ```gleam\n" " import given\n" "\n" " let user_understood = True\n" "\n" " use <- given.that(user_understood, return: fn() { \"Great!\" })\n" " // …else handle case where user did not understand here…\n" " \"Woof!\"\n" " ```\n" "\n" " ```gleam\n" " import given.{that as given}\n" "\n" " let user_understood = True\n" "\n" " use <- given(user_understood, return: fn() { \"Great!\" })\n" " // …else handle case where user did not understand here…\n" " \"Woof!\"\n" " ```\n" ). -spec that(boolean(), fun(() -> EWG), fun(() -> EWG)) -> EWG. that(Requirement, Consequence, Alternative) -> case Requirement of true -> Consequence(); false -> Alternative() end. -file("src/given.gleam", 81). ?DOC( " Checks if any of the conditions are true and runs the consequence if any\n" " are, else runs the alternative.\n" "\n" " ## Examples\n" "\n" " ```gleam\n" " import given\n" "\n" " let is_admin = False\n" " let is_editor = True\n" "\n" " use <- given.any([is_admin, is_editor], return: fn() { \"Great!\" })\n" "\n" " // …else handle case where user has no special role…\n" " \"Woof!\"\n" " ```\n" "\n" " ```gleam\n" " import given\n" "\n" " let is_admin = False\n" " let is_editor = True\n" "\n" " use <- given.any(are_true_in: [is_admin, is_editor], return: fn() { \"Great!\" })\n" "\n" " // …else handle case where user has no special role…\n" " \"Woof!\"\n" " ```\n" ). -spec any(list(boolean()), fun(() -> EWI), fun(() -> EWI)) -> EWI. any(Requirements, Consequence, Alternative) -> case begin _pipe = Requirements, gleam@list:any(_pipe, fun(V) -> V =:= true end) end of true -> Consequence(); false -> Alternative() end. -file("src/given.gleam", 120). ?DOC( " Checks if all of the conditions are true and runs the consequence if all\n" " are, else runs the alternative.\n" "\n" " ## Examples\n" "\n" " ```gleam\n" " import given\n" "\n" " let is_active = True\n" " let is_confirmed = True\n" "\n" " use <- given.all([is_active, is_confirmed], return: fn() { \"Great!\" })\n" "\n" " // …else handle case where user is not both active and confirmed…\n" " \"Woof!\"\n" " ```\n" "\n" " ```gleam\n" " import given\n" "\n" " let is_active = True\n" " let is_confirmed = True\n" "\n" " use <- given.all(are_true_in: [is_active, is_confirmed], return: fn() { \"Great!\" })\n" "\n" " // …else handle case where user is not both active and confirmed…\n" " \"Woof!\"\n" " ```\n" ). -spec all(list(boolean()), fun(() -> EWK), fun(() -> EWK)) -> EWK. all(Requirements, Consequence, Alternative) -> case begin _pipe = Requirements, gleam@list:all(_pipe, fun(V) -> V =:= true end) end of true -> Consequence(); false -> Alternative() end. -file("src/given.gleam", 169). ?DOC( " Checks if the condition is false and runs the consequence if it is, else\n" " runs the alternative.\n" "\n" " ## Examples\n" "\n" " ```gleam\n" " import given\n" "\n" " let user_understood = True\n" "\n" " use <- given.not(user_understood, return: fn() { \"Woof!\" })\n" "\n" " // …else handle case where user understood here…\n" " \"Great!\"\n" " ```\n" "\n" " ```gleam\n" " import given\n" "\n" " let user_understood = True\n" "\n" " use <- given.not(the_case: user_understood, return: fn() { \"Woof!\" })\n" "\n" " // …else handle case where user understood here…\n" " \"Great!\"\n" " ```\n" "\n" " ```gleam\n" " import given.{not as not_given}\n" "\n" " let user_understood = True\n" "\n" " use <- not_given(user_understood, return: fn() { \"Woof!\" })\n" "\n" " // …else handle case where user understood here…\n" " \"Great!\"\n" " ```\n" ). -spec 'not'(boolean(), fun(() -> EWL), fun(() -> EWL)) -> EWL. 'not'(Requirement, Consequence, Alternative) -> case Requirement of false -> Consequence(); true -> Alternative() end. -file("src/given.gleam", 209). ?DOC( " Checks if any of the conditions are false and runs the consequence if any\n" " are, else runs the alternative.\n" "\n" " ## Examples\n" "\n" " ```gleam\n" " import given\n" "\n" " let is_admin = False\n" " let is_editor = True\n" "\n" " use <- given.not_any([is_admin, is_editor], return: fn() { \"At least either Admin or Editor!\" })\n" "\n" " // …else handle case where user no special role…\n" " \"Woof!\"\n" " ```\n" "\n" " ```gleam\n" " import given\n" "\n" " let is_admin = False\n" " let is_editor = True\n" "\n" " use <- given.not_any(are_true_in: [is_admin, is_editor], return: fn() { \"At least either Admin or Editor!\" })\n" "\n" " // …else handle case where user no special role…\n" " \"Woof!\"\n" " ```\n" ). -spec not_any(list(boolean()), fun(() -> EWN), fun(() -> EWN)) -> EWN. not_any(Requirements, Consequence, Alternative) -> case begin _pipe = Requirements, gleam@list:any(_pipe, fun(V) -> V =:= true end) end of false -> Consequence(); true -> Alternative() end. -file("src/given.gleam", 248). ?DOC( " Checks if all of the conditions are false and runs the consequence if all\n" " are, else runs the alternative.\n" "\n" " ## Examples\n" "\n" " ```gleam\n" " import given\n" "\n" " let is_active = True\n" " let is_confirmed = True\n" "\n" " use <- given.not_all([is_active, is_confirmed], return: fn() { \"Cylone Sleeper Agent!\" })\n" "\n" " // …else handle case where user is neither active nor confirmed…\n" " \"Woof!\"\n" " ```\n" "\n" " ```gleam\n" " import given\n" "\n" " let is_active = True\n" " let is_confirmed = True\n" "\n" " use <- given.not_all(are_true_in: [is_active, is_confirmed], return: fn() { \"Cylone Sleeper Agent!\" })\n" "\n" " // …else handle case where user is neither active nor confirmed…\n" " \"Woof!\"\n" ). -spec not_all(list(boolean()), fun(() -> EWP), fun(() -> EWP)) -> EWP. not_all(Requirements, Consequence, Alternative) -> case begin _pipe = Requirements, gleam@list:all(_pipe, fun(V) -> V =:= true end) end of false -> Consequence(); true -> Alternative() end. -file("src/given.gleam", 288). ?DOC( " Checks if the condition function returns `True` and runs the consequence if\n" " it is, else runs the alternative.\n" "\n" " Use to lazily evaluate a complex condition and return early if they fail.\n" "\n" " ## Examples\n" "\n" " ```gleam\n" " import given\n" "\n" " let enabled = fn() { False }\n" "\n" " use <- given.when(enabled, else_return: fn() { \"Not an Admin\" })\n" "\n" " // …handle case where user is an Admin…\n" " \"Indeed an Admin\"\n" " ```\n" "\n" " ```gleam\n" " import given\n" "\n" " let enabled = fn() { False }\n" "\n" " use <- given.when(enabled, return: fn() { \"Indeed an Admin\" })\n" "\n" " // …handle case where user is not an Admin…\n" " \"Not an Admin\"\n" " ```\n" ). -spec 'when'(fun(() -> boolean()), fun(() -> EWQ), fun(() -> EWQ)) -> EWQ. 'when'(Condition, Alternative, Consequence) -> case Condition() of true -> Consequence(); false -> Alternative() end. -file("src/given.gleam", 328). ?DOC( " Checks if the condition function returns `False` and runs the consequence if\n" " it is, else runs the alternative.\n" "\n" " Use to lazily evaluate a complex condition and return early if they fail.\n" "\n" " ## Examples\n" "\n" " ```gleam\n" " import given\n" "\n" " let enabled = fn() { False }\n" "\n" " use <- given.when_not(enabled, else_return: fn() { \"Indeed an Admin\" })\n" "\n" " // …handle case where user is not an Admin…\n" " \"Not an Admin\"\n" " ```\n" "\n" " ```gleam\n" " import given\n" "\n" " let enabled = fn() { False }\n" "\n" " use <- given.when_not(enabled, return: fn() { \"Not an Admin\" })\n" "\n" " // …handle case where user is an Admin…\n" " \"Indeed an Admin\"\n" " ```\n" ). -spec when_not(fun(() -> boolean()), fun(() -> EWR), fun(() -> EWR)) -> EWR. when_not(Condition, Alternative, Consequence) -> case Condition() of false -> Consequence(); true -> Alternative() end. -file("src/given.gleam", 355). ?DOC( " Checks if the list is empty and runs the consequence if it is, else runs\n" " the alternative.\n" "\n" " ## Examples\n" "\n" " ```gleam\n" " import given\n" "\n" " let list = []\n" "\n" " use <- given.empty(list, else_return: fn() { \"Non-empty\" })\n" "\n" " // …handle empty list here…\n" " \"Empty\"\n" " ```\n" ). -spec empty(list(any()), fun(() -> EWU), fun(() -> EWU)) -> EWU. empty(List, Alternative, Consequence) -> case List of [] -> Consequence(); _ -> Alternative() end. -file("src/given.gleam", 382). ?DOC( " Checks if the list is non-empty and runs the consequence if it is, else\n" " runs the alternative.\n" "\n" " ## Examples\n" "\n" " ```gleam\n" " import given\n" "\n" " let list = []\n" "\n" " use <- given.non_empty(list, else_return: fn() { \"Empty\" })\n" "\n" " // …handle non-empty list here…\n" " \"Non-empty\"\n" " ```\n" ). -spec non_empty(list(any()), fun(() -> EWX), fun(() -> EWX)) -> EWX. non_empty(List, Alternative, Consequence) -> case List of [] -> Alternative(); _ -> Consequence() end. -file("src/given.gleam", 420). ?DOC( " Checks if the result is an `Ok` and runs the consequence if it is, else\n" " runs the alternative.\n" "\n" " ## Examples\n" "\n" " ```gleam\n" " import given\n" "\n" " let result = Ok(\"Great\")\n" "\n" " use ok_value <- given.ok(in: result, else_return: fn(error_value) { \"Error\" })\n" "\n" " // …handle Ok value here…\n" " \"Ok\"\n" " ```\n" "\n" " ```gleam\n" " import given.{ok as given_ok_in}\n" "\n" " let result = Ok(\"Great\")\n" "\n" " use ok_value <- given_ok_in(result, else_return: fn(error_value) { \"Error\" })\n" "\n" " // …handle Ok value here…\n" " \"Ok\"\n" " ```\n" ). -spec ok({ok, EWY} | {error, EWZ}, fun((EWZ) -> EXC), fun((EWY) -> EXC)) -> EXC. ok(Rslt, Alternative, Consequence) -> case Rslt of {ok, Val} -> Consequence(Val); {error, Err} -> Alternative(Err) end. -file("src/given.gleam", 448). ?DOC( " Checks if any of the results are `Ok` and runs the consequence - passing in\n" " the `Ok` and `Error` values - if they are, else runs the alternative passing\n" " in all `Error` values.\n" "\n" " ## Examples\n" "\n" " ```gleam\n" " import given\n" "\n" " let results = [Ok(\"Great\"), Error(\"Bad\")]\n" "\n" " use _oks, _errors <- given.any_ok(in: results, else_return: fn(_errors) { \"All Errors\" })\n" "\n" " // …handle at least some OKs here…\n" " \"At least some OKs\"\n" " ```\n" ). -spec any_ok( list({ok, EXD} | {error, EXE}), fun((list(EXE)) -> EXJ), fun((list(EXD), list(EXE)) -> EXJ) ) -> EXJ. any_ok(Rslts, Alternative, Consequence) -> {Oks, Errors} = begin _pipe = Rslts, gleam@result:partition(_pipe) end, case Oks of [] -> Alternative(Errors); _ -> Consequence(Oks, Errors) end. -file("src/given.gleam", 478). ?DOC( " Checks if all of the results are `Ok` and runs the consequence - passing in\n" " the `Ok` values - if they are, else runs the alternative passing in all\n" " `Ok` and `Error` values.\n" "\n" " ## Examples\n" "\n" " ```gleam\n" " import given\n" "\n" " let results = [Ok(\"Great\"), Error(\"Bad\")]\n" "\n" " use oks <- given.all_ok(in: results, else_return: fn(_oks, _errors) { \"Some Errors\" })\n" "\n" " // …handle all OKs here…\n" " \"All OKs\"\n" " ```\n" ). -spec all_ok( list({ok, EXM} | {error, EXN}), fun((list(EXM), list(EXN)) -> EXT), fun((list(EXM)) -> EXT) ) -> EXT. all_ok(Rslts, Alternative, Consequence) -> {Oks, Errors} = begin _pipe = Rslts, gleam@result:partition(_pipe) end, case Errors of [] -> Consequence(Oks); _ -> Alternative(Oks, Errors) end. -file("src/given.gleam", 518). ?DOC( " Checks if the result is an `Error` and runs the consequence if it is, else\n" " runs the alternative.\n" "\n" " ## Examples\n" "\n" " ```gleam\n" " import given\n" "\n" " let result = Error(Nil)\n" "\n" " use error_value <- given.error(in: result, else_return: fn(ok_value) { \"Ok\" })\n" "\n" " // …handle Error value here…\n" " \"Error\"\n" " ```\n" "\n" " ```gleam\n" " import given.{error as given_error_in}\n" "\n" " let result = Error(Nil)\n" "\n" " use error_value <- given_error_in(result, else_return: fn(ok_value) { \"Ok\" })\n" "\n" " // …handle Error value here…\n" " \"Error\"\n" " ```\n" ). -spec error({ok, EXV} | {error, EXW}, fun((EXV) -> EXZ), fun((EXW) -> EXZ)) -> EXZ. error(Rslt, Alternative, Consequence) -> case Rslt of {error, Err} -> Consequence(Err); {ok, Val} -> Alternative(Val) end. -file("src/given.gleam", 546). ?DOC( " Checks if any of the results are `Error` and runs the consequence - passing\n" " in the `Ok` and `Error` values - if they are, else runs the alternative\n" " passing in all `Ok` values.\n" "\n" " ## Examples\n" "\n" " ```gleam\n" " import given\n" "\n" " let results = [Ok(\"Great\"), Error(\"Bad\")]\n" "\n" " use _oks, _errors <- given.any_error(in: results, else_return: fn(_oks) { \"Only OKs\" })\n" "\n" " // …handle at least some Errors here…\n" " \"At least some Errors\"\n" " ```\n" ). -spec any_error( list({ok, EYA} | {error, EYB}), fun((list(EYA)) -> EYG), fun((list(EYA), list(EYB)) -> EYG) ) -> EYG. any_error(Rslts, Alternative, Consequence) -> {Oks, Errors} = begin _pipe = Rslts, gleam@result:partition(_pipe) end, case Errors of [] -> Alternative(Oks); _ -> Consequence(Oks, Errors) end. -file("src/given.gleam", 576). ?DOC( " Checks if all of the results are `Error` and runs the consequence - passing\n" " in the `Error` values - if they are, else runs the alternative passing in\n" " all `Ok` and `Error` values.\n" "\n" " ## Examples\n" "\n" " ```gleam\n" " import given\n" "\n" " let results = [Ok(\"Great\"), Error(\"Bad\")]\n" "\n" " use _errors <- given.all_error(in: results, else_return: fn(_oks, _errors) { \"Only some Errors\" })\n" "\n" " // …handle all errors here…\n" " \"All Errors\"\n" " ```\n" ). -spec all_error( list({ok, EYJ} | {error, EYK}), fun((list(EYJ), list(EYK)) -> EYQ), fun((list(EYK)) -> EYQ) ) -> EYQ. all_error(Rslts, Alternative, Consequence) -> {Oks, Errors} = begin _pipe = Rslts, gleam@result:partition(_pipe) end, case Oks of [] -> Consequence(Errors); _ -> Alternative(Oks, Errors) end. -file("src/given.gleam", 618). ?DOC( " Checks if the option is `Some` and runs the consequence if it is, else runs\n" " the alternative.\n" "\n" " ## Examples\n" "\n" " ```gleam\n" " import given\n" " import gleam/option.{Some}\n" "\n" " let option = Some(\"One\")\n" "\n" " use some_value <- given.some(in: option, else_return: fn() { \"None\" })\n" "\n" " // …handle Some value here…\n" " \"Some value\"\n" " ```\n" "\n" " ```gleam\n" " import given.{some as given_some_in}\n" " import gleam/option.{Some}\n" "\n" " let option = Some(\"One\")\n" "\n" " use some_value <- given_some_in(option, else_return: fn() { \"None\" })\n" "\n" " // …handle Some value here…\n" " \"Some value\"\n" " ```\n" ). -spec some(gleam@option:option(EYS), fun(() -> EYU), fun((EYS) -> EYU)) -> EYU. some(Optn, Alternative, Consequence) -> case Optn of {some, Val} -> Consequence(Val); none -> Alternative() end. -file("src/given.gleam", 646). ?DOC( " Checks if any of the options are `Some` and runs the consequence - passing\n" " in the `Some` values and a count of the `None` values - if they are, else\n" " runs the alternative passing in the count of `None` values.\n" "\n" " ## Examples\n" "\n" " ```gleam\n" " import given\n" "\n" " let options = [Some(\"One\"), None]\n" "\n" " use _somes, _nones_count <- given.any_some(in: options, else_return: fn(_nones_count) { \"All are None\" })\n" "\n" " // …handle at least some None values here…\n" " \"At least some are None\"\n" " ```\n" ). -spec any_some( list(gleam@option:option(EYV)), fun((integer()) -> EYY), fun((list(EYV), integer()) -> EYY) ) -> EYY. any_some(Optns, Alternative, Consequence) -> {Somes, Nones_count} = begin _pipe = Optns, given@internal@lib@optionx:partition(_pipe) end, case Somes of [] -> Alternative(Nones_count); _ -> Consequence(Somes, Nones_count) end. -file("src/given.gleam", 676). ?DOC( " Checks if all of the options are `Some` and runs the consequence - passing\n" " in the `Some` values - if they are, else runs the alternative passing in\n" " the `Some` and a count of the `None` values.\n" "\n" " ## Examples\n" "\n" " ```gleam\n" " import given\n" "\n" " let options = [Some(\"One\"), None]\n" "\n" " use _somes <- given.all_some(in: options, else_return: fn(_somes, _nones_count) { \"Some are None\" })\n" "\n" " // …handle all Some values here…\n" " \"All are Some\"\n" " ```\n" ). -spec all_some( list(gleam@option:option(EZA)), fun((list(EZA), integer()) -> EZE), fun((list(EZA)) -> EZE) ) -> EZE. all_some(Optns, Alternative, Consequence) -> {Somes, Nones_count} = begin _pipe = Optns, given@internal@lib@optionx:partition(_pipe) end, case Nones_count of 0 -> Consequence(Somes); _ -> Alternative(Somes, Nones_count) end. -file("src/given.gleam", 718). ?DOC( " Checks if the option is `None` and runs the consequence if it is, else runs\n" " the alternative.\n" "\n" " ## Examples\n" "\n" " ```gleam\n" " import given\n" " import gleam/option.{None}\n" "\n" " let option = None\n" "\n" " use <- given.none(in: option, else_return: fn(some_value) { \"Some value\" })\n" "\n" " // …handle None here…\n" " \"None\"\n" " ```\n" "\n" " ```gleam\n" " import given.{none as given_none_in}\n" " import gleam/option.{None}\n" "\n" " let option = None\n" "\n" " use <- given_none_in(option, else_return: fn(some_value) { \"Some value\" })\n" " // …handle None here…\n" "\n" " \"None\"\n" " ```\n" ). -spec none(gleam@option:option(EZG), fun((EZG) -> EZI), fun(() -> EZI)) -> EZI. none(Optn, Alternative, Consequence) -> case Optn of none -> Consequence(); {some, Val} -> Alternative(Val) end. -file("src/given.gleam", 746). ?DOC( " Checks if any of the options are `None` and runs the consequence if they\n" " are, else runs the alternative passing in the `Some` values and the count\n" " of `None` values.\n" "\n" " ## Examples\n" "\n" " ```gleam\n" " import given\n" "\n" " let options = [Some(\"One\"), None]\n" "\n" " use <- given.any_none(in: options, else_return: fn(_somes) { \"All are Some\" })\n" "\n" " // …handle at least some None values here…\n" " \"At least some are None\"\n" " ```\n" ). -spec any_none( list(gleam@option:option(EZJ)), fun((list(EZJ)) -> EZN), fun((list(EZJ), integer()) -> EZN) ) -> EZN. any_none(Optns, Alternative, Consequence) -> {Somes, Nones_count} = begin _pipe = Optns, given@internal@lib@optionx:partition(_pipe) end, case Nones_count of 0 -> Alternative(Somes); _ -> Consequence(Somes, Nones_count) end. -file("src/given.gleam", 775). ?DOC( " Checks if all of the options are `None` and runs the consequence if they\n" " are, else runs the alternative passing in the `Some` values.\n" "\n" " ## Examples\n" "\n" " ```gleam\n" " import given\n" "\n" " let options = [Some(\"One\"), None]\n" "\n" " use <- given.all_none(in: options, else_return: fn(_somes, _nones_count) { \"Some are Some\" })\n" "\n" " // …handle all None values here…\n" " \"All are None\"\n" " ```\n" ). -spec all_none( list(gleam@option:option(EZP)), fun((list(EZP), integer()) -> EZT), fun(() -> EZT) ) -> EZT. all_none(Optns, Alternative, Consequence) -> {Somes, Nones_count} = begin _pipe = Optns, given@internal@lib@optionx:partition(_pipe) end, case Somes of [] -> Consequence(); _ -> Alternative(Somes, Nones_count) end.