Current section

Files

Jump to
gleam_stdlib gen src gleam@list.erl
Raw

gen/src/gleam@list.erl

-module(gleam@list).
-compile(no_auto_import).
-export([length/1, reverse/1, is_empty/1, contains/2, head/1, tail/1, filter/2, filter_map/2, map/2, map_fold/3, index_map/2, try_map/2, drop/2, take/2, new/0, append/2, flatten/1, flat_map/2, fold/3, fold_right/3, index_fold/3, try_fold/3, fold_until/3, find/2, find_map/2, all/2, any/2, zip/2, strict_zip/2, unzip/1, intersperse/2, at/2, unique/1, sort/2, range/2, repeat/2, split/2, split_while/2, key_find/2, pop/2, pop_map/2, key_pop/2, key_set/3, each/2, partition/2, permutations/1, window/2, window_by_2/1, drop_while/2, take_while/2, chunk/2, sized_chunk/2, reduce/2, scan/3, last/1, combinations/2, combination_pairs/1, interleave/1, transpose/1]).
-export_type([length_mismatch/0, continue_or_stop/1]).
-type length_mismatch() :: length_mismatch.
-type continue_or_stop(UP) :: {continue, UP} | {stop, UP}.
-spec length(list(any())) -> integer().
length(A) ->
erlang:length(A).
-spec reverse(list(US)) -> list(US).
reverse(A) ->
lists:reverse(A).
-spec is_empty(list(any())) -> boolean().
is_empty(List) ->
List =:= [].
-spec contains(list(UX), UX) -> boolean().
contains(List, Elem) ->
case List of
[] ->
false;
[Head | Rest] ->
(Head =:= Elem) orelse contains(Rest, Elem)
end.
-spec head(list(UZ)) -> {ok, UZ} | {error, nil}.
head(List) ->
case List of
[] ->
{error, nil};
[X | _] ->
{ok, X}
end.
-spec tail(list(VD)) -> {ok, list(VD)} | {error, nil}.
tail(List) ->
case List of
[] ->
{error, nil};
[_ | Xs] ->
{ok, Xs}
end.
-spec do_filter(list(VI), fun((VI) -> boolean()), list(VI)) -> list(VI).
do_filter(List, Fun, Acc) ->
case List of
[] ->
lists:reverse(Acc);
[X | Xs] ->
New_acc = case Fun(X) of
true ->
[X | Acc];
false ->
Acc
end,
do_filter(Xs, Fun, New_acc)
end.
-spec filter(list(VM), fun((VM) -> boolean())) -> list(VM).
filter(List, Predicate) ->
do_filter(List, Predicate, []).
-spec do_filter_map(list(VP), fun((VP) -> {ok, VR} | {error, any()}), list(VR)) -> list(VR).
do_filter_map(List, Fun, Acc) ->
case List of
[] ->
lists:reverse(Acc);
[X | Xs] ->
New_acc = case Fun(X) of
{ok, X@1} ->
[X@1 | Acc];
{error, _} ->
Acc
end,
do_filter_map(Xs, Fun, New_acc)
end.
-spec filter_map(list(VX), fun((VX) -> {ok, VZ} | {error, any()})) -> list(VZ).
filter_map(List, Fun) ->
do_filter_map(List, Fun, []).
-spec do_map(list(WE), fun((WE) -> WG), list(WG)) -> list(WG).
do_map(List, Fun, Acc) ->
case List of
[] ->
lists:reverse(Acc);
[X | Xs] ->
do_map(Xs, Fun, [Fun(X) | Acc])
end.
-spec map(list(WJ), fun((WJ) -> WL)) -> list(WL).
map(List, Fun) ->
do_map(List, Fun, []).
-spec map_fold(list(WN), WP, fun((WN, WP) -> {WQ, WP})) -> {list(WQ), WP}.
map_fold(List, Memo, Fun) ->
gleam@pair:map_first(
fold(
List,
{[], Memo},
fun(Item, Acc) ->
{Items, Current_memo} = Acc,
{Next_item, Next_memo} = Fun(Item, Current_memo),
{[Next_item | Items], Next_memo}
end
),
fun lists:reverse/1
).
-spec do_index_map(list(WS), fun((integer(), WS) -> WU), integer(), list(WU)) -> list(WU).
do_index_map(List, Fun, Index, Acc) ->
case List of
[] ->
lists:reverse(Acc);
[X | Xs] ->
do_index_map(Xs, Fun, Index + 1, [Fun(Index, X) | Acc])
end.
-spec index_map(list(WX), fun((integer(), WX) -> WZ)) -> list(WZ).
index_map(List, Fun) ->
do_index_map(List, Fun, 0, []).
-spec do_try_map(list(XB), fun((XB) -> {ok, XD} | {error, XE}), list(XD)) -> {ok,
list(XD)} |
{error, XE}.
do_try_map(List, Fun, Acc) ->
case List of
[] ->
{ok, lists:reverse(Acc)};
[X | Xs] ->
case Fun(X) of
{ok, Y} ->
do_try_map(Xs, Fun, [Y | Acc]);
{error, Error} ->
{error, Error}
end
end.
-spec try_map(list(XL), fun((XL) -> {ok, XN} | {error, XO})) -> {ok, list(XN)} |
{error, XO}.
try_map(List, Fun) ->
do_try_map(List, Fun, []).
-spec drop(list(XU), integer()) -> list(XU).
drop(List, N) ->
case N =< 0 of
true ->
List;
false ->
case List of
[] ->
[];
[_ | Xs] ->
drop(Xs, N - 1)
end
end.
-spec do_take(list(XX), integer(), list(XX)) -> list(XX).
do_take(List, N, Acc) ->
case N =< 0 of
true ->
lists:reverse(Acc);
false ->
case List of
[] ->
lists:reverse(Acc);
[X | Xs] ->
do_take(Xs, N - 1, [X | Acc])
end
end.
-spec take(list(YB), integer()) -> list(YB).
take(List, N) ->
do_take(List, N, []).
-spec new() -> list(any()).
new() ->
[].
-spec append(list(YG), list(YG)) -> list(YG).
append(A, B) ->
lists:append(A, B).
-spec do_flatten(list(list(YK)), list(YK)) -> list(YK).
do_flatten(Lists, Acc) ->
case Lists of
[] ->
Acc;
[L | Rest] ->
do_flatten(Rest, lists:append(Acc, L))
end.
-spec flatten(list(list(YP))) -> list(YP).
flatten(Lists) ->
do_flatten(Lists, []).
-spec flat_map(list(YT), fun((YT) -> list(YV))) -> list(YV).
flat_map(List, Fun) ->
flatten(map(List, Fun)).
-spec fold(list(YY), AAA, fun((YY, AAA) -> AAA)) -> AAA.
fold(List, Initial, Fun) ->
case List of
[] ->
Initial;
[X | Rest] ->
fold(Rest, Fun(X, Initial), Fun)
end.
-spec fold_right(list(AAB), AAD, fun((AAB, AAD) -> AAD)) -> AAD.
fold_right(List, Initial, Fun) ->
case List of
[] ->
Initial;
[X | Rest] ->
Fun(X, fold_right(Rest, Initial, Fun))
end.
-spec do_index_fold(
list(AAE),
AAG,
fun((integer(), AAE, AAG) -> AAG),
integer()
) -> AAG.
do_index_fold(Over, Acc, With, Index) ->
case Over of
[] ->
Acc;
[First | Rest] ->
do_index_fold(Rest, With(Index, First, Acc), With, Index + 1)
end.
-spec index_fold(list(AAH), AAJ, fun((integer(), AAH, AAJ) -> AAJ)) -> AAJ.
index_fold(Over, Initial, Fun) ->
do_index_fold(Over, Initial, Fun, 0).
-spec try_fold(list(AAK), AAM, fun((AAK, AAM) -> {ok, AAM} | {error, AAN})) -> {ok,
AAM} |
{error, AAN}.
try_fold(Collection, Accumulator, Fun) ->
case Collection of
[] ->
{ok, Accumulator};
[First | Rest] ->
case Fun(First, Accumulator) of
{ok, Next_accumulator} ->
try_fold(Rest, Next_accumulator, Fun);
{error, Err} ->
{error, Err}
end
end.
-spec fold_until(list(AAS), AAU, fun((AAS, AAU) -> continue_or_stop(AAU))) -> AAU.
fold_until(Collection, Accumulator, Fun) ->
case Collection of
[] ->
Accumulator;
[First | Rest] ->
case Fun(First, Accumulator) of
{continue, Next_accumulator} ->
fold_until(Rest, Next_accumulator, Fun);
{stop, B} ->
B
end
end.
-spec find(list(AAW), fun((AAW) -> boolean())) -> {ok, AAW} | {error, nil}.
find(Haystack, Is_desired) ->
case Haystack of
[] ->
{error, nil};
[X | Rest] ->
case Is_desired(X) of
true ->
{ok, X};
_ ->
find(Rest, Is_desired)
end
end.
-spec find_map(list(ABA), fun((ABA) -> {ok, ABC} | {error, any()})) -> {ok, ABC} |
{error, nil}.
find_map(Haystack, Fun) ->
case Haystack of
[] ->
{error, nil};
[X | Rest] ->
case Fun(X) of
{ok, X@1} ->
{ok, X@1};
_ ->
find_map(Rest, Fun)
end
end.
-spec all(list(ABI), fun((ABI) -> boolean())) -> boolean().
all(List, Predicate) ->
case List of
[] ->
true;
[X | Rest] ->
Predicate(X) andalso all(Rest, Predicate)
end.
-spec any(list(ABK), fun((ABK) -> boolean())) -> boolean().
any(List, Predicate) ->
case List of
[] ->
false;
[X | Rest] ->
Predicate(X) orelse any(Rest, Predicate)
end.
-spec do_zip(list(ABM), list(ABO), list({ABM, ABO})) -> list({ABM, ABO}).
do_zip(Xs, Ys, Acc) ->
case {Xs, Ys} of
{[X | Xs@1], [Y | Ys@1]} ->
do_zip(Xs@1, Ys@1, [{X, Y} | Acc]);
{_, _} ->
lists:reverse(Acc)
end.
-spec zip(list(ABS), list(ABU)) -> list({ABS, ABU}).
zip(Xs, Ys) ->
do_zip(Xs, Ys, []).
-spec strict_zip(list(ABX), list(ABZ)) -> {ok, list({ABX, ABZ})} |
{error, length_mismatch()}.
strict_zip(L1, L2) ->
case erlang:length(L1) =:= erlang:length(L2) of
true ->
{ok, zip(L1, L2)};
false ->
{error, length_mismatch}
end.
-spec do_unzip(list({ACI, ACJ}), list(ACI), list(ACJ)) -> {list(ACI), list(ACJ)}.
do_unzip(Input, Xs, Ys) ->
case Input of
[] ->
{lists:reverse(Xs), lists:reverse(Ys)};
[{X, Y} | Rest] ->
do_unzip(Rest, [X | Xs], [Y | Ys])
end.
-spec unzip(list({ACI, ACJ})) -> {list(ACI), list(ACJ)}.
unzip(Input) ->
do_unzip(Input, [], []).
-spec do_intersperse(list(ACN), ACN, list(ACN)) -> list(ACN).
do_intersperse(List, Separator, Acc) ->
case List of
[] ->
lists:reverse(Acc);
[X | Rest] ->
do_intersperse(Rest, Separator, [X, Separator | Acc])
end.
-spec intersperse(list(ACR), ACR) -> list(ACR).
intersperse(List, Elem) ->
case List of
[] ->
List;
[_] ->
List;
[X | Rest] ->
do_intersperse(Rest, Elem, [X])
end.
-spec at(list(ACU), integer()) -> {ok, ACU} | {error, nil}.
at(List, Index) ->
case Index < 0 of
true ->
{error, nil};
false ->
case List of
[] ->
{error, nil};
[X | Rest] ->
case Index =:= 0 of
true ->
{ok, X};
false ->
at(Rest, Index - 1)
end
end
end.
-spec unique(list(ACY)) -> list(ACY).
unique(List) ->
case List of
[] ->
[];
[X | Rest] ->
[X | unique(filter(Rest, fun(Y) -> Y /= X end))]
end.
-spec merge_sort(list(ADB), list(ADB), fun((ADB, ADB) -> gleam@order:order())) -> list(ADB).
merge_sort(A, B, Compare) ->
case {A, B} of
{[], _} ->
B;
{_, []} ->
A;
{[Ax | Ar], [Bx | Br]} ->
case Compare(Ax, Bx) of
lt ->
[Ax | merge_sort(Ar, B, Compare)];
_ ->
[Bx | merge_sort(A, Br, Compare)]
end
end.
-spec do_sort(list(ADF), fun((ADF, ADF) -> gleam@order:order()), integer()) -> list(ADF).
do_sort(List, Compare, List_length) ->
case List_length < 2 of
true ->
List;
false ->
Split_length = List_length div 2,
A_list = take(List, Split_length),
B_list = drop(List, Split_length),
merge_sort(
do_sort(A_list, Compare, Split_length),
do_sort(B_list, Compare, List_length - Split_length),
Compare
)
end.
-spec sort(list(ADI), fun((ADI, ADI) -> gleam@order:order())) -> list(ADI).
sort(List, Compare) ->
do_sort(List, Compare, erlang:length(List)).
-spec range(integer(), integer()) -> list(integer()).
range(Start, Stop) ->
case gleam@int:compare(Start, Stop) of
eq ->
[];
gt ->
[Start | range(Start - 1, Stop)];
lt ->
[Start | range(Start + 1, Stop)]
end.
-spec do_repeat(ADM, integer(), list(ADM)) -> list(ADM).
do_repeat(A, Times, Acc) ->
case Times =< 0 of
true ->
Acc;
false ->
do_repeat(A, Times - 1, [A | Acc])
end.
-spec repeat(ADP, integer()) -> list(ADP).
repeat(A, Times) ->
do_repeat(A, Times, []).
-spec do_split(list(ADR), integer(), list(ADR)) -> {list(ADR), list(ADR)}.
do_split(List, N, Taken) ->
case N =< 0 of
true ->
{lists:reverse(Taken), List};
false ->
case List of
[] ->
{lists:reverse(Taken), []};
[X | Xs] ->
do_split(Xs, N - 1, [X | Taken])
end
end.
-spec split(list(ADW), integer()) -> {list(ADW), list(ADW)}.
split(List, Index) ->
do_split(List, Index, []).
-spec do_split_while(list(AEA), fun((AEA) -> boolean()), list(AEA)) -> {list(AEA),
list(AEA)}.
do_split_while(List, F, Acc) ->
case List of
[] ->
{lists:reverse(Acc), []};
[X | Xs] ->
case F(X) of
false ->
{lists:reverse(Acc), List};
_ ->
do_split_while(Xs, F, [X | Acc])
end
end.
-spec split_while(list(AEF), fun((AEF) -> boolean())) -> {list(AEF), list(AEF)}.
split_while(List, Predicate) ->
do_split_while(List, Predicate, []).
-spec key_find(list({AEJ, AEK}), AEJ) -> {ok, AEK} | {error, nil}.
key_find(Keyword_list, Desired_key) ->
find_map(
Keyword_list,
fun(Keyword) ->
{Key, Value} = Keyword,
case Key =:= Desired_key of
true ->
{ok, Value};
false ->
{error, nil}
end
end
).
-spec do_pop(list(AES), fun((AES) -> boolean()), list(AES)) -> {ok,
{AES, list(AES)}} |
{error, nil}.
do_pop(Haystack, Predicate, Checked) ->
case Haystack of
[] ->
{error, nil};
[X | Rest] ->
case Predicate(X) of
true ->
{ok, {X, lists:append(lists:reverse(Checked), Rest)}};
false ->
do_pop(Rest, Predicate, [X | Checked])
end
end.
-spec pop(list(AES), fun((AES) -> boolean())) -> {ok, {AES, list(AES)}} |
{error, nil}.
pop(Haystack, Is_desired) ->
do_pop(Haystack, Is_desired, []).
-spec do_pop_map(list(AFB), fun((AFB) -> {ok, AFD} | {error, any()}), list(AFB)) -> {ok,
{AFD,
list(AFB)}} |
{error, nil}.
do_pop_map(Haystack, Mapper, Checked) ->
case Haystack of
[] ->
{error, nil};
[X | Rest] ->
case Mapper(X) of
{ok, Y} ->
{ok, {Y, lists:append(lists:reverse(Checked), Rest)}};
{error, _} ->
do_pop_map(Rest, Mapper, [X | Checked])
end
end.
-spec pop_map(list(AFB), fun((AFB) -> {ok, AFD} | {error, any()})) -> {ok,
{AFD,
list(AFB)}} |
{error, nil}.
pop_map(Haystack, Is_desired) ->
do_pop_map(Haystack, Is_desired, []).
-spec key_pop(list({AFK, AFL}), AFK) -> {ok, {AFL, list({AFK, AFL})}} |
{error, nil}.
key_pop(Haystack, Key) ->
pop_map(
Haystack,
fun(Entry) ->
{K, V} = Entry,
case K of
K@1 when K@1 =:= Key ->
{ok, V};
_ ->
{error, nil}
end
end
).
-spec key_set(list({AFQ, AFR}), AFQ, AFR) -> list({AFQ, AFR}).
key_set(List, Key, Value) ->
case List of
[] ->
[{Key, Value}];
[{K, _} | Rest] when K =:= Key ->
[{Key, Value} | Rest];
[First | Rest@1] ->
[First | key_set(Rest@1, Key, Value)]
end.
-spec each(list(AFU), fun((AFU) -> any())) -> nil.
each(List, F) ->
case List of
[] ->
nil;
[X | Xs] ->
F(X),
each(Xs, F)
end.
-spec do_partition(list(AGC), fun((AGC) -> boolean()), list(AGC), list(AGC)) -> {list(AGC),
list(AGC)}.
do_partition(List, Categorise, Trues, Falses) ->
case List of
[] ->
{lists:reverse(Trues), lists:reverse(Falses)};
[X | Xs] ->
case Categorise(X) of
true ->
do_partition(Xs, Categorise, [X | Trues], Falses);
false ->
do_partition(Xs, Categorise, Trues, [X | Falses])
end
end.
-spec partition(list(AGC), fun((AGC) -> boolean())) -> {list(AGC), list(AGC)}.
partition(List, Categorise) ->
do_partition(List, Categorise, [], []).
-spec permutations(list(AGG)) -> list(list(AGG)).
permutations(L) ->
case L of
[] ->
[[]];
_ ->
flatten(
map(
L,
fun(X) ->
map(
permutations(filter(L, fun(Y) -> Y /= X end)),
fun(_gleam_capture) ->
lists:append([X], _gleam_capture)
end
)
end
)
)
end.
-spec do_window(list(list(AGK)), list(AGK), integer()) -> list(list(AGK)).
do_window(Acc, L, N) ->
Window = take(L, N),
case erlang:length(Window) =:= N of
true ->
do_window([Window | Acc], drop(L, 1), N);
false ->
Acc
end.
-spec window(list(AGQ), integer()) -> list(list(AGQ)).
window(L, N) ->
lists:reverse(do_window([], L, N)).
-spec window_by_2(list(AGU)) -> list({AGU, AGU}).
window_by_2(L) ->
zip(L, drop(L, 1)).
-spec drop_while(list(AGX), fun((AGX) -> boolean())) -> list(AGX).
drop_while(List, Predicate) ->
case List of
[] ->
[];
[X | Xs] ->
case Predicate(X) of
true ->
drop_while(Xs, Predicate);
false ->
[X | Xs]
end
end.
-spec do_take_while(list(AHA), fun((AHA) -> boolean()), list(AHA)) -> list(AHA).
do_take_while(List, Predicate, Acc) ->
case List of
[] ->
lists:reverse(Acc);
[Head | Tail] ->
case Predicate(Head) of
true ->
do_take_while(Tail, Predicate, [Head | Acc]);
false ->
lists:reverse(Acc)
end
end.
-spec take_while(list(AHE), fun((AHE) -> boolean())) -> list(AHE).
take_while(List, Predicate) ->
do_take_while(List, Predicate, []).
-spec do_chunk(list(AHH), fun((AHH) -> AHJ), AHJ, list(AHH), list(list(AHH))) -> list(list(AHH)).
do_chunk(List, F, Previous_key, Current_chunk, Acc) ->
case List of
[] ->
lists:reverse([lists:reverse(Current_chunk) | Acc]);
[Head | Tail] ->
Key = F(Head),
case Key =:= Previous_key of
false ->
do_chunk(
Tail,
F,
Key,
[Head],
[lists:reverse(Current_chunk) | Acc]
);
true ->
do_chunk(Tail, F, Key, [Head | Current_chunk], Acc)
end
end.
-spec chunk(list(AHP), fun((AHP) -> any())) -> list(list(AHP)).
chunk(List, F) ->
case List of
[] ->
[];
[Head | Tail] ->
do_chunk(Tail, F, F(Head), [Head], [])
end.
-spec do_sized_chunk(
list(AHU),
integer(),
integer(),
list(AHU),
list(list(AHU))
) -> list(list(AHU)).
do_sized_chunk(List, Count, Left, Current_chunk, Acc) ->
case List of
[] ->
case Current_chunk of
[] ->
lists:reverse(Acc);
Remaining ->
lists:reverse([lists:reverse(Remaining) | Acc])
end;
[Head | Tail] ->
Chunk = [Head | Current_chunk],
case Left > 1 of
false ->
do_sized_chunk(
Tail,
Count,
Count,
[],
[lists:reverse(Chunk) | Acc]
);
true ->
do_sized_chunk(Tail, Count, Left - 1, Chunk, Acc)
end
end.
-spec sized_chunk(list(AIB), integer()) -> list(list(AIB)).
sized_chunk(List, Count) ->
do_sized_chunk(List, Count, Count, [], []).
-spec reduce(list(AIF), fun((AIF, AIF) -> AIF)) -> {ok, AIF} | {error, nil}.
reduce(List, Fun) ->
case List of
[] ->
{error, nil};
[Head | Tail] ->
{ok, fold(Tail, Head, Fun)}
end.
-spec do_scan(list(AIJ), AIL, list(AIL), fun((AIJ, AIL) -> AIL)) -> list(AIL).
do_scan(List, Accumulator, Accumulated, Fun) ->
case List of
[] ->
lists:reverse(Accumulated);
[X | Xs] ->
Next = Fun(X, Accumulator),
do_scan(Xs, Next, [Next | Accumulated], Fun)
end.
-spec scan(list(AIO), AIQ, fun((AIO, AIQ) -> AIQ)) -> list(AIQ).
scan(List, Initial, Fun) ->
do_scan(List, Initial, [], Fun).
-spec last(list(AIS)) -> {ok, AIS} | {error, nil}.
last(List) ->
reduce(List, fun(Elem, _) -> Elem end).
-spec combinations(list(AIW), integer()) -> list(list(AIW)).
combinations(Items, N) ->
case N of
0 ->
[[]];
_ ->
case Items of
[] ->
[];
[X | Xs] ->
First_combinations = lists:reverse(
map(combinations(Xs, N - 1), fun(Com) -> [X | Com] end)
),
fold(
First_combinations,
combinations(Xs, N),
fun(C, Acc) -> [C | Acc] end
)
end
end.
-spec do_combination_pairs(list(AJA)) -> list(list({AJA, AJA})).
do_combination_pairs(Items) ->
case Items of
[] ->
[];
[X | Xs] ->
First_combinations = map(Xs, fun(Other) -> {X, Other} end),
[First_combinations | do_combination_pairs(Xs)]
end.
-spec combination_pairs(list(AJE)) -> list({AJE, AJE}).
combination_pairs(Items) ->
flatten(do_combination_pairs(Items)).
-spec interleave(list(list(AJH))) -> list(AJH).
interleave(List) ->
flatten(transpose(List)).
-spec transpose(list(list(AJL))) -> list(list(AJL)).
transpose(List_of_list) ->
Take_first = fun(List) -> case List of
[] ->
[];
[F] ->
[F];
[F@1 | _] ->
[F@1]
end end,
case List_of_list of
[] ->
[];
[[] | Xss] ->
transpose(Xss);
Rows ->
Firsts = flatten(map(Rows, Take_first)),
Rest = transpose(
map(Rows, fun(_gleam_capture) -> drop(_gleam_capture, 1) end)
),
[Firsts | Rest]
end.