Current section
Files
Jump to
Current section
Files
src/given.erl
-module(given).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/given.gleam").
-export([that/3, any/3, all/3, 'not'/3, any_not/3, all_not/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"
" - Comprehensible by not having to negate the conditions.\n"
" - Safe to execute because:\n"
" - either and or branches are enforced.\n"
" - not running discarded branch side effects by accident (much like\n"
" Gleam's standard library `bool.lazy_guard`, and its `bool.guard`).\n"
"\n"
).
-file("src/given.gleam", 35).
?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"
" let user_understood = True\n"
"\n"
" use <- given.that(user_understood, else_return: fn() { \"Woof!\" })\n"
"\n"
" \"๐ก Bright!\"\n"
" ```\n"
).
-spec that(boolean(), fun(() -> DLL), fun(() -> DLL)) -> DLL.
that(Requirement, Alternative, Consequence) ->
case Requirement of
true ->
Consequence();
false ->
Alternative()
end.
-file("src/given.gleam", 60).
?DOC(
" Checks if any of the conditions are `True` and runs the consequence if any\n"
" are, otherwise runs the alternative.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" let is_admin = False\n"
" let is_editor = True\n"
"\n"
" use <- given.any([is_admin, is_editor], else_return: fn() { \"Cannot pass!\" })\n"
"\n"
" \"๐ต Snap - I've got the power!\"\n"
" ```\n"
).
-spec any(list(boolean()), fun(() -> DLN), fun(() -> DLN)) -> DLN.
any(Requirements, Alternative, Consequence) ->
case begin
_pipe = Requirements,
gleam@list:any(_pipe, fun(V) -> V =:= true end)
end of
true ->
Consequence();
false ->
Alternative()
end.
-file("src/given.gleam", 85).
?DOC(
" Checks if all of the conditions are `True` and runs the consequence if all\n"
" are, otherwise runs the alternative.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" let is_active = True\n"
" let is_confirmed = True\n"
"\n"
" use <- given.all([is_active, is_confirmed], else_return: fn() { \"Stop!\" })\n"
"\n"
" \"๐ Ready, steady, go!\"\n"
" ```\n"
).
-spec all(list(boolean()), fun(() -> DLP), fun(() -> DLP)) -> DLP.
all(Requirements, Alternative, Consequence) ->
case begin
_pipe = Requirements,
gleam@list:all(_pipe, fun(V) -> V =:= true end)
end of
true ->
Consequence();
false ->
Alternative()
end.
-file("src/given.gleam", 109).
?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"
" let has_admin_role = False\n"
"\n"
" use <- given.not(has_admin_role, else_return: fn() { \"Access granted!\" })\n"
"\n"
" \"โ Denied!\"\n"
" ```\n"
).
-spec 'not'(boolean(), fun(() -> DLQ), fun(() -> DLQ)) -> DLQ.
'not'(Requirement, Alternative, Consequence) ->
case Requirement =:= false of
true ->
Consequence();
false ->
Alternative()
end.
-file("src/given.gleam", 136).
?DOC(
" Checks if any of the conditions are `False` and runs the consequence if any\n"
" are, otherwise runs the alternative.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" let got_veggies = True\n"
" let got_spices = False\n"
"\n"
" use <- given.any_not([got_veggies, got_spices], else_return: fn() {\n"
" \"Preparing a soup!\"\n"
" })\n"
"\n"
" \"๐ญ Ingredient missing...\"\n"
" ```\n"
).
-spec any_not(list(boolean()), fun(() -> DLS), fun(() -> DLS)) -> DLS.
any_not(Requirements, Alternative, Consequence) ->
case begin
_pipe = Requirements,
gleam@list:any(_pipe, fun(V) -> V =:= false end)
end of
true ->
Consequence();
false ->
Alternative()
end.
-file("src/given.gleam", 163).
?DOC(
" Checks if all of the conditions are `False` and runs the consequence if all\n"
" are, otherwise runs the alternative.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" let is_android = False\n"
" let is_synthetic = False\n"
"\n"
" use <- given.all_not([is_android, is_synthetic], else_return: fn() {\n"
" \"I am a Cylon!\"\n"
" })\n"
"\n"
" \"๐ชฆ Obsolete model detected.\"\n"
" ```\n"
).
-spec all_not(list(boolean()), fun(() -> DLU), fun(() -> DLU)) -> DLU.
all_not(Requirements, Alternative, Consequence) ->
case begin
_pipe = Requirements,
gleam@list:all(_pipe, fun(V) -> V =:= false end)
end of
true ->
Consequence();
false ->
Alternative()
end.
-file("src/given.gleam", 193).
?DOC(
" Checks if the condition function returns `True` and runs the consequence if\n"
" it is, otherwise runs the alternative.\n"
"\n"
" Use to lazily evaluate a complex condition and return early if it fails.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" let enabled_in_db = fn() { True }\n"
"\n"
" use <- given.when(enabled_in_db, else_return: fn() { \"User disabled!\" })\n"
"\n"
" \"โ
User enabled\"\n"
" ```\n"
).
-spec 'when'(fun(() -> boolean()), fun(() -> DLV), fun(() -> DLV)) -> DLV.
'when'(Condition, Alternative, Consequence) ->
case Condition() of
true ->
Consequence();
false ->
Alternative()
end.
-file("src/given.gleam", 219).
?DOC(
" Checks if the condition function returns `False` and runs the consequence if\n"
" it is, otherwise 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"
" let enabled_in_db = fn() { False }\n"
"\n"
" use <- given.when_not(enabled_in_db, else_return: fn() { \"User enabled!\" })\n"
"\n"
" \"โ User disabled\"\n"
" ```\n"
).
-spec when_not(fun(() -> boolean()), fun(() -> DLW), fun(() -> DLW)) -> DLW.
when_not(Condition, Alternative, Consequence) ->
case Condition() =:= false of
true ->
Consequence();
false ->
Alternative()
end.
-file("src/given.gleam", 249).
?DOC(
" Checks if the list is empty and runs the consequence if it is, otherwise runs\n"
" the alternative.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" let list = []\n"
"\n"
" use <- given.empty(list, else_return: fn() {\n"
" \"Full as if you ate two large vegan ๐!\"\n"
" })\n"
"\n"
" \"๐ธ Empty like vast space!\"\n"
" ```\n"
).
-spec empty(list(any()), fun(() -> DLZ), fun(() -> DLZ)) -> DLZ.
empty(List, Alternative, Consequence) ->
case List of
[] ->
Consequence();
_ ->
Alternative()
end.
-file("src/given.gleam", 273).
?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"
" let list = [1]\n"
"\n"
" use <- given.non_empty(list, else_return: fn() { \"Empty like vast space! ๐ธ\" })\n"
"\n"
" \"๐ Full as if you ate two large vegan!\"\n"
" ```\n"
).
-spec non_empty(list(any()), fun(() -> DMC), fun(() -> DMC)) -> DMC.
non_empty(List, Alternative, Consequence) ->
case List of
[] ->
Alternative();
_ ->
Consequence()
end.
-file("src/given.gleam", 303).
?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"
" let result = Ok(\"๐ Hello Joe, again!\")\n"
"\n"
" use val <- given.ok(in: result, else_return: fn(_error) {\n"
" \"Joe is unreachable, now ๐.\"\n"
" })\n"
"\n"
" val\n"
" ```\n"
).
-spec ok({ok, DMD} | {error, DME}, fun((DME) -> DMH), fun((DMD) -> DMH)) -> DMH.
ok(Result, Alternative, Consequence) ->
case Result of
{ok, Val} ->
Consequence(Val);
{error, Err} ->
Alternative(Err)
end.
-file("src/given.gleam", 330).
?DOC(
" Checks if any of the results are `Ok` and runs the consequence - passing in\n"
" the `Ok` and `Error` values - if they are, otherwise runs the alternative passing\n"
" in all `Error` values.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" let results = [Ok(\"Happy\"), Error(\"Sad\")]\n"
"\n"
" use _oks, _errors <- given.any_ok(in: results, else_return: fn(_errors) {\n"
" \"All Error values!\"\n"
" })\n"
"\n"
" \"๐ At least one Ok values!\"\n"
" ```\n"
).
-spec any_ok(
list({ok, DMI} | {error, DMJ}),
fun((list(DMJ)) -> DMO),
fun((list(DMI), list(DMJ)) -> DMO)
) -> DMO.
any_ok(Results, Alternative, Consequence) ->
{Oks, Errors} = begin
_pipe = Results,
gleam@result:partition(_pipe)
end,
case Oks of
[] ->
Alternative(Errors);
_ ->
Consequence(Oks, Errors)
end.
-file("src/given.gleam", 359).
?DOC(
" Checks if all of the results are `Ok` and runs the consequence - passing in\n"
" the `Ok` values - if they are, otherwise runs the alternative passing in all\n"
" `Ok` and `Error` values.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" let results = [Ok(\"Happy\"), Ok(\"Glad\")]\n"
"\n"
" use _oks <- given.all_ok(in: results, else_return: fn(_oks, _errors) {\n"
" \"At least one Error value!\"\n"
" })\n"
"\n"
" \"๐๐ All Ok values\"\n"
" ```\n"
).
-spec all_ok(
list({ok, DMR} | {error, DMS}),
fun((list(DMR), list(DMS)) -> DMY),
fun((list(DMR)) -> DMY)
) -> DMY.
all_ok(Results, Alternative, Consequence) ->
{Oks, Errors} = begin
_pipe = Results,
gleam@result:partition(_pipe)
end,
case Errors of
[] ->
Consequence(Oks);
_ ->
Alternative(Oks, Errors)
end.
-file("src/given.gleam", 387).
?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"
" let result = Error(\"๐ป Memory exhausted!\")\n"
"\n"
" use val <- given.error(in: result, else_return: fn(_ok) {\n"
" \"Allocating memory...\"\n"
" })\n"
"\n"
" val\n"
" ```\n"
).
-spec error({ok, DNA} | {error, DNB}, fun((DNA) -> DNE), fun((DNB) -> DNE)) -> DNE.
error(Result, Alternative, Consequence) ->
case Result of
{error, Err} ->
Consequence(Err);
{ok, Val} ->
Alternative(Val)
end.
-file("src/given.gleam", 414).
?DOC(
" Checks if any of the results are `Error` and runs the consequence - passing\n"
" in the `Ok` and `Error` values - if they are, otherwise runs the alternative\n"
" passing in all `Ok` values.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" let results = [Ok(\"Happy\"), Error(\"Sad\")]\n"
"\n"
" use _oks, _errors <- given.any_error(in: results, else_return: fn(_oks) {\n"
" \"No Errors\"\n"
" })\n"
"\n"
" \"๐ง At least one Error occured!\"\n"
" ```\n"
).
-spec any_error(
list({ok, DNF} | {error, DNG}),
fun((list(DNF)) -> DNL),
fun((list(DNF), list(DNG)) -> DNL)
) -> DNL.
any_error(Results, Alternative, Consequence) ->
{Oks, Errors} = begin
_pipe = Results,
gleam@result:partition(_pipe)
end,
case Errors of
[] ->
Alternative(Oks);
_ ->
Consequence(Oks, Errors)
end.
-file("src/given.gleam", 443).
?DOC(
" Checks if all of the results are `Error` and runs the consequence - passing\n"
" in the `Error` values - if they are, otherwise runs the alternative passing in\n"
" all `Ok` and `Error` values.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" let results = [Error(\"Sad\"), Error(\"Lonely\")]\n"
"\n"
" use _errors <- given.all_error(in: results, else_return: fn(_oks, _errors) {\n"
" \"Life is good!\"\n"
" })\n"
"\n"
" \"โ Take care and learn to love yourself!\"\n"
" ```\n"
).
-spec all_error(
list({ok, DNO} | {error, DNP}),
fun((list(DNO), list(DNP)) -> DNV),
fun((list(DNP)) -> DNV)
) -> DNV.
all_error(Results, Alternative, Consequence) ->
{Oks, Errors} = begin
_pipe = Results,
gleam@result:partition(_pipe)
end,
case Oks of
[] ->
Consequence(Errors);
_ ->
Alternative(Oks, Errors)
end.
-file("src/given.gleam", 475).
?DOC(
" Checks if the option is `Some` and runs the consequence if it is, otherwise runs\n"
" the alternative.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" import gleam/option.{Some}\n"
"\n"
" let option = Some(\"๐ช One more penny\")\n"
"\n"
" use val <- given.some(in: option, else_return: fn() { \"Nothing to spare!\" })\n"
"\n"
" val\n"
" ```\n"
).
-spec some(gleam@option:option(DNX), fun(() -> DNZ), fun((DNX) -> DNZ)) -> DNZ.
some(Option, Alternative, Consequence) ->
case Option of
{some, Val} ->
Consequence(Val);
none ->
Alternative()
end.
-file("src/given.gleam", 505).
?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 gleam/option.{None, Some}\n"
"\n"
" let options = [Some(\"One\"), None]\n"
"\n"
" use _somes, _nones_count <- given.any_some(\n"
" in: options,\n"
" else_return: fn(_nones_count) { \"Nothing at all.\" },\n"
" )\n"
"\n"
" \"๐
At least one Some!\"\n"
" ```\n"
).
-spec any_some(
list(gleam@option:option(DOA)),
fun((integer()) -> DOD),
fun((list(DOA), integer()) -> DOD)
) -> DOD.
any_some(Options, Alternative, Consequence) ->
{Somes, Nones_count} = begin
_pipe = Options,
given@internal@lib@optionx:partition(_pipe)
end,
case Somes of
[] ->
Alternative(Nones_count);
_ ->
Consequence(Somes, Nones_count)
end.
-file("src/given.gleam", 537).
?DOC(
" Checks if all of the options are `Some` and runs the consequence - passing\n"
" in the `Some` values - if they are, otherwise runs the alternative passing in\n"
" the `Some` and a count of the `None` values.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" import gleam/option.{Some}\n"
"\n"
" let options = [Some(\"Treasure Chest\"), Some(\"Nugget\")]\n"
"\n"
" use _somes <- given.all_some(\n"
" in: options,\n"
" else_return: fn(_somes, _nones_count) { \"Nothing at all\" },\n"
" )\n"
"\n"
" \"๐
There is gold everywhere!\"\n"
" ```\n"
).
-spec all_some(
list(gleam@option:option(DOF)),
fun((list(DOF), integer()) -> DOJ),
fun((list(DOF)) -> DOJ)
) -> DOJ.
all_some(Options, Alternative, Consequence) ->
{Somes, Nones_count} = begin
_pipe = Options,
given@internal@lib@optionx:partition(_pipe)
end,
case Nones_count of
0 ->
Consequence(Somes);
_ ->
Alternative(Somes, Nones_count)
end.
-file("src/given.gleam", 579).
?DOC(
" Checks if the option is `None` and runs the consequence if it is, otherwise 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 gleam/option.{None}\n"
"\n"
" let option = None\n"
"\n"
" use <- given.none(in: option, else_return: fn(_some_value) {\n"
" \"There is someone sleeping!\"\n"
" })\n"
"\n"
" \"๐, aka None is in this bed!\"\n"
" ```\n"
).
-spec none(gleam@option:option(DOL), fun((DOL) -> DON), fun(() -> DON)) -> DON.
none(Option, Alternative, Consequence) ->
case Option of
none ->
Consequence();
{some, Val} ->
Alternative(Val)
end.
-file("src/given.gleam", 609).
?DOC(
" Checks if any of the options are `None` and runs the consequence if they\n"
" are, otherwise runs the alternative passing in the `Some` values and the count\n"
" of `None` values.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" import gleam/option.{None, Some}\n"
"\n"
" let options = [Some(\"One\"), None]\n"
"\n"
" use _somes, _none_count <- given.any_none(\n"
" in: options,\n"
" else_return: fn(_somes) { \"Only Somes here!\" },\n"
" )\n"
"\n"
" \"๐ณ๏ธ, aka None, detected in the system at least once.\"\n"
" ```\n"
).
-spec any_none(
list(gleam@option:option(DOO)),
fun((list(DOO)) -> DOS),
fun((list(DOO), integer()) -> DOS)
) -> DOS.
any_none(Options, Alternative, Consequence) ->
{Somes, Nones_count} = begin
_pipe = Options,
given@internal@lib@optionx:partition(_pipe)
end,
case Nones_count of
0 ->
Alternative(Somes);
_ ->
Consequence(Somes, Nones_count)
end.
-file("src/given.gleam", 639).
?DOC(
" Checks if all of the options are `None` and runs the consequence if they\n"
" are, otherwise runs the alternative passing in the `Some` values.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" import gleam/option.{None}\n"
"\n"
" let options = [None, None]\n"
"\n"
" use <- given.all_none(in: options, else_return: fn(_somes, _nones_count) {\n"
" \"Someone tipped me :)!\"\n"
" })\n"
"\n"
" \"๐ซ There is nothing in the jar...\"\n"
" ```\n"
).
-spec all_none(
list(gleam@option:option(DOU)),
fun((list(DOU), integer()) -> DOY),
fun(() -> DOY)
) -> DOY.
all_none(Options, Alternative, Consequence) ->
{Somes, Nones_count} = begin
_pipe = Options,
given@internal@lib@optionx:partition(_pipe)
end,
case Somes of
[] ->
Consequence();
_ ->
Alternative(Somes, Nones_count)
end.