Current section
Files
Jump to
Current section
Files
src/on.erl
-module(on).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-define(FILEPATH, "src/on.gleam").
-export([ok/2, error/2, error_ok/3, ok_error/3, true/2, false/2, false_true/3, lazy_false_true/3, true_false/3, lazy_true_false/3, some/2, none/2, none_some/3, lazy_none_some/3, some_none/3, nonempty/2, empty/2, empty_nonempty/3, lazy_empty_nonempty/3, nonempty_empty/3, empty_singleton_gt1/4, lazy_empty_singleton_gt1/4, empty_gt1_singleton/4, lazy_empty_gt1_singleton/4, singleton_gt1_empty/4, continue/2]).
-export_type([return/2]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-type return(DTU, DTV) :: {return, DTU} | {continue, DTV}.
-file("src/on.gleam", 28).
?DOC(
" Given a Result(a, b) and a callback f(a) -> Result(c, b) applies\n"
" the callback if the Result has type Ok else maps the Error variant\n"
" of type Result(a, b) to the Error variant of type Result(c, b).\n"
" \n"
" Equivalent to result.try and to error_ok(_, fn(e) -> {Error(e)}, _).\n"
"\n"
" ### Example 1\n"
"\n"
" ```gleam\n"
" use ok_payload <- on.ok(Ok(3))\n"
" // -> execution proceeds, ok_payload == 3; the current scope must\n"
" // return a Result(c, b) for some c, b\n"
" ```\n"
"\n"
" ### Example 2\n"
"\n"
" ```gleam\n"
" use ok_payload <- on.ok(Error(\"Joe\"))\n"
" // -> execution discontinues, scope returns Error(\"Joe\")\n"
" ```\n"
).
-spec ok({ok, DTW} | {error, DTX}, fun((DTW) -> {ok, DUA} | {error, DTX})) -> {ok,
DUA} |
{error, DTX}.
ok(Result, F2) ->
case Result of
{error, E} ->
{error, E};
{ok, A} ->
F2(A)
end.
-file("src/on.gleam", 59).
?DOC(
" Given a Result(a, b) and a callback f(b) -> Result(a, c) applies\n"
" the callback if the Result has type Error else maps the Ok variant\n"
" of type Result(a, b) to the Ok variant of type Result(c, b).\n"
"\n"
" Equivalent to on.ok_error(_, fn(o) -> {Ok(o)}, _).\n"
"\n"
" ### Example 1\n"
"\n"
" ```gleam\n"
" use error_payload <- on.error(Ok(3))\n"
" // -> execution discontinues, scope returns Ok(3)\n"
" ```\n"
"\n"
" ### Example 2\n"
"\n"
" ```gleam\n"
" use error_payload <- on.error(Error(\"Joe\"))\n"
" // -> execution proceeds, error_payload == \"Joe\";\n"
" // the scope must return a Result(a, c)\n"
" ```\n"
).
-spec error({ok, DUF} | {error, DUG}, fun((DUG) -> {ok, DUF} | {error, DUJ})) -> {ok,
DUF} |
{error, DUJ}.
error(Result, F2) ->
case Result of
{ok, A} ->
{ok, A};
{error, E} ->
F2(E)
end.
-file("src/on.gleam", 99).
?DOC(
" Given a Result(a, b) and callbacks f(b) -> c, f(a) -> c \n"
" returns the evaluation of the first callback at b1 if the\n"
" Result is Error(b1) and the evaluation of the second callback\n"
" at a1 if the Result is Ok(a1).\n"
"\n"
" ### Example 1\n"
" \n"
" ```gleam\n"
" use ok_payload <- on.error_ok(\n"
" Ok(3),\n"
" on_error: fn(e) { \"hi \" <> e <> \"!\" },\n"
" )\n"
" // -> execution proceeds, ok_payload == 3; the scope must return\n"
" // a String to match the return value of the on_error callback\n"
" ```\n"
" \n"
" ### Example 2\n"
" \n"
" ```gleam\n"
" use ok_payload <- on.error_ok(\n"
" Error(\"Joe\"),\n"
" on_error: fn(e) { \"hi \" <> e <> \"!\" },\n"
" )\n"
" // -> execution discontinues, scope returns \"hi Joe!\"\n"
" ```\n"
).
-spec error_ok({ok, DUO} | {error, DUP}, fun((DUP) -> DUS), fun((DUO) -> DUS)) -> DUS.
error_ok(Result, F1, F2) ->
case Result of
{error, B} ->
F1(B);
{ok, A} ->
F2(A)
end.
-file("src/on.gleam", 137).
?DOC(
" Given a Result(a, b) and callbacks f(b) -> c, f(a) -> c \n"
" returns the evaluation of the first callback at a1 if the\n"
" Result is Ok(a1) and the evaluation of the second callback\n"
" at b1 if the Result is Error(b1).\n"
"\n"
" ### Example 1\n"
"\n"
" ```gleam\n"
" use error_payload <- on.ok_error(\n"
" Ok(3),\n"
" on_ok: fn(x) { x + 1 },\n"
" )\n"
" // -> execution discontinues, scope returns 4\n"
" ```\n"
"\n"
" ### Example 2\n"
" \n"
" ```gleam\n"
" use error_payload <- on.ok_error(\n"
" Error(\"Joe\"),\n"
" on_ok: fn(x) { x + 1 },\n"
" )\n"
" // -> execution proceeds, error_payload == \"Joe\"; the scope\n"
" // must return an Int to match the return value of the on_ok\n"
" // callback\n"
" ```\n"
).
-spec ok_error({ok, DUT} | {error, DUU}, fun((DUT) -> DUX), fun((DUU) -> DUX)) -> DUX.
ok_error(Result, F1, F2) ->
case Result of
{ok, A} ->
F1(A);
{error, B} ->
F2(B)
end.
-file("src/on.gleam", 171).
?DOC(
" Given a Bool returns False if the bool is False, else\n"
" returns a lazily evaluated callback.\n"
"\n"
" Equivalent to on.false_true(_, False, _).\n"
"\n"
" ### Example 1\n"
"\n"
" ```gleam\n"
" use <- on.true(True)\n"
" // -> execution proceeds, scope must return a Bool\n"
" ```\n"
"\n"
" ### Example 2\n"
"\n"
" ```gleam\n"
" use <- on.true(False)\n"
" // -> execution discontinues, scope returns False\n"
" ```\n"
).
-spec true(boolean(), fun(() -> boolean())) -> boolean().
true(Bool, F2) ->
case Bool of
false ->
false;
true ->
F2()
end.
-file("src/on.gleam", 198).
?DOC(
" Given a Bool returns True if the bool is True, else\n"
" returns a lazily evaluated callback.\n"
"\n"
" ### Example 1\n"
"\n"
" ```gleam\n"
" use <- on.false(True)\n"
" // -> execution discontinues, scope returns True\n"
" ```\n"
"\n"
" ### Example 2\n"
"\n"
" ```gleam\n"
" use <- on.false(False)\n"
" // -> execution proceeds, scope must return a Bool\n"
" ```\n"
).
-spec false(boolean(), fun(() -> boolean())) -> boolean().
false(Bool, F2) ->
case Bool of
true ->
true;
false ->
F2()
end.
-file("src/on.gleam", 237).
?DOC(
" Given a Bool, a value of type c and a callback f() -> c,\n"
" returns the value of type c if the bool is False, else the\n"
" evaluation of the callback.\n"
"\n"
" ### Example 1\n"
"\n"
" ```gleam\n"
" use <- on.false_true(\n"
" True,\n"
" on_false: \"Joe\",\n"
" )\n"
" // -> execution proceeds, the scope must return a String to \n"
" // match the return value of the on_false argument\n"
" ```\n"
"\n"
" ### Example 2\n"
"\n"
" ```gleam\n"
" use <- on.false_true(\n"
" False,\n"
" on_false: \"Joe\",\n"
" )\n"
" // -> execution discontinues, scope returns \"Joe\"\n"
" ```\n"
).
-spec false_true(boolean(), DUY, fun(() -> DUY)) -> DUY.
false_true(Bool, C, F2) ->
case Bool of
false ->
C;
true ->
F2()
end.
-file("src/on.gleam", 273).
?DOC(
" Given a Bool and two callbacks f() -> c returns the\n"
" evaluation of the first callback if the bool is False,\n"
" else the second callback.\n"
"\n"
" ### Example 1\n"
"\n"
" ```gleam\n"
" use <- on.lazy_false_true(\n"
" True,\n"
" on_false: fn() { \"Joe\" },\n"
" )\n"
" // -> execution proceeds, the scope must return a String \n"
" // to match the return value of the on_false argument\n"
" ```\n"
"\n"
" ### Example 2\n"
"\n"
" ```gleam\n"
" use <- on.lazy_false_true(\n"
" False,\n"
" on_false: fn() { \"Joe\" },\n"
" )\n"
" // -> execution discontinues, scope returns \"Joe\"\n"
" ```\n"
).
-spec lazy_false_true(boolean(), fun(() -> DUZ), fun(() -> DUZ)) -> DUZ.
lazy_false_true(Bool, F1, F2) ->
case Bool of
false ->
F1();
true ->
F2()
end.
-file("src/on.gleam", 309).
?DOC(
" Given a Bool, a value of type c and a callback f() -> c, \n"
" returns the value of type c if the bool is True, else the\n"
" evaluation of the callback.\n"
"\n"
" ### Example 1\n"
"\n"
" ```gleam\n"
" use <- on.true_false(\n"
" True,\n"
" on_true: \"Joe\",\n"
" )\n"
" // -> execution discontinues, scope returns \"Joe\"\n"
" ```\n"
"\n"
" ### Example 2\n"
" \n"
" ```gleam\n"
" use <- on.true_false(\n"
" False,\n"
" on_true: \"Joe\",\n"
" )\n"
" // -> execution proceeds, the scope must return a String to \n"
" // match the return value of the on_true argument\n"
" ```\n"
).
-spec true_false(boolean(), DVA, fun(() -> DVA)) -> DVA.
true_false(Bool, C, F2) ->
case Bool of
true ->
C;
false ->
F2()
end.
-file("src/on.gleam", 346).
?DOC(
" Given a Bool and two callbacks f() -> c returns the\n"
" evaluation of the first callback if the bool is True,\n"
" else the second callback.\n"
"\n"
"\n"
" ### Example 1\n"
"\n"
" ```gleam\n"
" use <- on.lazy_true_false(\n"
" True,\n"
" on_true: fn() { \"Joe\" },\n"
" )\n"
" // -> execution discontinues, scope returns \"Joe\"\n"
" ```\n"
"\n"
" ### Example 2\n"
"\n"
" ```gleam\n"
" use <- on.lazy_true_false(\n"
" False,\n"
" on_true: fn() { \"Joe\" },\n"
" )\n"
" // -> execution proceeds, the scope must return a String to \n"
" // match the return value of the on_false argument\n"
" ```\n"
).
-spec lazy_true_false(boolean(), fun(() -> DVB), fun(() -> DVB)) -> DVB.
lazy_true_false(Bool, F1, F2) ->
case Bool of
true ->
F1();
false ->
F2()
end.
-file("src/on.gleam", 384).
?DOC(
" Given an Option(a) and a callback f(a) -> Option(c)\n"
" applies the callback to the payload a if the option is Some(a),\n"
" else returns None.\n"
"\n"
" Equivalent to:\n"
" - on.none_some(_, None, _).\n"
" - option.then\n"
" \n"
" ### Example 1\n"
"\n"
" ```gleam\n"
" use payload <- on.some(Some(3))\n"
" // -> execution proceeds, payload == 3; the scope must return\n"
" // an Option(c)\n"
" ```\n"
"\n"
" ### Example 2\n"
" \n"
" ```gleam\n"
" use payload <- on.some(None)\n"
" // -> execution discontinues, scope returns None\n"
" ```\n"
).
-spec some(gleam@option:option(DVC), fun((DVC) -> gleam@option:option(DVE))) -> gleam@option:option(DVE).
some(Option, F2) ->
case Option of
none ->
none;
{some, A} ->
F2(A)
end.
-file("src/on.gleam", 412).
?DOC(
" Given an Option(a) and a callback f() -> Option(a), applies\n"
" the callback if the option is None, else returns the option\n"
" unchanged.\n"
"\n"
" ### Example 1\n"
"\n"
" ```gleam\n"
" use <- on.none(Some(3))\n"
" // -> execution discontinues, scope returns Some(3)\n"
" ```\n"
"\n"
" ### Example 2\n"
"\n"
" ```gleam\n"
" use <- on.none(None)\n"
" // -> execution proceeds, the scope must return an Option(a)\n"
" ```\n"
).
-spec none(gleam@option:option(DVH), fun(() -> gleam@option:option(DVH))) -> gleam@option:option(DVH).
none(Option, F2) ->
case Option of
{some, A} ->
{some, A};
none ->
F2()
end.
-file("src/on.gleam", 449).
?DOC(
" Given an Option(a), a value of type c and a callback f(a) -> c, \n"
" returns the value of type c if the option is None, else\n"
" applies the callback to the option's payload.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" use payload <- on.none_some(\n"
" Some(3),\n"
" on_none: \"Bob\",\n"
" )\n"
" // -> execution proceeds, payload == 3; the scope must return a\n"
" // String to match the return value of the on_none return value\n"
" ```\n"
"\n"
" ```gleam\n"
" use payload <- on.none_some(\n"
" None,\n"
" on_none: \"Bob\",\n"
" )\n"
" // -> execution discontinues, scope returns \"Bob\"\n"
" ```\n"
).
-spec none_some(gleam@option:option(DVL), DVN, fun((DVL) -> DVN)) -> DVN.
none_some(Option, C, F2) ->
case Option of
none ->
C;
{some, A} ->
F2(A)
end.
-file("src/on.gleam", 487).
?DOC(
" Given an Option(a), a callback f() -> c and a callback\n"
" f(a) -> c evaluates the first callback if the option is\n"
" None, else evaluates the second callback on the option's\n"
" payload.\n"
"\n"
" ### Example 1\n"
"\n"
" ```gleam\n"
" use payload <- on.lazy_none_some(\n"
" Some(3),\n"
" on_none: fn() { \"Bob\" },\n"
" )\n"
" // -> execution proceeds, payload == 3; the scope must return\n"
" // a String to match the return value of the on_none return\n"
" // value\n"
" ```\n"
"\n"
" ### Example 2\n"
" \n"
" ```gleam\n"
" use payload <- on.lazy_none_some(\n"
" None,\n"
" on_none: fn() { \"Bob\" },\n"
" )\n"
" // -> execution discontinues, scope returns \"Bob\"\n"
" ```\n"
).
-spec lazy_none_some(
gleam@option:option(DVO),
fun(() -> DVQ),
fun((DVO) -> DVQ)
) -> DVQ.
lazy_none_some(Option, F1, F2) ->
case Option of
none ->
F1();
{some, A} ->
F2(A)
end.
-file("src/on.gleam", 524).
?DOC(
" Given an Option(a), a callback f(a) -> c and a callback\n"
" f() -> c, evaluates the first callback on the option's\n"
" payload, else evaluates the second callback if the option\n"
" is None.\n"
"\n"
" ### Example 1\n"
"\n"
" ```gleam\n"
" use <- on.some_none(\n"
" Some(3),\n"
" on_some: fn(x) { x + 1 },\n"
" )\n"
" // -> execution discontinues, scope returns 4\n"
" ```\n"
"\n"
" ### Example 2\n"
"\n"
" ```gleam\n"
" use <- on.some_none(\n"
" None,\n"
" on_some: fn(x) { x + 1 },\n"
" )\n"
" // -> execution proceeds, the scope must return an Int to match\n"
" // the return value of the on_some return value\n"
" ```\n"
).
-spec some_none(gleam@option:option(DVR), fun((DVR) -> DVT), fun(() -> DVT)) -> DVT.
some_none(Option, F1, F2) ->
case Option of
{some, A} ->
F1(A);
none ->
F2()
end.
-file("src/on.gleam", 560).
?DOC(
" Given a List(a) and a callback\n"
" f(a, List(a)) -> List(c), returns the empty list if the\n"
" list is empty, else evaluates the callback on (first, rest)\n"
" where 'first' is the first element and 'tail' is the tail of \n"
" the list.\n"
" \n"
" ### Example 1\n"
" \n"
" ```gleam\n"
" use first, rest <- on.nonempty([1, 4, 7])\n"
" // -> execution proceeds, first == 1, rest == [1, 7];\n"
" // scope must return a List(c)\n"
" ```\n"
" \n"
" ### Example 2\n"
" \n"
" ```gleam\n"
" use first, rest <- on.nonempty([])\n"
" // -> execution discontinues, scope returns []\n"
" ```\n"
).
-spec nonempty(list(DVU), fun((DVU, list(DVU)) -> list(DVX))) -> list(DVX).
nonempty(List, F2) ->
case List of
[] ->
[];
[First | Rest] ->
F2(First, Rest)
end.
-file("src/on.gleam", 588).
?DOC(
" Given a List(a) and a callback f() -> List(a)\n"
" returns the list if the is nonempty, else evaluates\n"
" the callback.\n"
"\n"
" ### Example 1\n"
" \n"
" ```gleam\n"
" use <- on.empty([1, 4, 7])\n"
" // -> execution discontinues, scope returns [1, 4, 7]\n"
" ```\n"
" \n"
" ### Example 2\n"
" \n"
" ```gleam\n"
" use <- on.empty([])\n"
" // -> execution proceeds, the scope must return a List(c)\n"
" ```\n"
).
-spec empty(list(DWA), fun(() -> list(DWA))) -> list(DWA).
empty(List, F2) ->
case List of
[_ | _] ->
List;
[] ->
F2()
end.
-file("src/on.gleam", 629).
?DOC(
" Given a List(a), a value of type c, and callback\n"
" f(a, List(a)) -> c, returns either the value of type c\n"
" if the list is empty or else applies the callback to \n"
" the head: a and tail: List(a) of the list.\n"
" \n"
" ### Example 1\n"
" \n"
" ```gleam\n"
" use first, rest <- on.empty_nonempty(\n"
" [1, 4, 7],\n"
" on_empty: \"Joe\",\n"
" )\n"
" // -> execution proceeds, first == 1, rest == [4, 7];\n"
" // scope must return a String to match the empty list\n"
" // return value\n"
" ```\n"
" \n"
" ### Example 2\n"
" \n"
" ```gleam\n"
" use first, rest <- on.empty_nonempty(\n"
" [],\n"
" on_empty: \"Joe\",\n"
" )\n"
" // -> execution discontinues, scope returns \"Joe\"\n"
" ```\n"
).
-spec empty_nonempty(list(DWE), DWG, fun((DWE, list(DWE)) -> DWG)) -> DWG.
empty_nonempty(List, C, F2) ->
case List of
[] ->
C;
[First | Rest] ->
F2(First, Rest)
end.
-file("src/on.gleam", 667).
?DOC(
" Given a List(a), a callback fn() -> c and a callback\n"
" f(a, List(a)) -> c, evaluates the first callback if the\n"
" list is empty else applies the second callback to the\n"
" head: a and tail: List(a) of the list.\n"
" \n"
" ### Example 1\n"
" \n"
" ```gleam\n"
" use first, rest <- on.lazy_empty_nonempty(\n"
" [1, 4, 7],\n"
" on_empty: fn() { \"Joe\" },\n"
" )\n"
" // -> execution proceeds, first == 1, rest == [4, 7];\n"
" // scope must return a String to match the empty list\n"
" // callback return value\n"
" ```\n"
" \n"
" ### Example 2\n"
" \n"
" ```gleam\n"
" use first, rest <- on.lazy_empty_nonempty(\n"
" [],\n"
" on_empty: fn() { \"Joe\" },\n"
" )\n"
" // -> execution discontinues, scope returns \"Joe\"\n"
" ```\n"
).
-spec lazy_empty_nonempty(
list(DWI),
fun(() -> DWK),
fun((DWI, list(DWI)) -> DWK)
) -> DWK.
lazy_empty_nonempty(List, F1, F2) ->
case List of
[] ->
F1();
[First | Rest] ->
F2(First, Rest)
end.
-file("src/on.gleam", 706).
?DOC(
" Given a List(a), a callback f(a, List(a)) -> c, and\n"
" a callback f() -> c, returns either the first callback\n"
" evaluated on the first element and tail of the list if\n"
" the list is nonempty, and otherwise evaluates the \n"
" second callback, if the list is empty. \n"
" \n"
" ### Example 1\n"
" \n"
" ```gleam\n"
" use <- on.nonempty_empty(\n"
" [1, 4, 7],\n"
" fn(first, _rest) { first + 1 },\n"
" )\n"
" // -> execution discontinues, scope returns 2\n"
" ```\n"
" \n"
" ### Example 2\n"
" \n"
" ```gleam\n"
" use <- on.nonempty_empty(\n"
" [],\n"
" fn(first, _rest) { first + 1 },\n"
" )\n"
" // -> execution proceeds, the scope must return an\n"
" // integer to match the return value of the first\n"
" // callback\n"
" ```\n"
).
-spec nonempty_empty(list(DWM), fun((DWM, list(DWM)) -> DWP), fun(() -> DWP)) -> DWP.
nonempty_empty(List, F1, F2) ->
case List of
[First | Rest] ->
F1(First, Rest);
[] ->
F2()
end.
-file("src/on.gleam", 751).
?DOC(
" Given a List(a), a value of type c, and callbacks\n"
" f(a) -> c, f(a, a, List(a)) -> c, returns:\n"
"\n"
" - the value of type c if the list is empty\n"
" - the first callback evaluated with argument a1 if the list as the form [a1]\n"
" - the second callback evaluated with arguments a1, a2, rest if the list has the form [a1, a2, ..rest]\n"
" \n"
" ### Example 1\n"
" \n"
" ```gleam\n"
" use first, second, ..rest <- on.empty_singleton_gt1(\n"
" [1, 4, 7],\n"
" on_empty: 0,\n"
" on_singleton: fn(first) { first },\n"
" )\n"
" // -> execution proceeds, first == 1, second == 4, rest == [7];\n"
" // scope must return an Int to match the on_empty, on_singleton callbacks\n"
" ```\n"
" \n"
" ### Example 2\n"
" \n"
" ```gleam\n"
" use first, second, ..rest <- on.empty_singleton_gt1(\n"
" [4],\n"
" on_empty: 0,\n"
" on_singleton: fn(first) { first },\n"
" )\n"
" // -> execution discontinues, scope resturns 4\n"
" ```\n"
).
-spec empty_singleton_gt1(
list(DWQ),
DWS,
fun((DWQ) -> DWS),
fun((DWQ, DWQ, list(DWQ)) -> DWS)
) -> DWS.
empty_singleton_gt1(List, C, F2, F3) ->
case List of
[] ->
C;
[First] ->
F2(First);
[First@1, Second | Rest] ->
F3(First@1, Second, Rest)
end.
-file("src/on.gleam", 794).
?DOC(
" Given a List(a), and callbacks f() -> c, f(a) -> c,\n"
" f(a, a, List(a)) -> c, returns:\n"
"\n"
" - the evaluation of the first callback if the list is empty\n"
" - the second callback evaluated with argument a1 if the list as the form [a1]\n"
" - the third callback evaluated with arguments a1, a2, rest if the list has the form [a1, a2, ..rest]\n"
" \n"
" ### Example 1\n"
" \n"
" ```gleam\n"
" use first, second, ..rest <- on.lazy_empty_singleton_gt1(\n"
" [1, 4, 7],\n"
" on_empty: 0,\n"
" on_singleton: fn(first) { first },\n"
" )\n"
" // -> execution proceeds, first == 1, second == 4, rest == [7];\n"
" // scope must return an Int to match the on_empty, on_singleton callbacks\n"
" ```\n"
" \n"
" ### Example 2\n"
" \n"
" ```gleam\n"
" use first, second, ..rest <- on.lazy_empty_singleton_gt1(\n"
" [4],\n"
" on_empty: 0,\n"
" on_singleton: fn(first) { first },\n"
" )\n"
" // -> execution discontinues, scope resturns 4\n"
" ```\n"
).
-spec lazy_empty_singleton_gt1(
list(DWU),
fun(() -> DWW),
fun((DWU) -> DWW),
fun((DWU, DWU, list(DWU)) -> DWW)
) -> DWW.
lazy_empty_singleton_gt1(List, F1, F2, F3) ->
case List of
[] ->
F1();
[First] ->
F2(First);
[First@1, Second | Rest] ->
F3(First@1, Second, Rest)
end.
-file("src/on.gleam", 838).
?DOC(
" Given a List(a), a value of type c, and callbacks\n"
" f(a, a, List(a)) -> c, f(a) -> c, returns:\n"
"\n"
" - the value of type c if the list is empty\n"
" - the first callback evaluated with arguments a1, a2, rest if the list has the form [a1, a2, ..rest]\n"
" - the second callback evaluated with argument a1 if the list as the form [a1]\n"
" \n"
" ### Example 1\n"
" \n"
" ```gleam\n"
" use first <- on.empty_gt1_singleton(\n"
" [1, 4, 7],\n"
" on_empty: Error(\"empty list\"),\n"
" on_gt1: Error(\"> 1 element in list\"),\n"
" )\n"
" // -> execution discontinues, scope returns Error(\"> 1 element in list\")\n"
" ```\n"
" \n"
" ### Example 2\n"
" \n"
" ```gleam\n"
" use first <- on.empty_gt1_singleton(\n"
" [4],\n"
" on_empty: Error(\"empty list\"),\n"
" on_gt1: Error(\"> 1 element in list\"),\n"
" )\n"
" // -> execution proceeds, first == 4;\n"
" // scope must return a Result(c, String) to match the on_empty \n"
" // and on_gt1 callbacks\n"
" ```\n"
).
-spec empty_gt1_singleton(
list(DWY),
DXA,
fun((DWY, DWY, list(DWY)) -> DXA),
fun((DWY) -> DXA)
) -> DXA.
empty_gt1_singleton(List, C, F2, F3) ->
case List of
[] ->
C;
[First, Second | Rest] ->
F2(First, Second, Rest);
[First@1] ->
F3(First@1)
end.
-file("src/on.gleam", 882).
?DOC(
" Given a List(a) and callbacks f() -> c,\n"
" f(a, a, List(a)) -> c, f(a) -> c, returns:\n"
"\n"
" - the evaluation of the first callback if the list is empty\n"
" - the second callback evaluated with arguments a1, a2, rest if the list has the form [a1, a2, ..rest]\n"
" - the third callback evaluated with argument a1 if the list as the form [a1]\n"
" \n"
" ### Example 1\n"
" \n"
" ```gleam\n"
" use first <- on.lazy_empty_gt1_singleton(\n"
" [1, 4, 7],\n"
" on_empty: Error(\"empty list\"),\n"
" on_gt1: fn(_, _, _) {Error(\"> 1 element in list\")},\n"
" )\n"
" // -> execution discontinues, scope returns Error(\"> 1 element in list\")\n"
" ```\n"
" \n"
" ### Example 2\n"
" \n"
" ```gleam\n"
" use first <- on.lazy_empty_gt1_singleton(\n"
" [4],\n"
" on_empty: Error(\"empty list\"),\n"
" on_gt1: Error(\"> 1 element in list\"),\n"
" )\n"
" // -> execution proceeds, first == 4;\n"
" // scope must return a Result(c, String) to match the on_empty \n"
" // and on_gt1 callbacks\n"
" ```\n"
).
-spec lazy_empty_gt1_singleton(
list(DXC),
fun(() -> DXE),
fun((DXC, DXC, list(DXC)) -> DXE),
fun((DXC) -> DXE)
) -> DXE.
lazy_empty_gt1_singleton(List, F1, F2, F3) ->
case List of
[] ->
F1();
[First, Second | Rest] ->
F2(First, Second, Rest);
[First@1] ->
F3(First@1)
end.
-file("src/on.gleam", 926).
?DOC(
" Given a List(a) and callbacks f(a) -> c,\n"
" f(a, a, List(a)) -> c, f() -> c, returns:\n"
"\n"
" - the first callback evaluated with argument a1 if the list as the form [a1]\n"
" - the second callback evaluated with arguments a1, a2, rest if the list has the form [a1, a2, ..rest]\n"
" - the evaluation of the third callback if the list is empty\n"
" \n"
" ### Example 1\n"
" \n"
" ```gleam\n"
" use <- on.singleton_gt1_empty(\n"
" [1, 4, 7],\n"
" on_singleton: fn(x) { x + 1 },\n"
" on_gt1: fn(first, second, ..rest) { first + second},\n"
" )\n"
" // -> execution discontinues, scope returns 5 (= 1 + 4)\n"
" ```\n"
" \n"
" ### Example 2\n"
" \n"
" ```gleam\n"
" use first, second, ..rest <- on.singleton_gt1_empty(\n"
" [],\n"
" on_singleton: fn(x) { x + 1 },\n"
" on_gt1: fn(first, second, ..rest) { first + second},\n"
" )\n"
" // -> execution proceeds, scope must return an Int to \n"
" // match the on_singleton, on_gt1 callbacks\n"
" \n"
" ```\n"
).
-spec singleton_gt1_empty(
list(DXG),
fun((DXG) -> DXI),
fun((DXG, DXG, list(DXG)) -> DXI),
fun(() -> DXI)
) -> DXI.
singleton_gt1_empty(List, F1, F2, F3) ->
case List of
[First] ->
F1(First);
[First@1, Second | Rest] ->
F2(First@1, Second, Rest);
[] ->
F3()
end.
-file("src/on.gleam", 977).
?DOC(
" Given a value of type Return(a, b) and a callback f(b) -> a, returns\n"
" f(b1) if the value has the form Continue(b1) and returns a1\n"
" if the value has the form Return(a1).\n"
" \n"
" ### Example 1\n"
"\n"
" ```gleam\n"
" let #(string1, string2) = #(\"bob\", \"\")\n"
" use _ <- on.continue(case string {\n"
" \"\" -> Return(#(string1, string1))\n"
" _ -> Continue(Nil)\n"
" })\n"
" // -> execution discontinues, scope returns #(\"bob\", \"bob\")\n"
" ```\n"
"\n"
" ### Example 2\n"
"\n"
" ```gleam\n"
" let #(string1, string2) = #(\"bob\", \"alice\")\n"
" use _ <- on.continue(case string {\n"
" \"\" -> Return(#(string1, string1))\n"
" _ -> Continue(Nil)\n"
" })\n"
" // -> execution proceeds; the current scope must return a\n"
" // #(String, String)\n"
" ```\n"
).
-spec continue(return(DXK, DXL), fun((DXL) -> DXK)) -> DXK.
continue(R, F1) ->
case R of
{return, A} ->
A;
{continue, B} ->
F1(B)
end.