Current section

Files

Jump to
decepticon src decepticon.erl
Raw

src/decepticon.erl

-module(decepticon).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([eval/2, exec/2, 'try'/1, action/1, error/1, ok_state/1, error_state/1, get/0, put/1, map/2, apply/2, map_error/2, do/2]).
-export_type([state_result/3]).
-type state_result(FND, FNE, FNF) :: {state_result,
decepticon@stateful:state({ok, FND} | {error, FNF}, FNE)}.
-spec eval(state_result(FNG, FNH, FNI), FNH) -> {ok, FNG} | {error, FNI}.
eval(State_result, S) ->
decepticon@stateful:eval(erlang:element(2, State_result), S).
-spec exec(state_result(any(), FNP, any()), FNP) -> FNP.
exec(State_result, S) ->
decepticon@stateful:exec(erlang:element(2, State_result), S).
-spec 'try'({ok, FNU} | {error, FNV}) -> state_result(FNU, any(), FNV).
'try'(Result_value) ->
{state_result, decepticon@stateful:action(Result_value)}.
-spec action(FNZ) -> state_result(FNZ, any(), any()).
action(Action_value) ->
{state_result, decepticon@stateful:action({ok, Action_value})}.
-spec error(FOB) -> state_result(any(), any(), FOB).
error(Error) ->
{state_result, decepticon@stateful:action({error, Error})}.
-spec ok_state(decepticon@stateful:state(FOD, FOE)) -> state_result(FOD, FOE, any()).
ok_state(State) ->
{state_result, decepticon@stateful:map(State, fun(A) -> {ok, A} end)}.
-spec error_state(decepticon@stateful:state(FOL, FOM)) -> state_result(any(), FOM, FOL).
error_state(State) ->
{state_result, decepticon@stateful:map(State, fun(Err) -> {error, Err} end)}.
-spec get() -> state_result(FOT, FOT, any()).
get() ->
ok_state(decepticon@stateful:get()).
-spec put(FOY) -> state_result(nil, FOY, any()).
put(State_value) ->
ok_state(decepticon@stateful:put(State_value)).
-spec map(state_result(FPD, FPE, FPF), fun((FPD) -> FPJ)) -> state_result(FPJ, FPE, FPF).
map(State_result, Map_fn) ->
{state_result,
decepticon@stateful:map(
erlang:element(2, State_result),
fun(A_result) -> gleam@result:map(A_result, Map_fn) end
)}.
-spec apply(
state_result(fun((FPN) -> FPO), FPP, FPQ),
state_result(FPN, FPP, FPQ)
) -> state_result(FPO, FPP, FPQ).
apply(Prev, Next) ->
Combined = decepticon@stateful:do(
erlang:element(2, Prev),
fun(Result_fn) -> case Result_fn of
{ok, Map_fn} ->
decepticon@stateful:map(
erlang:element(2, Next),
fun(Arg) -> gleam@result:map(Arg, Map_fn) end
);
{error, Err} ->
decepticon@stateful:action({error, Err})
end end
),
{state_result, Combined}.
-spec map_error(state_result(FQA, FQB, FQC), fun((FQC) -> FQG)) -> state_result(FQA, FQB, FQG).
map_error(State_result, Map_error_fn) ->
{state_result,
decepticon@stateful:map(
erlang:element(2, State_result),
fun(A_result) -> gleam@result:map_error(A_result, Map_error_fn) end
)}.
-spec do(state_result(FQK, FQL, FQM), fun((FQK) -> state_result(FQQ, FQL, FQM))) -> state_result(FQQ, FQL, FQM).
do(State_result, And_then_fn) ->
{state_result,
decepticon@stateful:do(
erlang:element(2, State_result),
fun(A_result) -> case A_result of
{ok, A} ->
erlang:element(2, And_then_fn(A));
{error, Err} ->
decepticon@stateful:action({error, Err})
end end
)}.