-module(lustre@cmd). -compile([no_auto_import, nowarn_unused_vars]). -export([from/1, none/0, to_list/1, batch/1, map/2]). -export_type([cmd/1]). -opaque cmd(GLP) :: {cmd, fun((fun((GLP) -> nil)) -> nil), cmd(GLP)} | none. -spec from(fun((fun((GLQ) -> nil)) -> nil)) -> cmd(GLQ). from(Cmd) -> {cmd, Cmd, none}. -spec none() -> cmd(any()). none() -> none. -spec to_list(cmd(GMC)) -> list(fun((fun((GMC) -> nil)) -> nil)). to_list(Cmd) -> case Cmd of {cmd, Cmd@1, Next} -> [Cmd@1 | to_list(Next)]; none -> [] end. -spec batch(list(cmd(GLU))) -> cmd(GLU). 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(GLY), fun((GLY) -> GMA)) -> cmd(GMA). 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.