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]).
-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, not_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", 43).
?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(() -> FHB), fun(() -> FHB)) -> FHB.
that(Requirement, Consequence, Alternative) ->
case Requirement of
true ->
Consequence();
false ->
Alternative()
end.
-file("src/given.gleam", 83).
?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(() -> FHD), fun(() -> FHD)) -> FHD.
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", 122).
?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(() -> FHF), fun(() -> FHF)) -> FHF.
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", 171).
?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(() -> FHG), fun(() -> FHG)) -> FHG.
'not'(Requirement, Consequence, Alternative) ->
case Requirement of
false ->
Consequence();
true ->
Alternative()
end.
-file("src/given.gleam", 213).
?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.any_not([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.any_not(are_true_in: [is_admin, is_editor], return: fn() {\n"
" \"At least either Admin or Editor!\"\n"
" })\n"
"\n"
" // …else handle case where user no special role…\n"
" \"Woof!\"\n"
" ```\n"
).
-spec any_not(list(boolean()), fun(() -> FHI), fun(() -> FHI)) -> FHI.
any_not(Requirements, Consequence, Alternative) ->
case begin
_pipe = Requirements,
gleam@list:any(_pipe, fun(V) -> V =:= false end)
end of
true ->
Consequence();
false ->
Alternative()
end.
-file("src/given.gleam", 227).
?DOC(" See `given.any_not()`\n").
-spec not_any(list(boolean()), fun(() -> FHK), fun(() -> FHK)) -> FHK.
not_any(Requirements, Consequence, Alternative) ->
any_not(Requirements, Consequence, Alternative).
-file("src/given.gleam", 267).
?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.all_not([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.all_not(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(() -> FHM), fun(() -> FHM)) -> FHM.
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", 281).
?DOC(" See `given.all_not`.\n").
-spec not_all(list(boolean()), fun(() -> FHO), fun(() -> FHO)) -> FHO.
not_all(Requirements, Consequence, Alternative) ->
all_not(Requirements, Consequence, Alternative).
-file("src/given.gleam", 322).
?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(() -> FHP), fun(() -> FHP)) -> FHP.
'when'(Condition, Alternative, Consequence) ->
case Condition() of
true ->
Consequence();
false ->
Alternative()
end.
-file("src/given.gleam", 362).
?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(() -> FHQ), fun(() -> FHQ)) -> FHQ.
when_not(Condition, Alternative, Consequence) ->
case Condition() of
false ->
Consequence();
true ->
Alternative()
end.
-file("src/given.gleam", 389).
?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(() -> FHT), fun(() -> FHT)) -> FHT.
empty(List, Alternative, Consequence) ->
case List of
[] ->
Consequence();
_ ->
Alternative()
end.
-file("src/given.gleam", 416).
?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(() -> FHW), fun(() -> FHW)) -> FHW.
non_empty(List, Alternative, Consequence) ->
case List of
[] ->
Alternative();
_ ->
Consequence()
end.
-file("src/given.gleam", 428).
-spec not_empty(list(any()), fun(() -> FHZ), fun(() -> FHZ)) -> FHZ.
not_empty(List, Alternative, Consequence) ->
case List of
[] ->
Alternative();
_ ->
Consequence()
end.
-file("src/given.gleam", 466).
?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, FIA} | {error, FIB}, fun((FIB) -> FIE), fun((FIA) -> FIE)) -> FIE.
ok(Rslt, Alternative, Consequence) ->
case Rslt of
{ok, Val} ->
Consequence(Val);
{error, Err} ->
Alternative(Err)
end.
-file("src/given.gleam", 494).
?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, FIF} | {error, FIG}),
fun((list(FIG)) -> FIL),
fun((list(FIF), list(FIG)) -> FIL)
) -> FIL.
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", 524).
?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, FIO} | {error, FIP}),
fun((list(FIO), list(FIP)) -> FIV),
fun((list(FIO)) -> FIV)
) -> FIV.
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", 564).
?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, FIX} | {error, FIY}, fun((FIX) -> FJB), fun((FIY) -> FJB)) -> FJB.
error(Rslt, Alternative, Consequence) ->
case Rslt of
{error, Err} ->
Consequence(Err);
{ok, Val} ->
Alternative(Val)
end.
-file("src/given.gleam", 592).
?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, FJC} | {error, FJD}),
fun((list(FJC)) -> FJI),
fun((list(FJC), list(FJD)) -> FJI)
) -> FJI.
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", 622).
?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, FJL} | {error, FJM}),
fun((list(FJL), list(FJM)) -> FJS),
fun((list(FJM)) -> FJS)
) -> FJS.
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", 664).
?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(FJU), fun(() -> FJW), fun((FJU) -> FJW)) -> FJW.
some(Optn, Alternative, Consequence) ->
case Optn of
{some, Val} ->
Consequence(Val);
none ->
Alternative()
end.
-file("src/given.gleam", 692).
?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(FJX)),
fun((integer()) -> FKA),
fun((list(FJX), integer()) -> FKA)
) -> FKA.
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", 722).
?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(FKC)),
fun((list(FKC), integer()) -> FKG),
fun((list(FKC)) -> FKG)
) -> FKG.
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", 764).
?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(FKI), fun((FKI) -> FKK), fun(() -> FKK)) -> FKK.
none(Optn, Alternative, Consequence) ->
case Optn of
none ->
Consequence();
{some, Val} ->
Alternative(Val)
end.
-file("src/given.gleam", 792).
?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(FKL)),
fun((list(FKL)) -> FKP),
fun((list(FKL), integer()) -> FKP)
) -> FKP.
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", 821).
?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(FKR)),
fun((list(FKR), integer()) -> FKV),
fun(() -> FKV)
) -> FKV.
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.