Packages

A library for working with lazy lists in Gleam

Current section

Files

Jump to
gleam_zlists src gleam_zlists.erl
Raw

src/gleam_zlists.erl

-module(gleam_zlists).
-compile(no_auto_import).
-export([of_list/1, append/2, to_list/1, range/3, take/2, flat_map/2, iterate/2, each/2, reduce/3, map/2, drop_while/2, drop/2, take_while/2, filter/2, concat/1, split/2, split_while/2, zip/2, dedup/1, count/1, singleton/1, new/0, is_empty/1, cons/2, head/1, tail/1, uncons/1, all/2, any/2, fetch/2, find/2, has_member/2, reverse/1, slice/3, indices/0, with_index/1, unzip/1, sum/1, max/1, min/1, to_iterator/1, of_iterator/1]).
-spec of_list(list(EPH)) -> gleam_zlists@interop:z_list(EPH).
of_list(List) ->
gleam_zlists@interface:new_1(List, fun() -> [] end).
-spec append(gleam_zlists@interop:z_list(EPK), gleam_zlists@interop:z_list(EPK)) -> gleam_zlists@interop:z_list(EPK).
append(Left, Right) ->
gleam_zlists@interface:new_2(Left, fun() -> Right end).
-spec to_list(gleam_zlists@interop:z_list(EPO)) -> list(EPO).
to_list(Zlist) ->
gleam_zlists@interface:expand(Zlist).
-spec range(integer(), integer(), integer()) -> gleam_zlists@interop:z_list(integer()).
range(First, Last, Step) ->
gleam_zlists@interface:seq(First, Last, Step).
-spec take(gleam_zlists@interop:z_list(EPS), integer()) -> gleam_zlists@interop:z_list(EPS).
take(Zlist, N) ->
gleam_zlists@interface:take(N, Zlist).
-spec flat_map(
gleam_zlists@interop:z_list(EPV),
fun((EPV) -> gleam_zlists@interop:z_list(EPX))
) -> gleam_zlists@interop:z_list(EPX).
flat_map(Zlist, Fun) ->
gleam_zlists@interface:generate(Zlist, Fun).
-spec iterate(EQA, fun((EQA) -> EQA)) -> gleam_zlists@interop:z_list(EQA).
iterate(Start_value, Next_fun) ->
gleam_zlists@interface:recurrent_2(Start_value, Next_fun).
-spec each(gleam_zlists@interop:z_list(EQC), fun((EQC) -> any())) -> nil.
each(Zlist, Fun) ->
gleam_zlists@interface:foreach(Fun, Zlist).
-spec reduce(gleam_zlists@interop:z_list(EQF), EQH, fun((EQF, EQH) -> EQH)) -> EQH.
reduce(Zlist, Acc, Fun) ->
gleam_zlists@interface:foldl(Fun, Acc, Zlist).
-spec map(gleam_zlists@interop:z_list(EQI), fun((EQI) -> EQK)) -> gleam_zlists@interop:z_list(EQK).
map(Zlist, Fun) ->
gleam_zlists@interface:map(Fun, Zlist).
-spec drop_while(gleam_zlists@interop:z_list(EQM), fun((EQM) -> boolean())) -> gleam_zlists@interop:z_list(EQM).
drop_while(Zlist, Fun) ->
gleam_zlists@interface:dropwhile(Fun, Zlist).
-spec drop(gleam_zlists@interop:z_list(EQP), integer()) -> gleam_zlists@interop:z_list(EQP).
drop(Zlist, N) ->
{_@1, Zls} = split(Zlist, N),
Zls.
-spec take_while(gleam_zlists@interop:z_list(EQS), fun((EQS) -> boolean())) -> gleam_zlists@interop:z_list(EQS).
take_while(Zlist, Fun) ->
gleam_zlists@interface:takewhile(Fun, Zlist).
-spec filter(gleam_zlists@interop:z_list(EQV), fun((EQV) -> boolean())) -> gleam_zlists@interop:z_list(EQV).
filter(Zlist, Fun) ->
gleam_zlists@interface:filter(Fun, Zlist).
-spec concat(gleam_zlists@interop:z_list(gleam_zlists@interop:z_list(EQY))) -> gleam_zlists@interop:z_list(EQY).
concat(Zlists) ->
gleam_zlists@interface:append(Zlists).
-spec split(gleam_zlists@interop:z_list(ERC), integer()) -> {list(ERC),
gleam_zlists@interop:z_list(ERC)}.
split(Zlist, N) ->
gleam_zlists@interface:scroll(N, Zlist).
-spec split_while(gleam_zlists@interop:z_list(ERG), fun((ERG) -> boolean())) -> {list(ERG),
gleam_zlists@interop:z_list(ERG)}.
split_while(Zlist, Fun) ->
gleam_zlists@interface:splitwith(Fun, Zlist).
-spec zip(gleam_zlists@interop:z_list(ERK), gleam_zlists@interop:z_list(ERM)) -> gleam_zlists@interop:z_list({ERK,
ERM}).
zip(Left, Right) ->
gleam_zlists@interface:ziph(Left, Right).
-spec dedup(gleam_zlists@interop:z_list(ERP)) -> gleam_zlists@interop:z_list(ERP).
dedup(Zlist) ->
gleam_zlists@interface:unique_1(Zlist).
-spec count(gleam_zlists@interop:z_list(any())) -> integer().
count(Zlist) ->
gleam_zlists@interface:count(Zlist).
-spec singleton(ERU) -> gleam_zlists@interop:z_list(ERU).
singleton(Value) ->
of_list([Value]).
-spec new() -> gleam_zlists@interop:z_list(any()).
new() ->
of_list([]).
-spec is_empty(gleam_zlists@interop:z_list(any())) -> boolean().
is_empty(Zlist) ->
Head_length = begin
_pipe = Zlist,
_pipe@1 = take(_pipe, 1),
count(_pipe@1)
end,
Head_length =:= 0.
-spec cons(gleam_zlists@interop:z_list(ESA), ESA) -> gleam_zlists@interop:z_list(ESA).
cons(Zlist, First_value) ->
_pipe = First_value,
_pipe@1 = singleton(_pipe),
append(_pipe@1, Zlist).
-spec head(gleam_zlists@interop:z_list(ESE)) -> {ok, ESE} | {error, nil}.
head(Zlist) ->
Ls = begin
_pipe = Zlist,
_pipe@1 = take(_pipe, 1),
to_list(_pipe@1)
end,
case Ls of
[] ->
{error, nil};
[V] ->
{ok, V}
end.
-spec tail(gleam_zlists@interop:z_list(ESI)) -> {ok,
gleam_zlists@interop:z_list(ESI)} |
{error, nil}.
tail(Zlist) ->
Ls = begin
_pipe = Zlist,
_pipe@1 = take(_pipe, 1),
to_list(_pipe@1)
end,
case Ls of
[] ->
{error, nil};
[_@1] ->
_pipe@2 = Zlist,
_pipe@3 = drop(_pipe@2, 1),
{ok, _pipe@3}
end.
-spec uncons(gleam_zlists@interop:z_list(ESN)) -> {ok,
{ESN,
gleam_zlists@interop:z_list(ESN)}} |
{error, nil}.
uncons(Zlist) ->
case {head(Zlist), tail(Zlist)} of
{{ok, Hd}, {ok, Tl}} ->
_pipe = {Hd, Tl},
{ok, _pipe};
_@1 ->
{error, nil}
end.
-spec all(gleam_zlists@interop:z_list(ESS), fun((ESS) -> boolean())) -> boolean().
all(Zlist, Fun) ->
case uncons(Zlist) of
{error, nil} ->
true;
{ok, {Hd, Tl}} ->
Head_bool = Fun(Hd),
{_@1, Diff_bool_zls} = split_while(
Tl,
fun(X) -> Fun(X) =:= Head_bool end
),
case is_empty(Diff_bool_zls) of
true ->
Head_bool;
false ->
gleam@bool:negate(Head_bool)
end
end.
-spec any(gleam_zlists@interop:z_list(ESU), fun((ESU) -> boolean())) -> boolean().
any(Zlist, Fun) ->
{_@1, Zls_b} = split_while(
Zlist,
fun(X) ->
_pipe = X,
_pipe@1 = Fun(_pipe),
gleam@bool:negate(_pipe@1)
end
),
_pipe@2 = Zls_b,
_pipe@3 = is_empty(_pipe@2),
gleam@bool:negate(_pipe@3).
-spec fetch(gleam_zlists@interop:z_list(ESW), integer()) -> {ok, ESW} |
{error, nil}.
fetch(Zlist, Index) ->
_pipe = Zlist,
_pipe@1 = drop(_pipe, Index),
head(_pipe@1).
-spec find(gleam_zlists@interop:z_list(ETA), fun((ETA) -> boolean())) -> {ok,
ETA} |
{error, nil}.
find(Zlist, Fun) ->
_pipe = Zlist,
_pipe@3 = drop_while(
_pipe,
fun(X) ->
_pipe@1 = X,
_pipe@2 = Fun(_pipe@1),
gleam@bool:negate(_pipe@2)
end
),
head(_pipe@3).
-spec has_member(gleam_zlists@interop:z_list(ETE), ETE) -> boolean().
has_member(Zlist, Element) ->
case find(Zlist, fun(X) -> X =:= Element end) of
{ok, _@1} ->
true;
{error, nil} ->
false
end.
-spec reverse(gleam_zlists@interop:z_list(ETG)) -> gleam_zlists@interop:z_list(ETG).
reverse(Zlist) ->
reduce(Zlist, new(), fun(X, Acc) -> cons(Acc, X) end).
-spec slice(gleam_zlists@interop:z_list(ETJ), integer(), integer()) -> gleam_zlists@interop:z_list(ETJ).
slice(Zlist, Start_index, Amount) ->
_pipe = Zlist,
_pipe@1 = drop(_pipe, Start_index),
take(_pipe@1, Amount).
-spec indices() -> gleam_zlists@interop:z_list(integer()).
indices() ->
iterate(0, fun(X) -> X + 1 end).
-spec with_index(gleam_zlists@interop:z_list(ETN)) -> gleam_zlists@interop:z_list({ETN,
integer()}).
with_index(Zlist) ->
zip(Zlist, indices()).
-spec unzip(gleam_zlists@interop:z_list({ETQ, ETR})) -> {gleam_zlists@interop:z_list(ETQ),
gleam_zlists@interop:z_list(ETR)}.
unzip(Zlist) ->
_pipe = Zlist,
_pipe@1 = reverse(_pipe),
reduce(
_pipe@1,
{new(), new()},
fun(It, Acc) ->
{X, Y} = It,
{Acc_xs, Acc_ys} = Acc,
{cons(Acc_xs, X), cons(Acc_ys, Y)}
end
).
-spec sum(gleam_zlists@interop:z_list(float())) -> float().
sum(Zlist) ->
reduce(Zlist, 0.0, fun(X, Acc) -> X + Acc end).
-spec max(gleam_zlists@interop:z_list(float())) -> {ok, float()} | {error, nil}.
max(Zlist) ->
_pipe = Zlist,
_pipe@1 = uncons(_pipe),
gleam@result:map(
_pipe@1,
fun(X) ->
{Hd, Tl} = X,
reduce(Tl, Hd, fun(X@1, Acc) -> case X@1 > Acc of
true ->
X@1;
false ->
Acc
end end)
end
).
-spec min(gleam_zlists@interop:z_list(float())) -> {ok, float()} | {error, nil}.
min(Zlist) ->
_pipe = Zlist,
_pipe@1 = uncons(_pipe),
gleam@result:map(
_pipe@1,
fun(X) ->
{Hd, Tl} = X,
reduce(Tl, Hd, fun(X@1, Acc) -> case X@1 < Acc of
true ->
X@1;
false ->
Acc
end end)
end
).
-spec to_iterator(gleam_zlists@interop:z_list(EUC)) -> gleam@iterator:iterator(EUC).
to_iterator(Zlist) ->
Yield = fun(Acc) -> case uncons(Acc) of
{error, nil} ->
done;
{ok, {Hd, Tl}} ->
{next, Hd, Tl}
end end,
gleam@iterator:unfold(Zlist, Yield).
-spec recurrent(EUF, EUG, fun((EUF, EUG) -> {ok, {EUF, EUG}} | {error, nil})) -> gleam_zlists@interop:z_list(EUF).
recurrent(X0, S0, Rec_fun) ->
gleam_zlists@interface:new_2(singleton(X0), fun() -> case Rec_fun(X0, S0) of
{ok, {X1, S1}} ->
recurrent(X1, S1, Rec_fun);
{error, nil} ->
new()
end end).
-spec of_iterator(gleam@iterator:iterator(EUK)) -> gleam_zlists@interop:z_list(EUK).
of_iterator(Iter) ->
Rec_fun = fun(_, T1) -> case gleam@iterator:step(T1) of
{next, E, Es} ->
{ok, {E, Es}};
done ->
{error, nil}
end end,
case gleam@iterator:step(Iter) of
done ->
new();
{next, E@1, Es@1} ->
recurrent(E@1, Es@1, Rec_fun)
end.