-module(given). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -define(FILEPATH, "src/given.gleam"). -export([that/3, any/3, all/3, 'not'/3, any_not/3, not_any/3, all_not/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(() -> DUM), fun(() -> DUM)) -> DUM. 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(() -> DUO), fun(() -> DUO)) -> DUO. 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(() -> DUQ), fun(() -> DUQ)) -> DUQ. 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(() -> DUR), fun(() -> DUR)) -> DUR. '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 any_not(list(boolean()), fun(() -> DUT), fun(() -> DUT)) -> DUT. any_not(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", 223). ?DOC(" See any_not\n"). -spec not_any(list(boolean()), fun(() -> DUV), fun(() -> DUV)) -> DUV. not_any(Requirements, Consequence, Alternative) -> any_not(Requirements, Consequence, Alternative). -file("src/given.gleam", 263). ?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 all_not(list(boolean()), fun(() -> DUX), fun(() -> DUX)) -> DUX. all_not(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", 277). ?DOC(" See all_not\n"). -spec not_all(list(boolean()), fun(() -> DUZ), fun(() -> DUZ)) -> DUZ. not_all(Requirements, Consequence, Alternative) -> all_not(Requirements, Consequence, Alternative). -file("src/given.gleam", 318). ?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(() -> DVA), fun(() -> DVA)) -> DVA. 'when'(Condition, Alternative, Consequence) -> case Condition() of true -> Consequence(); false -> Alternative() end. -file("src/given.gleam", 358). ?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(() -> DVB), fun(() -> DVB)) -> DVB. when_not(Condition, Alternative, Consequence) -> case Condition() of false -> Consequence(); true -> Alternative() end. -file("src/given.gleam", 385). ?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(() -> DVE), fun(() -> DVE)) -> DVE. empty(List, Alternative, Consequence) -> case List of [] -> Consequence(); _ -> Alternative() end. -file("src/given.gleam", 412). ?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(() -> DVH), fun(() -> DVH)) -> DVH. non_empty(List, Alternative, Consequence) -> case List of [] -> Alternative(); _ -> Consequence() end. -file("src/given.gleam", 450). ?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, DVI} | {error, DVJ}, fun((DVJ) -> DVM), fun((DVI) -> DVM)) -> DVM. ok(Rslt, Alternative, Consequence) -> case Rslt of {ok, Val} -> Consequence(Val); {error, Err} -> Alternative(Err) end. -file("src/given.gleam", 478). ?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, DVN} | {error, DVO}), fun((list(DVO)) -> DVT), fun((list(DVN), list(DVO)) -> DVT) ) -> DVT. 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", 508). ?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, DVW} | {error, DVX}), fun((list(DVW), list(DVX)) -> DWD), fun((list(DVW)) -> DWD) ) -> DWD. 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", 548). ?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, DWF} | {error, DWG}, fun((DWF) -> DWJ), fun((DWG) -> DWJ)) -> DWJ. error(Rslt, Alternative, Consequence) -> case Rslt of {error, Err} -> Consequence(Err); {ok, Val} -> Alternative(Val) end. -file("src/given.gleam", 576). ?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, DWK} | {error, DWL}), fun((list(DWK)) -> DWQ), fun((list(DWK), list(DWL)) -> DWQ) ) -> DWQ. 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", 606). ?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, DWT} | {error, DWU}), fun((list(DWT), list(DWU)) -> DXA), fun((list(DWU)) -> DXA) ) -> DXA. 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", 648). ?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(DXC), fun(() -> DXE), fun((DXC) -> DXE)) -> DXE. some(Optn, Alternative, Consequence) -> case Optn of {some, Val} -> Consequence(Val); none -> Alternative() end. -file("src/given.gleam", 676). ?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(DXF)), fun((integer()) -> DXI), fun((list(DXF), integer()) -> DXI) ) -> DXI. 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", 706). ?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(DXK)), fun((list(DXK), integer()) -> DXO), fun((list(DXK)) -> DXO) ) -> DXO. 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", 748). ?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(DXQ), fun((DXQ) -> DXS), fun(() -> DXS)) -> DXS. none(Optn, Alternative, Consequence) -> case Optn of none -> Consequence(); {some, Val} -> Alternative(Val) end. -file("src/given.gleam", 776). ?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(DXT)), fun((list(DXT)) -> DXX), fun((list(DXT), integer()) -> DXX) ) -> DXX. 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", 805). ?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(DXZ)), fun((list(DXZ), integer()) -> DYD), fun(() -> DYD) ) -> DYD. 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.