Current section

Files

Jump to
gleamy_bench src test.erl
Raw

src/test.erl

-module(test).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function]).
-export([yield/2, mprompt/2, prompt/2, main/0]).
-export_type([ctrl/1]).
-type ctrl(FLE) :: {pure, FLE} |
{yield,
binary(),
fun((fun((FLE) -> ctrl(FLE))) -> ctrl(FLE)),
fun((FLE) -> ctrl(FLE))}.
-spec yield(binary(), fun((fun((FMB) -> ctrl(FMB))) -> ctrl(FMB))) -> ctrl(FMB).
yield(Marker, Op) ->
{yield, Marker, Op, fun(Field@0) -> {pure, Field@0} end}.
-spec mprompt(binary(), ctrl(FMG)) -> ctrl(FMG).
mprompt(Marker, Ctl) ->
case Ctl of
{pure, X} ->
{pure, X};
{yield, M, Op, Cont} ->
Cont@1 = fun(X@1) -> mprompt(Marker, Cont(X@1)) end,
case Marker =:= M of
true ->
Op(Cont@1);
false ->
{yield, M, Op, Cont@1}
end
end.
-spec prompt(binary(), fun(() -> ctrl(FMJ))) -> ctrl(FMJ).
prompt(Marker, Action) ->
mprompt(Marker, Action()).
-spec bind(ctrl(FLK), fun((FLK) -> ctrl(FLK))) -> ctrl(FLK).
bind(Ctl, F) ->
case Ctl of
{pure, X} ->
F(X);
{yield, M, Op, Cont} ->
{yield, M, Op, kcompose(F, Cont)}
end.
-spec kcompose(fun((FLK) -> ctrl(FLK)), fun((FLK) -> ctrl(FLK))) -> fun((FLK) -> ctrl(FLK)).
kcompose(G, F) ->
fun(X) -> _pipe = F(X),
bind(_pipe, G) end.
-spec run() -> ctrl(integer()).
run() ->
prompt(
<<"read"/utf8>>,
fun() ->
bind(
{pure, 5},
fun(A) ->
bind(
yield(<<"read"/utf8>>, fun(K) -> K(2) end),
fun(B) -> {pure, A + B} end
)
end
)
end
).
-spec main() -> nil.
main() ->
_pipe = run(),
gleam@io:debug(_pipe),
gleam@io:println(<<"Hello from deli!"/utf8>>).