-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, drop_while/2, take_while/2]). -export_type([length_mismatch/0, continue_or_stop/1]). -type length_mismatch() :: length_mismatch. -type continue_or_stop(ABC) :: {continue, ABC} | {stop, ABC}. -spec length(list(any())) -> integer(). length(A) -> erlang:length(A). -spec reverse(list(ABF)) -> list(ABF). reverse(A) -> lists:reverse(A). -spec is_empty(list(any())) -> boolean(). is_empty(List) -> List =:= []. -spec contains(list(ABK), ABK) -> boolean(). contains(List, Elem) -> case List of [] -> false; [Head | Rest] -> (Head =:= Elem) orelse contains(Rest, Elem) end. -spec head(list(ABM)) -> {ok, ABM} | {error, nil}. head(List) -> case List of [] -> {error, nil}; [X | _] -> {ok, X} end. -spec tail(list(ABQ)) -> {ok, list(ABQ)} | {error, nil}. tail(List) -> case List of [] -> {error, nil}; [_ | Xs] -> {ok, Xs} end. -spec do_filter(list(ABV), fun((ABV) -> boolean()), list(ABV)) -> list(ABV). 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(ABZ), fun((ABZ) -> boolean())) -> list(ABZ). filter(List, Predicate) -> do_filter(List, Predicate, []). -spec do_filter_map( list(ACC), fun((ACC) -> {ok, ACE} | {error, any()}), list(ACE) ) -> list(ACE). 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(ACK), fun((ACK) -> {ok, ACM} | {error, any()})) -> list(ACM). filter_map(List, Fun) -> do_filter_map(List, Fun, []). -spec do_map(list(ACR), fun((ACR) -> ACT), list(ACT)) -> list(ACT). do_map(List, Fun, Acc) -> case List of [] -> lists:reverse(Acc); [X | Xs] -> do_map(Xs, Fun, [Fun(X) | Acc]) end. -spec map(list(ACW), fun((ACW) -> ACY)) -> list(ACY). map(List, Fun) -> do_map(List, Fun, []). -spec do_index_map( list(ADA), fun((integer(), ADA) -> ADC), integer(), list(ADC) ) -> list(ADC). 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(ADF), fun((integer(), ADF) -> ADH)) -> list(ADH). index_map(List, Fun) -> do_index_map(List, Fun, 0, []). -spec do_try_map(list(ADJ), fun((ADJ) -> {ok, ADL} | {error, ADM}), list(ADL)) -> {ok, list(ADL)} | {error, ADM}. 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(ADT), fun((ADT) -> {ok, ADV} | {error, ADW})) -> {ok, list(ADV)} | {error, ADW}. try_map(List, Fun) -> do_try_map(List, Fun, []). -spec drop(list(AEC), integer()) -> list(AEC). drop(List, N) -> case N =< 0 of true -> List; false -> case List of [] -> []; [_ | Xs] -> drop(Xs, N - 1) end end. -spec do_take(list(AEF), integer(), list(AEF)) -> list(AEF). 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(AEJ), integer()) -> list(AEJ). take(List, N) -> do_take(List, N, []). -spec new() -> list(any()). new() -> []. -spec append(list(AEO), list(AEO)) -> list(AEO). append(A, B) -> lists:append(A, B). -spec do_flatten(list(list(AES)), list(AES)) -> list(AES). do_flatten(Lists, Acc) -> case Lists of [] -> Acc; [L | Rest] -> do_flatten(Rest, lists:append(Acc, L)) end. -spec flatten(list(list(AEX))) -> list(AEX). flatten(Lists) -> do_flatten(Lists, []). -spec fold(list(AFB), AFD, fun((AFB, AFD) -> AFD)) -> AFD. fold(List, Initial, Fun) -> case List of [] -> Initial; [X | Rest] -> fold(Rest, Fun(X, Initial), Fun) end. -spec fold_right(list(AFE), AFG, fun((AFE, AFG) -> AFG)) -> AFG. fold_right(List, Initial, Fun) -> case List of [] -> Initial; [X | Rest] -> Fun(X, fold_right(Rest, Initial, Fun)) end. -spec do_index_fold( list(AFH), AFJ, fun((integer(), AFH, AFJ) -> AFJ), integer() ) -> AFJ. 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(AFK), AFM, fun((integer(), AFK, AFM) -> AFM)) -> AFM. index_fold(Over, Initial, Fun) -> do_index_fold(Over, Initial, Fun, 0). -spec try_fold(list(AFN), AFP, fun((AFN, AFP) -> {ok, AFP} | {error, AFQ})) -> {ok, AFP} | {error, AFQ}. 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(AFV), AFX, fun((AFV, AFX) -> continue_or_stop(AFX))) -> AFX. 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(AFZ), fun((AFZ) -> boolean())) -> {ok, AFZ} | {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(AGD), fun((AGD) -> {ok, AGF} | {error, any()})) -> {ok, AGF} | {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(AGL), fun((AGL) -> 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(AGN), fun((AGN) -> 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(AGP), list(AGR)) -> list({AGP, AGR}). 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(AGU), list(AGW)) -> {ok, list({AGU, AGW})} | {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({AHF, AHG}), list(AHF), list(AHG)) -> {list(AHF), list(AHG)}. 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({AHF, AHG})) -> {list(AHF), list(AHG)}. unzip(Input) -> do_unzip(Input, [], []). -spec intersperse(list(AHK), AHK) -> list(AHK). intersperse(List, Elem) -> case List of [] -> List; [_] -> List; [X | Rest] -> [X, Elem | intersperse(Rest, Elem)] end. -spec at(list(AHN), integer()) -> {ok, AHN} | {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(AHR)) -> list(AHR). unique(List) -> case List of [] -> []; [X | Rest] -> [X | unique(filter(Rest, fun(Y) -> Y /= X end))] end. -spec merge_sort(list(AHU), list(AHU), fun((AHU, AHU) -> gleam@order:order())) -> list(AHU). 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(AHY), fun((AHY, AHY) -> gleam@order:order()), integer()) -> list(AHY). 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(AIB), fun((AIB, AIB) -> gleam@order:order())) -> list(AIB). 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(AIF, integer(), list(AIF)) -> list(AIF). do_repeat(A, Times, Acc) -> case Times =< 0 of true -> Acc; false -> do_repeat(A, Times - 1, [A | Acc]) end. -spec repeat(AII, integer()) -> list(AII). repeat(A, Times) -> do_repeat(A, Times, []). -spec do_split(list(AIK), integer(), list(AIK)) -> {list(AIK), list(AIK)}. 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(AIP), integer()) -> {list(AIP), list(AIP)}. split(List, Index) -> do_split(List, Index, []). -spec do_split_while(list(AIT), fun((AIT) -> boolean()), list(AIT)) -> {list(AIT), list(AIT)}. 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(AIY), fun((AIY) -> boolean())) -> {list(AIY), list(AIY)}. split_while(List, Predicate) -> do_split_while(List, Predicate, []). -spec key_find(list({AJC, AJD}), AJC) -> {ok, AJD} | {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(AJL), fun((AJL) -> boolean()), list(AJL)) -> {ok, {AJL, list(AJL)}} | {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(AJL), fun((AJL) -> boolean())) -> {ok, {AJL, list(AJL)}} | {error, nil}. pop(Haystack, Is_desired) -> do_pop(Haystack, Is_desired, []). -spec do_pop_map(list(AJU), fun((AJU) -> {ok, AJW} | {error, any()}), list(AJU)) -> {ok, {AJW, list(AJU)}} | {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(AJU), fun((AJU) -> {ok, AJW} | {error, any()})) -> {ok, {AJW, list(AJU)}} | {error, nil}. pop_map(Haystack, Is_desired) -> do_pop_map(Haystack, Is_desired, []). -spec key_pop(list({AKD, AKE}), AKD) -> {ok, {AKE, list({AKD, AKE})}} | {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({AKJ, AKK}), AKJ, AKK) -> list({AKJ, AKK}). 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(AKN), fun((AKN) -> any())) -> nil. each(List, F) -> case List of [] -> nil; [X | Xs] -> F(X), each(Xs, F) end. -spec do_partition(list(AKV), fun((AKV) -> boolean()), list(AKV), list(AKV)) -> {list(AKV), list(AKV)}. 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(AKV), fun((AKV) -> boolean())) -> {list(AKV), list(AKV)}. partition(List, Categorise) -> do_partition(List, Categorise, [], []). -spec permutations(list(AKZ)) -> list(list(AKZ)). 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(ALD)), list(ALD), integer()) -> list(list(ALD)). 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(ALJ), integer()) -> list(list(ALJ)). window(L, N) -> lists:reverse(do_window([], L, N)). -spec window_by_2(list(ALN)) -> list({ALN, ALN}). window_by_2(L) -> zip(L, drop(L, 1)). -spec drop_while(list(ALQ), fun((ALQ) -> boolean())) -> list(ALQ). 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(ALT), fun((ALT) -> boolean()), list(ALT)) -> list(ALT). 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(ALX), fun((ALX) -> boolean())) -> list(ALX). take_while(List, Predicate) -> do_take_while(List, Predicate, []).