Current section

Files

Jump to
lustre src lustre@cmd.erl
Raw

src/lustre@cmd.erl

-module(lustre@cmd).
-compile(no_auto_import).
-export([from/1, none/0, batch/1, map/2, to_list/1]).
-export_type([cmd/1]).
-opaque cmd(EIN) :: {cmd, fun((fun((EIN) -> nil)) -> nil), cmd(EIN)} | none.
-spec from(fun((fun((EIO) -> nil)) -> nil)) -> cmd(EIO).
from(Cmd) ->
{cmd, Cmd, none}.
-spec none() -> cmd(any()).
none() ->
none.
-spec batch(list(cmd(EIS))) -> cmd(EIS).
batch(Cmds) ->
_pipe = Cmds,
_pipe@1 = gleam@list:flat_map(_pipe, fun to_list/1),
gleam@list:fold_right(_pipe@1, none, fun(Rest, Cmd) -> {cmd, Cmd, Rest} end).
-spec map(cmd(EIW), fun((EIW) -> EIY)) -> cmd(EIY).
map(Cmd, F) ->
case Cmd of
{cmd, Cmd@1, Next} ->
{cmd,
fun(Dispatch) -> Cmd@1(fun(A) -> Dispatch(F(A)) end) end,
map(Next, F)};
none ->
none
end.
-spec to_list(cmd(EJA)) -> list(fun((fun((EJA) -> nil)) -> nil)).
to_list(Cmd) ->
case Cmd of
{cmd, Cmd@1, Next} ->
[Cmd@1 | to_list(Next)];
none ->
[]
end.