-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, index_map/2, try_map/2, drop/2, take/2, new/0, append/2, flatten/1, 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]). -export_type([length_mismatch/0, continue_or_stop/1]). -type length_mismatch() :: length_mismatch. -type continue_or_stop(VW) :: {continue, VW} | {stop, VW}. -spec length(list(any())) -> integer(). length(A) -> erlang:length(A). -spec reverse(list(VZ)) -> list(VZ). reverse(A) -> lists:reverse(A). -spec is_empty(list(any())) -> boolean(). is_empty(List) -> List =:= []. -spec contains(list(WE), WE) -> boolean(). contains(List, Elem) -> case List of [] -> false; [Head | Rest] -> (Head =:= Elem) orelse contains(Rest, Elem) end. -spec head(list(WG)) -> {ok, WG} | {error, nil}. head(List) -> case List of [] -> {error, nil}; [X | _] -> {ok, X} end. -spec tail(list(WK)) -> {ok, list(WK)} | {error, nil}. tail(List) -> case List of [] -> {error, nil}; [_ | Xs] -> {ok, Xs} end. -spec do_filter(list(WP), fun((WP) -> boolean()), list(WP)) -> list(WP). 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(WT), fun((WT) -> boolean())) -> list(WT). filter(List, Predicate) -> do_filter(List, Predicate, []). -spec do_filter_map(list(WW), fun((WW) -> {ok, WY} | {error, any()}), list(WY)) -> list(WY). 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(XE), fun((XE) -> {ok, XG} | {error, any()})) -> list(XG). filter_map(List, Fun) -> do_filter_map(List, Fun, []). -spec do_map(list(XL), fun((XL) -> XN), list(XN)) -> list(XN). do_map(List, Fun, Acc) -> case List of [] -> lists:reverse(Acc); [X | Xs] -> do_map(Xs, Fun, [Fun(X) | Acc]) end. -spec map(list(XQ), fun((XQ) -> XS)) -> list(XS). map(List, Fun) -> do_map(List, Fun, []). -spec do_index_map(list(XU), fun((integer(), XU) -> XW), integer(), list(XW)) -> list(XW). 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(XZ), fun((integer(), XZ) -> YB)) -> list(YB). index_map(List, Fun) -> do_index_map(List, Fun, 0, []). -spec do_try_map(list(YD), fun((YD) -> {ok, YF} | {error, YG}), list(YF)) -> {ok, list(YF)} | {error, YG}. 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(YN), fun((YN) -> {ok, YP} | {error, YQ})) -> {ok, list(YP)} | {error, YQ}. try_map(List, Fun) -> do_try_map(List, Fun, []). -spec drop(list(YW), integer()) -> list(YW). drop(List, N) -> case N =< 0 of true -> List; false -> case List of [] -> []; [_ | Xs] -> drop(Xs, N - 1) end end. -spec do_take(list(YZ), integer(), list(YZ)) -> list(YZ). 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(AAD), integer()) -> list(AAD). take(List, N) -> do_take(List, N, []). -spec new() -> list(any()). new() -> []. -spec append(list(AAI), list(AAI)) -> list(AAI). append(A, B) -> lists:append(A, B). -spec do_flatten(list(list(AAM)), list(AAM)) -> list(AAM). do_flatten(Lists, Acc) -> case Lists of [] -> Acc; [L | Rest] -> do_flatten(Rest, lists:append(Acc, L)) end. -spec flatten(list(list(AAR))) -> list(AAR). flatten(Lists) -> do_flatten(Lists, []). -spec fold(list(AAV), AAX, fun((AAV, AAX) -> AAX)) -> AAX. fold(List, Initial, Fun) -> case List of [] -> Initial; [X | Rest] -> fold(Rest, Fun(X, Initial), Fun) end. -spec fold_right(list(AAY), ABA, fun((AAY, ABA) -> ABA)) -> ABA. fold_right(List, Initial, Fun) -> case List of [] -> Initial; [X | Rest] -> Fun(X, fold_right(Rest, Initial, Fun)) end. -spec do_index_fold( list(ABB), ABD, fun((integer(), ABB, ABD) -> ABD), integer() ) -> ABD. 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(ABE), ABG, fun((integer(), ABE, ABG) -> ABG)) -> ABG. index_fold(Over, Initial, Fun) -> do_index_fold(Over, Initial, Fun, 0). -spec try_fold(list(ABH), ABJ, fun((ABH, ABJ) -> {ok, ABJ} | {error, ABK})) -> {ok, ABJ} | {error, ABK}. 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(ABP), ABR, fun((ABP, ABR) -> continue_or_stop(ABR))) -> ABR. 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(ABT), fun((ABT) -> boolean())) -> {ok, ABT} | {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(ABX), fun((ABX) -> {ok, ABZ} | {error, any()})) -> {ok, ABZ} | {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(ACF), fun((ACF) -> boolean())) -> boolean(). all(List, Predicate) -> case List of [] -> true; [X | Rest] -> case Predicate(X) of true -> all(Rest, Predicate); _ -> false end end. -spec any(list(ACH), fun((ACH) -> boolean())) -> boolean(). any(List, Predicate) -> case List of [] -> false; [X | Rest] -> case Predicate(X) of false -> any(Rest, Predicate); _ -> true end end. -spec zip(list(ACJ), list(ACL)) -> list({ACJ, ACL}). zip(Xs, Ys) -> case {Xs, Ys} of {[], _} -> []; {_, []} -> []; {[X | Xs@1], [Y | Ys@1]} -> [{X, Y} | zip(Xs@1, Ys@1)] end. -spec strict_zip(list(ACO), list(ACQ)) -> {ok, list({ACO, ACQ})} | {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({ACZ, ADA}), list(ACZ), list(ADA)) -> {list(ACZ), list(ADA)}. 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({ACZ, ADA})) -> {list(ACZ), list(ADA)}. unzip(Input) -> do_unzip(Input, [], []). -spec intersperse(list(ADE), ADE) -> list(ADE). intersperse(List, Elem) -> case List of [] -> List; [_] -> List; [X | Rest] -> [X, Elem | intersperse(Rest, Elem)] end. -spec at(list(ADH), integer()) -> {ok, ADH} | {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(ADL)) -> list(ADL). unique(List) -> case List of [] -> []; [X | Rest] -> [X | unique(filter(Rest, fun(Y) -> Y /= X end))] end. -spec merge_sort(list(ADO), list(ADO), fun((ADO, ADO) -> gleam@order:order())) -> list(ADO). 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(ADS), fun((ADS, ADS) -> gleam@order:order()), integer()) -> list(ADS). 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(ADV), fun((ADV, ADV) -> gleam@order:order())) -> list(ADV). 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(ADZ, integer(), list(ADZ)) -> list(ADZ). do_repeat(A, Times, Acc) -> case Times =< 0 of true -> Acc; false -> do_repeat(A, Times - 1, [A | Acc]) end. -spec repeat(AEC, integer()) -> list(AEC). repeat(A, Times) -> do_repeat(A, Times, []). -spec do_split(list(AEE), integer(), list(AEE)) -> {list(AEE), list(AEE)}. 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(AEJ), integer()) -> {list(AEJ), list(AEJ)}. split(List, Index) -> do_split(List, Index, []). -spec do_split_while(list(AEN), fun((AEN) -> boolean()), list(AEN)) -> {list(AEN), list(AEN)}. 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(AES), fun((AES) -> boolean())) -> {list(AES), list(AES)}. split_while(List, Predicate) -> do_split_while(List, Predicate, []). -spec key_find(list({AEW, AEX}), AEW) -> {ok, AEX} | {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(AFF), fun((AFF) -> boolean()), list(AFF)) -> {ok, {AFF, list(AFF)}} | {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(AFF), fun((AFF) -> boolean())) -> {ok, {AFF, list(AFF)}} | {error, nil}. pop(Haystack, Is_desired) -> do_pop(Haystack, Is_desired, []). -spec do_pop_map(list(AFO), fun((AFO) -> {ok, AFQ} | {error, any()}), list(AFO)) -> {ok, {AFQ, list(AFO)}} | {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(AFO), fun((AFO) -> {ok, AFQ} | {error, any()})) -> {ok, {AFQ, list(AFO)}} | {error, nil}. pop_map(Haystack, Is_desired) -> do_pop_map(Haystack, Is_desired, []). -spec key_pop(list({AFX, AFY}), AFX) -> {ok, {AFY, list({AFX, AFY})}} | {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({AGD, AGE}), AGD, AGE) -> list({AGD, AGE}). 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(AGH), fun((AGH) -> any())) -> nil. each(List, F) -> case List of [] -> nil; [X | Xs] -> F(X), each(Xs, F) end. -spec do_partition(list(AGP), fun((AGP) -> boolean()), list(AGP), list(AGP)) -> {list(AGP), list(AGP)}. 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(AGP), fun((AGP) -> boolean())) -> {list(AGP), list(AGP)}. partition(List, Categorise) -> do_partition(List, Categorise, [], []). -spec permutations(list(AGT)) -> list(list(AGT)). permutations(L) -> case L of [] -> [[]]; _ -> flatten( map( L, fun(X) -> map( permutations(filter(L, fun(Y) -> Y /= X end)), fun(Gleam@capture_variable) -> lists:append([X], Gleam@capture_variable) end ) end ) ) end. -spec do_window(list(list(AGX)), list(AGX), integer()) -> list(list(AGX)). 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(AHD), integer()) -> list(list(AHD)). window(L, N) -> lists:reverse(do_window([], L, N)). -spec window_by_2(list(AHH)) -> list({AHH, AHH}). window_by_2(L) -> zip(L, drop(L, 1)).