-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, 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(() -> FHB), fun(() -> FHB)) -> FHB. 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(() -> FHD), fun(() -> FHD)) -> FHD. 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(() -> FHF), fun(() -> FHF)) -> FHF. 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(() -> FHG), fun(() -> FHG)) -> FHG. '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(() -> FHI), fun(() -> FHI)) -> FHI. 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(() -> FHK), fun(() -> FHK)) -> FHK. 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(() -> FHL), fun(() -> FHL)) -> FHL. '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(() -> FHM), fun(() -> FHM)) -> FHM. when_not(Condition, Alternative, Consequence) -> case Condition() =:= false of true -> Consequence(); false -> Alternative() end. -file("src/given.gleam", 249). ?DOC(" ```\n"). -spec empty(list(any()), fun(() -> FHP), fun(() -> FHP)) -> FHP. 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(() -> FHS), fun(() -> FHS)) -> FHS. 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, FHT} | {error, FHU}, fun((FHU) -> FHX), fun((FHT) -> FHX)) -> FHX. 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, FHY} | {error, FHZ}), fun((list(FHZ)) -> FIE), fun((list(FHY), list(FHZ)) -> FIE) ) -> FIE. 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, FIH} | {error, FII}), fun((list(FIH), list(FII)) -> FIO), fun((list(FIH)) -> FIO) ) -> FIO. 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, FIQ} | {error, FIR}, fun((FIQ) -> FIU), fun((FIR) -> FIU)) -> FIU. 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, FIV} | {error, FIW}), fun((list(FIV)) -> FJB), fun((list(FIV), list(FIW)) -> FJB) ) -> FJB. 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, FJE} | {error, FJF}), fun((list(FJE), list(FJF)) -> FJL), fun((list(FJF)) -> FJL) ) -> FJL. 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(FJN), fun(() -> FJP), fun((FJN) -> FJP)) -> FJP. 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(FJQ)), fun((integer()) -> FJT), fun((list(FJQ), integer()) -> FJT) ) -> FJT. 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(FJV)), fun((list(FJV), integer()) -> FJZ), fun((list(FJV)) -> FJZ) ) -> FJZ. 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(FKB), fun((FKB) -> FKD), fun(() -> FKD)) -> FKD. 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(FKE)), fun((list(FKE)) -> FKI), fun((list(FKE), integer()) -> FKI) ) -> FKI. 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(FKK)), fun((list(FKK), integer()) -> FKO), fun(() -> FKO) ) -> FKO. 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.