Current section

Files

Jump to
given src given.erl
Raw

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(() -> DRH), fun(() -> DRH)) -> DRH.
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(() -> DRJ), fun(() -> DRJ)) -> DRJ.
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(() -> DRL), fun(() -> DRL)) -> DRL.
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(() -> DRM), fun(() -> DRM)) -> DRM.
'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(() -> DRO), fun(() -> DRO)) -> DRO.
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(" ```\n").
-spec all_not(list(boolean()), fun(() -> DRQ), fun(() -> DRQ)) -> DRQ.
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(() -> DRR), fun(() -> DRR)) -> DRR.
'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(() -> DRS), fun(() -> DRS)) -> DRS.
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(() -> DRV), fun(() -> DRV)) -> DRV.
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(() -> DRY), fun(() -> DRY)) -> DRY.
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, DRZ} | {error, DSA}, fun((DSA) -> DSD), fun((DRZ) -> DSD)) -> DSD.
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, DSE} | {error, DSF}),
fun((list(DSF)) -> DSK),
fun((list(DSE), list(DSF)) -> DSK)
) -> DSK.
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, DSN} | {error, DSO}),
fun((list(DSN), list(DSO)) -> DSU),
fun((list(DSN)) -> DSU)
) -> DSU.
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, DSW} | {error, DSX}, fun((DSW) -> DTA), fun((DSX) -> DTA)) -> DTA.
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, DTB} | {error, DTC}),
fun((list(DTB)) -> DTH),
fun((list(DTB), list(DTC)) -> DTH)
) -> DTH.
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, DTK} | {error, DTL}),
fun((list(DTK), list(DTL)) -> DTR),
fun((list(DTL)) -> DTR)
) -> DTR.
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(DTT), fun(() -> DTV), fun((DTT) -> DTV)) -> DTV.
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(DTW)),
fun((integer()) -> DTZ),
fun((list(DTW), integer()) -> DTZ)
) -> DTZ.
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(DUB)),
fun((list(DUB), integer()) -> DUF),
fun((list(DUB)) -> DUF)
) -> DUF.
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(DUH), fun((DUH) -> DUJ), fun(() -> DUJ)) -> DUJ.
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(DUK)),
fun((list(DUK)) -> DUO),
fun((list(DUK), integer()) -> DUO)
) -> DUO.
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(DUQ)),
fun((list(DUQ), integer()) -> DUU),
fun(() -> DUU)
) -> DUU.
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.