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(WK) :: {continue, WK} | {stop, WK}.
-spec length(list(any())) -> integer().
length(List) ->
erlang:length(List).
-spec reverse(list(WP)) -> list(WP).
reverse(Xs) ->
lists:reverse(Xs).
-spec is_empty(list(any())) -> boolean().
is_empty(List) ->
List =:= [].
-spec contains(list(WX), WX) -> boolean().
contains(List, Elem) ->
case List of
[] ->
false;
[Head | Rest] ->
(Head =:= Elem) orelse contains(Rest, Elem)
end.
-spec head(list(WZ)) -> {ok, WZ} | {error, nil}.
head(List) ->
case List of
[] ->
{error, nil};
[X | _@1] ->
{ok, X}
end.
-spec tail(list(XD)) -> {ok, list(XD)} | {error, nil}.
tail(List) ->
case List of
[] ->
{error, nil};
[_@1 | Xs] ->
{ok, Xs}
end.
-spec do_filter(list(XI), fun((XI) -> boolean()), list(XI)) -> list(XI).
do_filter(List, Fun, Acc) ->
case List of
[] ->
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(XM), fun((XM) -> boolean())) -> list(XM).
filter(List, Predicate) ->
do_filter(List, Predicate, []).
-spec do_filter_map(list(XP), fun((XP) -> {ok, XR} | {error, any()}), list(XR)) -> list(XR).
do_filter_map(List, Fun, Acc) ->
case List of
[] ->
reverse(Acc);
[X | Xs] ->
New_acc = case Fun(X) of
{ok, X@1} ->
[X@1 | Acc];
{error, _@1} ->
Acc
end,
do_filter_map(Xs, Fun, New_acc)
end.
-spec filter_map(list(XX), fun((XX) -> {ok, XZ} | {error, any()})) -> list(XZ).
filter_map(List, Fun) ->
do_filter_map(List, Fun, []).
-spec do_map(list(YE), fun((YE) -> YG), list(YG)) -> list(YG).
do_map(List, Fun, Acc) ->
case List of
[] ->
reverse(Acc);
[X | Xs] ->
do_map(Xs, Fun, [Fun(X) | Acc])
end.
-spec map(list(YJ), fun((YJ) -> YL)) -> list(YL).
map(List, Fun) ->
do_map(List, Fun, []).
-spec map_fold(list(YN), YP, fun((YP, YN) -> {YP, YQ})) -> {YP, list(YQ)}.
map_fold(List, Acc, Fun) ->
_pipe = fold(
List,
{Acc, []},
fun(Acc@1, Item) ->
{Current_acc, Items} = Acc@1,
{Next_acc, Next_item} = Fun(Current_acc, Item),
{Next_acc, [Next_item | Items]}
end
),
gleam@pair:map_second(_pipe, fun reverse/1).
-spec do_index_map(list(YS), fun((integer(), YS) -> YU), integer(), list(YU)) -> list(YU).
do_index_map(List, Fun, Index, Acc) ->
case List of
[] ->
reverse(Acc);
[X | Xs] ->
Acc@1 = [Fun(Index, X) | Acc],
do_index_map(Xs, Fun, Index + 1, Acc@1)
end.
-spec index_map(list(YX), fun((integer(), YX) -> YZ)) -> list(YZ).
index_map(List, Fun) ->
do_index_map(List, Fun, 0, []).
-spec do_try_map(list(AAB), fun((AAB) -> {ok, AAD} | {error, AAE}), list(AAD)) -> {ok,
list(AAD)} |
{error, AAE}.
do_try_map(List, Fun, Acc) ->
case List of
[] ->
{ok, 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(AAL), fun((AAL) -> {ok, AAN} | {error, AAO})) -> {ok,
list(AAN)} |
{error, AAO}.
try_map(List, Fun) ->
do_try_map(List, Fun, []).
-spec drop(list(AAU), integer()) -> list(AAU).
drop(List, N) ->
case N =< 0 of
true ->
List;
false ->
case List of
[] ->
[];
[_@1 | Xs] ->
drop(Xs, N - 1)
end
end.
-spec do_take(list(AAX), integer(), list(AAX)) -> list(AAX).
do_take(List, N, Acc) ->
case N =< 0 of
true ->
reverse(Acc);
false ->
case List of
[] ->
reverse(Acc);
[X | Xs] ->
do_take(Xs, N - 1, [X | Acc])
end
end.
-spec take(list(ABB), integer()) -> list(ABB).
take(List, N) ->
do_take(List, N, []).
-spec new() -> list(any()).
new() ->
[].
-spec append(list(ABG), list(ABG)) -> list(ABG).
append(First, Second) ->
lists:append(First, Second).
-spec do_flatten(list(list(ABO)), list(ABO)) -> list(ABO).
do_flatten(Lists, Acc) ->
case Lists of
[] ->
Acc;
[L | Rest] ->
do_flatten(Rest, append(Acc, L))
end.
-spec flatten(list(list(ABT))) -> list(ABT).
flatten(Lists) ->
do_flatten(Lists, []).
-spec flat_map(list(ABX), fun((ABX) -> list(ABZ))) -> list(ABZ).
flat_map(List, Fun) ->
_pipe = map(List, Fun),
flatten(_pipe).
-spec fold(list(ACC), ACE, fun((ACE, ACC) -> ACE)) -> ACE.
fold(List, Initial, Fun) ->
case List of
[] ->
Initial;
[X | Rest] ->
fold(Rest, Fun(Initial, X), Fun)
end.
-spec fold_right(list(ACF), ACH, fun((ACH, ACF) -> ACH)) -> ACH.
fold_right(List, Initial, Fun) ->
case List of
[] ->
Initial;
[X | Rest] ->
Fun(fold_right(Rest, Initial, Fun), X)
end.
-spec do_index_fold(
list(ACI),
ACK,
fun((ACK, ACI, integer()) -> ACK),
integer()
) -> ACK.
do_index_fold(Over, Acc, With, Index) ->
case Over of
[] ->
Acc;
[First | Rest] ->
do_index_fold(Rest, With(Acc, First, Index), With, Index + 1)
end.
-spec index_fold(list(ACL), ACN, fun((ACN, ACL, integer()) -> ACN)) -> ACN.
index_fold(Over, Initial, Fun) ->
do_index_fold(Over, Initial, Fun, 0).
-spec try_fold(list(ACO), ACQ, fun((ACQ, ACO) -> {ok, ACQ} | {error, ACR})) -> {ok,
ACQ} |
{error, ACR}.
try_fold(Collection, Accumulator, Fun) ->
case Collection of
[] ->
{ok, Accumulator};
[First | Rest] ->
case Fun(Accumulator, First) of
{error, _try} -> {error, _try};
{ok, Accumulator@1} ->
try_fold(Rest, Accumulator@1, Fun)
end
end.
-spec fold_until(list(ACW), ACY, fun((ACY, ACW) -> continue_or_stop(ACY))) -> ACY.
fold_until(Collection, Accumulator, Fun) ->
case Collection of
[] ->
Accumulator;
[First | Rest] ->
case Fun(Accumulator, First) of
{continue, Next_accumulator} ->
fold_until(Rest, Next_accumulator, Fun);
{stop, B} ->
B
end
end.
-spec find(list(ADA), fun((ADA) -> boolean())) -> {ok, ADA} | {error, nil}.
find(Haystack, Is_desired) ->
case Haystack of
[] ->
{error, nil};
[X | Rest] ->
case Is_desired(X) of
true ->
{ok, X};
_@1 ->
find(Rest, Is_desired)
end
end.
-spec find_map(list(ADE), fun((ADE) -> {ok, ADG} | {error, any()})) -> {ok, ADG} |
{error, nil}.
find_map(Haystack, Fun) ->
case Haystack of
[] ->
{error, nil};
[X | Rest] ->
case Fun(X) of
{ok, X@1} ->
{ok, X@1};
_@1 ->
find_map(Rest, Fun)
end
end.
-spec all(list(ADM), fun((ADM) -> boolean())) -> boolean().
all(List, Predicate) ->
case List of
[] ->
true;
[X | Rest] ->
Predicate(X) andalso all(Rest, Predicate)
end.
-spec any(list(ADO), fun((ADO) -> boolean())) -> boolean().
any(List, Predicate) ->
case List of
[] ->
false;
[X | Rest] ->
Predicate(X) orelse any(Rest, Predicate)
end.
-spec do_zip(list(ADQ), list(ADS), list({ADQ, ADS})) -> list({ADQ, ADS}).
do_zip(Xs, Ys, Acc) ->
case {Xs, Ys} of
{[X | Xs@1], [Y | Ys@1]} ->
do_zip(Xs@1, Ys@1, [{X, Y} | Acc]);
{_@1, _@2} ->
reverse(Acc)
end.
-spec zip(list(ADW), list(ADY)) -> list({ADW, ADY}).
zip(Xs, Ys) ->
do_zip(Xs, Ys, []).
-spec strict_zip(list(AEB), list(AED)) -> {ok, list({AEB, AED})} |
{error, length_mismatch()}.
strict_zip(L1, L2) ->
case length(L1) =:= length(L2) of
true ->
{ok, zip(L1, L2)};
false ->
{error, length_mismatch}
end.
-spec do_unzip(list({AEM, AEN}), list(AEM), list(AEN)) -> {list(AEM), list(AEN)}.
do_unzip(Input, Xs, Ys) ->
case Input of
[] ->
{reverse(Xs), reverse(Ys)};
[{X, Y} | Rest] ->
do_unzip(Rest, [X | Xs], [Y | Ys])
end.
-spec unzip(list({AEM, AEN})) -> {list(AEM), list(AEN)}.
unzip(Input) ->
do_unzip(Input, [], []).
-spec do_intersperse(list(AER), AER, list(AER)) -> list(AER).
do_intersperse(List, Separator, Acc) ->
case List of
[] ->
reverse(Acc);
[X | Rest] ->
do_intersperse(Rest, Separator, [X, Separator | Acc])
end.
-spec intersperse(list(AEV), AEV) -> list(AEV).
intersperse(List, Elem) ->
case List of
[] ->
List;
[_@1] ->
List;
[X | Rest] ->
do_intersperse(Rest, Elem, [X])
end.
-spec at(list(AEY), integer()) -> {ok, AEY} | {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(AFC)) -> list(AFC).
unique(List) ->
case List of
[] ->
[];
[X | Rest] ->
[X | unique(filter(Rest, fun(Y) -> Y /= X end))]
end.
-spec merge_sort(list(AFF), list(AFF), fun((AFF, AFF) -> gleam@order:order())) -> list(AFF).
merge_sort(A, B, Compare) ->
case {A, B} of
{[], _@1} ->
B;
{_@2, []} ->
A;
{[Ax | Ar], [Bx | Br]} ->
case Compare(Ax, Bx) of
lt ->
[Ax | merge_sort(Ar, B, Compare)];
_@3 ->
[Bx | merge_sort(A, Br, Compare)]
end
end.
-spec do_sort(list(AFJ), fun((AFJ, AFJ) -> gleam@order:order()), integer()) -> list(AFJ).
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(AFM), fun((AFM, AFM) -> gleam@order:order())) -> list(AFM).
sort(List, Compare) ->
do_sort(List, Compare, 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(AFQ, integer(), list(AFQ)) -> list(AFQ).
do_repeat(A, Times, Acc) ->
case Times =< 0 of
true ->
Acc;
false ->
do_repeat(A, Times - 1, [A | Acc])
end.
-spec repeat(AFT, integer()) -> list(AFT).
repeat(A, Times) ->
do_repeat(A, Times, []).
-spec do_split(list(AFV), integer(), list(AFV)) -> {list(AFV), list(AFV)}.
do_split(List, N, Taken) ->
case N =< 0 of
true ->
{reverse(Taken), List};
false ->
case List of
[] ->
{reverse(Taken), []};
[X | Xs] ->
do_split(Xs, N - 1, [X | Taken])
end
end.
-spec split(list(AGA), integer()) -> {list(AGA), list(AGA)}.
split(List, Index) ->
do_split(List, Index, []).
-spec do_split_while(list(AGE), fun((AGE) -> boolean()), list(AGE)) -> {list(AGE),
list(AGE)}.
do_split_while(List, F, Acc) ->
case List of
[] ->
{reverse(Acc), []};
[X | Xs] ->
case F(X) of
false ->
{reverse(Acc), List};
_@1 ->
do_split_while(Xs, F, [X | Acc])
end
end.
-spec split_while(list(AGJ), fun((AGJ) -> boolean())) -> {list(AGJ), list(AGJ)}.
split_while(List, Predicate) ->
do_split_while(List, Predicate, []).
-spec key_find(list({AGN, AGO}), AGN) -> {ok, AGO} | {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(AGW), fun((AGW) -> boolean()), list(AGW)) -> {ok,
{AGW, list(AGW)}} |
{error, nil}.
do_pop(Haystack, Predicate, Checked) ->
case Haystack of
[] ->
{error, nil};
[X | Rest] ->
case Predicate(X) of
true ->
{ok, {X, append(reverse(Checked), Rest)}};
false ->
do_pop(Rest, Predicate, [X | Checked])
end
end.
-spec pop(list(AGW), fun((AGW) -> boolean())) -> {ok, {AGW, list(AGW)}} |
{error, nil}.
pop(Haystack, Is_desired) ->
do_pop(Haystack, Is_desired, []).
-spec do_pop_map(list(AHF), fun((AHF) -> {ok, AHH} | {error, any()}), list(AHF)) -> {ok,
{AHH,
list(AHF)}} |
{error, nil}.
do_pop_map(Haystack, Mapper, Checked) ->
case Haystack of
[] ->
{error, nil};
[X | Rest] ->
case Mapper(X) of
{ok, Y} ->
{ok, {Y, append(reverse(Checked), Rest)}};
{error, _@1} ->
do_pop_map(Rest, Mapper, [X | Checked])
end
end.
-spec pop_map(list(AHF), fun((AHF) -> {ok, AHH} | {error, any()})) -> {ok,
{AHH,
list(AHF)}} |
{error, nil}.
pop_map(Haystack, Is_desired) ->
do_pop_map(Haystack, Is_desired, []).
-spec key_pop(list({AHO, AHP}), AHO) -> {ok, {AHP, list({AHO, AHP})}} |
{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};
_@1 ->
{error, nil}
end
end
).
-spec key_set(list({AHU, AHV}), AHU, AHV) -> list({AHU, AHV}).
key_set(List, Key, Value) ->
case List of
[] ->
[{Key, Value}];
[{K, _@1} | Rest] when K =:= Key ->
[{Key, Value} | Rest];
[First | Rest@1] ->
[First | key_set(Rest@1, Key, Value)]
end.
-spec each(list(AHY), fun((AHY) -> any())) -> nil.
each(List, F) ->
case List of
[] ->
nil;
[X | Xs] ->
F(X),
each(Xs, F)
end.
-spec do_partition(list(AIG), fun((AIG) -> boolean()), list(AIG), list(AIG)) -> {list(AIG),
list(AIG)}.
do_partition(List, Categorise, Trues, Falses) ->
case List of
[] ->
{reverse(Trues), 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(AIG), fun((AIG) -> boolean())) -> {list(AIG), list(AIG)}.
partition(List, Categorise) ->
do_partition(List, Categorise, [], []).
-spec permutations(list(AIK)) -> list(list(AIK)).
permutations(L) ->
case L of
[] ->
[[]];
_@1 ->
_pipe@2 = map(
L,
fun(X) ->
_pipe = filter(L, fun(Y) -> Y /= X end),
_pipe@1 = permutations(_pipe),
map(_pipe@1, fun(_capture) -> append([X], _capture) end)
end
),
flatten(_pipe@2)
end.
-spec do_window(list(list(AIO)), list(AIO), integer()) -> list(list(AIO)).
do_window(Acc, L, N) ->
Window = take(L, N),
case length(Window) =:= N of
true ->
do_window([Window | Acc], drop(L, 1), N);
false ->
Acc
end.
-spec window(list(AIU), integer()) -> list(list(AIU)).
window(L, N) ->
_pipe = do_window([], L, N),
reverse(_pipe).
-spec window_by_2(list(AIY)) -> list({AIY, AIY}).
window_by_2(L) ->
zip(L, drop(L, 1)).
-spec drop_while(list(AJB), fun((AJB) -> boolean())) -> list(AJB).
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(AJE), fun((AJE) -> boolean()), list(AJE)) -> list(AJE).
do_take_while(List, Predicate, Acc) ->
case List of
[] ->
reverse(Acc);
[Head | Tail] ->
case Predicate(Head) of
true ->
do_take_while(Tail, Predicate, [Head | Acc]);
false ->
reverse(Acc)
end
end.
-spec take_while(list(AJI), fun((AJI) -> boolean())) -> list(AJI).
take_while(List, Predicate) ->
do_take_while(List, Predicate, []).
-spec do_chunk(list(AJL), fun((AJL) -> AJN), AJN, list(AJL), list(list(AJL))) -> list(list(AJL)).
do_chunk(List, F, Previous_key, Current_chunk, Acc) ->
case List of
[Head | Tail] ->
Key = F(Head),
case Key =:= Previous_key of
false ->
New_acc = [reverse(Current_chunk) | Acc],
do_chunk(Tail, F, Key, [Head], New_acc);
_@1 ->
do_chunk(Tail, F, Key, [Head | Current_chunk], Acc)
end;
_@2 ->
reverse([reverse(Current_chunk) | Acc])
end.
-spec chunk(list(AJT), fun((AJT) -> any())) -> list(list(AJT)).
chunk(List, F) ->
case List of
[] ->
[];
[Head | Tail] ->
do_chunk(Tail, F, F(Head), [Head], [])
end.
-spec do_sized_chunk(
list(AJY),
integer(),
integer(),
list(AJY),
list(list(AJY))
) -> list(list(AJY)).
do_sized_chunk(List, Count, Left, Current_chunk, Acc) ->
case List of
[] ->
case Current_chunk of
[] ->
reverse(Acc);
Remaining ->
reverse([reverse(Remaining) | Acc])
end;
[Head | Tail] ->
Chunk = [Head | Current_chunk],
case Left > 1 of
false ->
do_sized_chunk(
Tail,
Count,
Count,
[],
[reverse(Chunk) | Acc]
);
true ->
do_sized_chunk(Tail, Count, Left - 1, Chunk, Acc)
end
end.
-spec sized_chunk(list(AKF), integer()) -> list(list(AKF)).
sized_chunk(List, Count) ->
do_sized_chunk(List, Count, Count, [], []).
-spec reduce(list(AKJ), fun((AKJ, AKJ) -> AKJ)) -> {ok, AKJ} | {error, nil}.
reduce(List, Fun) ->
case List of
[] ->
{error, nil};
[Head | Tail] ->
{ok, fold(Tail, Head, Fun)}
end.
-spec do_scan(list(AKN), AKP, list(AKP), fun((AKP, AKN) -> AKP)) -> list(AKP).
do_scan(List, Accumulator, Accumulated, Fun) ->
case List of
[] ->
reverse(Accumulated);
[X | Xs] ->
Next = Fun(Accumulator, X),
do_scan(Xs, Next, [Next | Accumulated], Fun)
end.
-spec scan(list(AKS), AKU, fun((AKU, AKS) -> AKU)) -> list(AKU).
scan(List, Initial, Fun) ->
do_scan(List, Initial, [], Fun).
-spec last(list(AKW)) -> {ok, AKW} | {error, nil}.
last(List) ->
_pipe = List,
reduce(_pipe, fun(_, Elem) -> Elem end).
-spec combinations(list(ALA), integer()) -> list(list(ALA)).
combinations(Items, N) ->
case N of
0 ->
[[]];
_@1 ->
case Items of
[] ->
[];
[X | Xs] ->
First_combinations = begin
_pipe = map(
combinations(Xs, N - 1),
fun(Com) -> [X | Com] end
),
reverse(_pipe)
end,
fold(
First_combinations,
combinations(Xs, N),
fun(Acc, C) -> [C | Acc] end
)
end
end.
-spec do_combination_pairs(list(ALE)) -> list(list({ALE, ALE})).
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(ALI)) -> list({ALI, ALI}).
combination_pairs(Items) ->
_pipe = do_combination_pairs(Items),
flatten(_pipe).
-spec interleave(list(list(ALL))) -> list(ALL).
interleave(List) ->
_pipe = transpose(List),
flatten(_pipe).
-spec transpose(list(list(ALP))) -> list(list(ALP)).
transpose(List_of_list) ->
Take_first = fun(List) -> case List of
[] ->
[];
[F] ->
[F];
[F@1 | _@1] ->
[F@1]
end end,
case List_of_list of
[] ->
[];
[[] | Xss] ->
transpose(Xss);
Rows ->
Firsts = begin
_pipe = Rows,
_pipe@1 = map(_pipe, Take_first),
flatten(_pipe@1)
end,
Rest = transpose(map(Rows, fun(_capture) -> drop(_capture, 1) end)),
[Firsts | Rest]
end.