Current section

Files

Jump to
non_empty_list src non_empty_list.erl
Raw

src/non_empty_list.erl

-module(non_empty_list).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/non_empty_list.gleam").
-export([first/1, last/1, length/1, new/2, append_list/2, from_list/1, index_map/2, intersperse/2, map/2, prepend/2, reduce/2, rest/1, single/1, to_list/1, append/2, drop/2, group/2, reverse/1, all/1, flatten/1, flat_map/2, map2/3, map_fold/3, scan/3, shuffle/1, sort/2, take/2, unique/1, unzip/1, zip/2, strict_zip/2]).
-export_type([non_empty_list/1]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-type non_empty_list(DQN) :: {non_empty_list, DQN, list(DQN)}.
-file("src/non_empty_list.gleam", 110).
?DOC(
" Gets the first element from the start of the non-empty list.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" assert first(new(1, [2, 3, 4])) == 1\n"
" ```\n"
).
-spec first(non_empty_list(DRO)) -> DRO.
first(List) ->
erlang:element(2, List).
-file("src/non_empty_list.gleam", 241).
-spec do_index_map(
list(DSX),
list(DSZ),
integer(),
fun((integer(), DSX) -> DSZ)
) -> list(DSZ).
do_index_map(List, Accumulator, Index, Fun) ->
case List of
[] ->
lists:reverse(Accumulator);
[First | Rest] ->
do_index_map(
Rest,
[Fun(Index, First) | Accumulator],
Index + 1,
Fun
)
end.
-file("src/non_empty_list.gleam", 286).
?DOC(
" Returns the last element in the given list.\n"
"\n"
" This function runs in linear time.\n"
" For a collection oriented around performant access at either end,\n"
" see `gleam/queue.Queue`.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" assert last(single(1)) == 1\n"
" assert last(new(1, [2, 3, 4])) == 4\n"
" ```\n"
).
-spec last(non_empty_list(DTF)) -> DTF.
last(List) ->
_pipe = gleam@list:last(erlang:element(3, List)),
gleam@result:unwrap(_pipe, erlang:element(2, List)).
-file("src/non_empty_list.gleam", 303).
?DOC(
" Counts the number of elements in a given list.\n"
"\n"
" This function has to traverse the list to determine the number of elements,\n"
" so it runs in linear time.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" assert length(single(0)) == 1\n"
" assert length(new(0, [1])) == 2\n"
" ```\n"
).
-spec length(non_empty_list(any())) -> integer().
length(List) ->
case erlang:element(3, List) of
[] ->
1;
Rest ->
1 + erlang:length(Rest)
end.
-file("src/non_empty_list.gleam", 390).
?DOC(
" Creates a new non-empty list given its first element and a list\n"
" for the rest of the elements.\n"
).
-spec new(DUF, list(DUF)) -> non_empty_list(DUF).
new(First, Rest) ->
{non_empty_list, First, Rest}.
-file("src/non_empty_list.gleam", 82).
?DOC(
" Joins a list onto the end of a non-empty list.\n"
"\n"
" This function runs in linear time, and it traverses and copies the first non-empty list.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" assert new(1, [2, 3, 4])\n"
" |> append_list([5, 6, 7])\n"
" == new(1, [2, 3, 4, 5, 6, 7])\n"
"\n"
" assert new(\"a\", [\"b\", \"c\"])\n"
" |> append_list([])\n"
" == new(\"a\", [\"b\", \"c\"])\n"
" ```\n"
).
-spec append_list(non_empty_list(DRH), list(DRH)) -> non_empty_list(DRH).
append_list(First, Second) ->
new(
erlang:element(2, First),
lists:append(erlang:element(3, First), Second)
).
-file("src/non_empty_list.gleam", 182).
?DOC(
" Attempts to turn a list into a non-empty list, fails if the starting\n"
" list is empty.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" assert from_list([1, 2, 3, 4]) == Ok(new(1, [2, 3, 4]))\n"
" assert from_list([\"a\"]) == Ok(single(\"a\"))\n"
" assert from_list([]) == Error(Nil)\n"
" ```\n"
).
-spec from_list(list(DSI)) -> {ok, non_empty_list(DSI)} | {error, nil}.
from_list(List) ->
case List of
[] ->
{error, nil};
[First | Rest] ->
{ok, new(First, Rest)}
end.
-file("src/non_empty_list.gleam", 234).
?DOC(
" Returns a new list containing only the elements of the first list after the\n"
" function has been applied to each one and their index.\n"
"\n"
" The index starts at 0, so the first element is 0, the second is 1, and so on.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" assert new(\"a\", [\"b\", \"c\"])\n"
" |> index_map(fn(index, letter) { #(index, letter) })\n"
" == new(#(0, \"a\"), [#(1, \"b\"), #(2, \"c\")])\n"
" ```\n"
).
-spec index_map(non_empty_list(DST), fun((integer(), DST) -> DSV)) -> non_empty_list(DSV).
index_map(List, Fun) ->
new(
Fun(0, erlang:element(2, List)),
do_index_map(erlang:element(3, List), [], 1, Fun)
).
-file("src/non_empty_list.gleam", 265).
?DOC(
" Inserts a given value between each existing element in a given list.\n"
"\n"
" This function runs in linear time and copies the list.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" assert new(1, [2, 3]) |> intersperse(with: 0) == new(1, [0, 2, 0, 3])\n"
" assert single(\"a\") |> intersperse(with: \"z\") == single(\"a\")\n"
" ```\n"
).
-spec intersperse(non_empty_list(DTC), DTC) -> non_empty_list(DTC).
intersperse(List, Elem) ->
case List of
{non_empty_list, _, []} ->
List;
{non_empty_list, First, [_ | _] = Rest} ->
new(First, [Elem | gleam@list:intersperse(Rest, Elem)])
end.
-file("src/non_empty_list.gleam", 319).
?DOC(
" Returns a new non-empty list containing only the elements of the first\n"
" non-empty list after the function has been applied to each one.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" assert new(1, [2, 3]) |> map(fn(x) { x + 1 }) == new(2, [3, 4])\n"
" ```\n"
).
-spec map(non_empty_list(DTJ), fun((DTJ) -> DTL)) -> non_empty_list(DTL).
map(List, Fun) ->
new(
Fun(erlang:element(2, List)),
gleam@list:map(erlang:element(3, List), Fun)
).
-file("src/non_empty_list.gleam", 402).
?DOC(
" Prefixes an item to a non-empty list.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" assert new(2, [3, 4]) |> prepend(1) == new(1, [2, 3, 4])\n"
" ```\n"
).
-spec prepend(non_empty_list(DUI), DUI) -> non_empty_list(DUI).
prepend(List, Item) ->
new(Item, [erlang:element(2, List) | erlang:element(3, List)]).
-file("src/non_empty_list.gleam", 417).
?DOC(
" This function acts similar to fold, but does not take an initial state.\n"
" Instead, it starts from the first element in the non-empty list and combines it with each\n"
" subsequent element in turn using the given function.\n"
" The function is called as `fun(accumulator, current_element)`.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" assert new(1, [2, 3, 4]) |> reduce(fn(acc, x) { acc + x }) == 10\n"
" ```\n"
).
-spec reduce(non_empty_list(DUL), fun((DUL, DUL) -> DUL)) -> DUL.
reduce(List, Fun) ->
gleam@list:fold(erlang:element(3, List), erlang:element(2, List), Fun).
-file("src/non_empty_list.gleam", 431).
?DOC(
" Returns the list minus the first element. Since the remaining list could\n"
" be empty this functions returns a normal list.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" assert new(1, [2, 3, 4]) |> rest == [2, 3, 4]\n"
" assert single(1) |> rest == []\n"
" ```\n"
).
-spec rest(non_empty_list(DUN)) -> list(DUN).
rest(List) ->
erlang:element(3, List).
-file("src/non_empty_list.gleam", 508).
?DOC(
" Creates a non-empty list with a single element.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" assert single(1) == new(1, [])\n"
" ```\n"
).
-spec single(DVA) -> non_empty_list(DVA).
single(First) ->
new(First, []).
-file("src/non_empty_list.gleam", 591).
?DOC(
" Turns a non-empty list back into a normal list with the same\n"
" elements.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" assert new(1, [2, 3, 4]) |> to_list == [1, 2, 3, 4]\n"
" assert single(\"a\") |> to_list == [\"a\"]\n"
" ```\n"
).
-spec to_list(non_empty_list(DVP)) -> list(DVP).
to_list(Non_empty) ->
[erlang:element(2, Non_empty) | erlang:element(3, Non_empty)].
-file("src/non_empty_list.gleam", 59).
?DOC(
" Joins a non-empty list onto the end of a non-empty list.\n"
"\n"
" This function runs in linear time, and it traverses and copies the first\n"
" non-empty list.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" assert new(1, [2, 3, 4])\n"
" |> append(new(5, [6, 7]))\n"
" == new(1, [2, 3, 4, 5, 6, 7])\n"
"\n"
" assert single(\"a\")\n"
" |> append(new(\"b\", [\"c\"])\n"
" == new(\"a\", [\"b\", \"c\"])\n"
" ````\n"
).
-spec append(non_empty_list(DRD), non_empty_list(DRD)) -> non_empty_list(DRD).
append(First, Second) ->
new(
erlang:element(2, First),
lists:append(erlang:element(3, First), to_list(Second))
).
-file("src/non_empty_list.gleam", 96).
?DOC(
" Returns a list that is the given non-empty list with up to the given\n"
" number of elements removed from the front of the list.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" assert new(\"a\", [\"b\", \"c\"]) |> drop(up_to: 2) == [\"c\"]\n"
" assert new(\"a\", [\"b\", \"c\"]) |> drop(up_to: 3) == []\n"
" ```\n"
).
-spec drop(non_empty_list(DRL), integer()) -> list(DRL).
drop(List, N) ->
_pipe = List,
_pipe@1 = to_list(_pipe),
gleam@list:drop(_pipe@1, N).
-file("src/non_empty_list.gleam", 160).
-spec reverse_and_prepend(non_empty_list(DSE), non_empty_list(DSE)) -> non_empty_list(DSE).
reverse_and_prepend(Prefix, Suffix) ->
case erlang:element(3, Prefix) of
[] ->
new(erlang:element(2, Prefix), to_list(Suffix));
[First | Rest] ->
reverse_and_prepend(
new(First, Rest),
new(erlang:element(2, Prefix), to_list(Suffix))
)
end.
-file("src/non_empty_list.gleam", 208).
?DOC(
" Takes a list and groups the values by a key\n"
" which is built from a key function.\n"
"\n"
" Does not preserve the initial value order.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" import gleam/dict\n"
"\n"
" assert new(1, [2, 3, 4, 5])\n"
" |> group(by: fn(i) { i - i / 3 * 3 })\n"
" == dict.from_list([\n"
" #(0, new(3, [])),\n"
" #(1, new(4, [1])),\n"
" #(2, new(5, [2]))\n"
" ]\n"
" ```\n"
).
-spec group(non_empty_list(DSN), fun((DSN) -> DSP)) -> gleam@dict:dict(DSP, non_empty_list(DSN)).
group(List, Key) ->
_pipe = List,
_pipe@1 = to_list(_pipe),
_pipe@2 = gleam@list:group(_pipe@1, Key),
gleam@dict:map_values(
_pipe@2,
fun(_, Group) ->
Group@2 = case from_list(Group) of
{ok, Group@1} -> Group@1;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"non_empty_list"/utf8>>,
function => <<"group"/utf8>>,
line => 216,
value => _assert_fail,
start => 5235,
'end' => 5274,
pattern_start => 5246,
pattern_end => 5255})
end,
Group@2
end
).
-file("src/non_empty_list.gleam", 447).
?DOC(
" Creates a new non-empty list from a given non-empty list containing the same\n"
" elements but in the opposite order.\n"
"\n"
" This function has to traverse the non-empty list to create the new reversed\n"
" non-empty list, so it runs in linear time.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" assert new(1, [2, 3, 4]) |> reverse == new(4, [3, 2, 1])\n"
" ```\n"
).
-spec reverse(non_empty_list(DUQ)) -> non_empty_list(DUQ).
reverse(List) ->
Reversed@1 = case begin
_pipe = List,
_pipe@1 = to_list(_pipe),
_pipe@2 = lists:reverse(_pipe@1),
from_list(_pipe@2)
end of
{ok, Reversed} -> Reversed;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"non_empty_list"/utf8>>,
function => <<"reverse"/utf8>>,
line => 448,
value => _assert_fail,
start => 11338,
'end' => 11424,
pattern_start => 11349,
pattern_end => 11361})
end,
Reversed@1.
-file("src/non_empty_list.gleam", 34).
-spec all_loop(list({ok, DQW} | {error, DQX}), DQW, list(DQW)) -> {ok,
non_empty_list(DQW)} |
{error, DQX}.
all_loop(Results, Acc_first, Acc_rest) ->
case Results of
[{error, Error} | _] ->
{error, Error};
[{ok, Value} | Rest] ->
all_loop(Rest, Value, [Acc_first | Acc_rest]);
[] ->
{ok, reverse(new(Acc_first, Acc_rest))}
end.
-file("src/non_empty_list.gleam", 27).
?DOC(
" Combines a `NonEmptyList` of `Result`s into a single `Result`. If all\n"
" elements in the list are `Ok` then returns an `Ok` holding the list of\n"
" values. If any element is `Error` then returns the first error.\n"
"\n"
" This function runs in linear time, and it traverses and copies the `Ok`\n"
" values or the `Error` value.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" assert all(new(Ok(1), [Ok(2)])) == Ok(new(1, [2]))\n"
" assert all(new(Ok(1), [Error(\"e\")])) == Error(\"e\")\n"
" ```\n"
).
-spec all(non_empty_list({ok, DQO} | {error, DQP})) -> {ok, non_empty_list(DQO)} |
{error, DQP}.
all(Results) ->
case first(Results) of
{ok, Value} ->
all_loop(rest(Results), Value, []);
{error, Error} ->
{error, Error}
end.
-file("src/non_empty_list.gleam", 149).
-spec do_flatten(list(non_empty_list(DRZ)), non_empty_list(DRZ)) -> non_empty_list(DRZ).
do_flatten(Lists, Accumulator) ->
case Lists of
[] ->
reverse(Accumulator);
[List | Further_lists] ->
do_flatten(Further_lists, reverse_and_prepend(List, Accumulator))
end.
-file("src/non_empty_list.gleam", 145).
?DOC(
" Flattens a non-empty list of non-empty lists into a single non-empty list.\n"
"\n"
" This function traverses all elements twice.\n"
"\n"
" ### Examples\n"
"\n"
" ```gleam\n"
" assert new(new(1, [2, 3]), [new(3, [4, 5])])\n"
" |> flatten\n"
" == new(1, [2, 3, 4, 5])\n"
" ```\n"
).
-spec flatten(non_empty_list(non_empty_list(DRV))) -> non_empty_list(DRV).
flatten(Lists) ->
do_flatten(erlang:element(3, Lists), reverse(erlang:element(2, Lists))).
-file("src/non_empty_list.gleam", 124).
?DOC(
" Maps the non-empty list with the given function and then flattens it.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" assert new(1, [3, 5])\n"
" |> flat_map(fn(x) { new(x, [x + 1]) })\n"
" == new(1, [2, 3, 4, 5, 6])\n"
" ```\n"
).
-spec flat_map(non_empty_list(DRQ), fun((DRQ) -> non_empty_list(DRS))) -> non_empty_list(DRS).
flat_map(List, Fun) ->
_pipe = List,
_pipe@1 = map(_pipe, Fun),
flatten(_pipe@1).
-file("src/non_empty_list.gleam", 345).
-spec do_map2(non_empty_list(DTT), list(DTV), list(DTX), fun((DTV, DTX) -> DTT)) -> non_empty_list(DTT).
do_map2(Acc, List1, List2, Fun) ->
case {List1, List2} of
{[], _} ->
reverse(Acc);
{_, []} ->
reverse(Acc);
{[First_a | Rest_as], [First_b | Rest_bs]} ->
_pipe = prepend(Acc, Fun(First_a, First_b)),
do_map2(_pipe, Rest_as, Rest_bs, Fun)
end.
-file("src/non_empty_list.gleam", 337).
?DOC(
" Combines two non-empty lists into a single non-empty list using the given\n"
" function.\n"
"\n"
" If a list is longer than the other the extra elements are dropped.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" assert map2(new(1, [2, 3]), new(4, [5, 6]), fn(x, y) { x + y })\n"
" == new(5, [7, 9])\n"
" assert map2(new(1, [2]), new(\"a\", [\"b\", \"c\"]), fn(i, x) { #(i, x) })\n"
" == new(#(1, \"a\"), [#(2, \"b\")])\n"
" ```\n"
).
-spec map2(non_empty_list(DTN), non_empty_list(DTP), fun((DTN, DTP) -> DTR)) -> non_empty_list(DTR).
map2(List1, List2, Fun) ->
do_map2(
single(Fun(erlang:element(2, List1), erlang:element(2, List2))),
erlang:element(3, List1),
erlang:element(3, List2),
Fun
).
-file("src/non_empty_list.gleam", 369).
?DOC(
" Similar to `map` but also lets you pass around an accumulated value.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" assert new(1, [2, 3])\n"
" |> map_fold(from: 100, with: fn(memo, n) { #(memo + i, i * 2) })\n"
" == #(106, new(2, [4, 6]))\n"
" ```\n"
).
-spec map_fold(non_empty_list(DUA), DUC, fun((DUC, DUA) -> {DUC, DUD})) -> {DUC,
non_empty_list(DUD)}.
map_fold(List, Acc, Fun) ->
{Acc@1, First_elem} = Fun(Acc, erlang:element(2, List)),
_pipe = gleam@list:fold(
erlang:element(3, List),
{Acc@1, single(First_elem)},
fun(Acc_non_empty, Item) ->
{Acc@2, Non_empty} = Acc_non_empty,
{Acc@3, New_item} = Fun(Acc@2, Item),
{Acc@3, prepend(Non_empty, New_item)}
end
),
gleam@pair:map_second(_pipe, fun reverse/1).
-file("src/non_empty_list.gleam", 466).
?DOC(
" Similar to fold, but yields the state of the accumulator at each stage.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" assert new(1, [2, 3, 4])\n"
" |> scan(from: 100, with: fn(acc, i) { acc + i })\n"
" == new(101 [103, 106, 110])\n"
" ```\n"
).
-spec scan(non_empty_list(DUT), DUV, fun((DUV, DUT) -> DUV)) -> non_empty_list(DUV).
scan(List, Initial, Fun) ->
Scanned@1 = case begin
_pipe = List,
_pipe@1 = to_list(_pipe),
_pipe@2 = gleam@list:scan(_pipe@1, Initial, Fun),
from_list(_pipe@2)
end of
{ok, Scanned} -> Scanned;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"non_empty_list"/utf8>>,
function => <<"scan"/utf8>>,
line => 471,
value => _assert_fail,
start => 11796,
'end' => 11904,
pattern_start => 11807,
pattern_end => 11818})
end,
Scanned@1.
-file("src/non_empty_list.gleam", 491).
?DOC(
" Takes a non-empty list, randomly sorts all items and returns the shuffled\n"
" non-empty list.\n"
"\n"
" This function uses Erlang's `:rand` module or Javascript's\n"
" `Math.random()` to calcuate the index shuffling.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" assert new(\"a\", [\"b\", \"c\", \"d\"]) |> shuffle == new(\"c\", [\"a\", \"d\", \"b\"])\n"
" ```\n"
).
-spec shuffle(non_empty_list(DUX)) -> non_empty_list(DUX).
shuffle(List) ->
Shuffled@1 = case begin
_pipe = List,
_pipe@1 = to_list(_pipe),
_pipe@2 = gleam@list:shuffle(_pipe@1),
from_list(_pipe@2)
end of
{ok, Shuffled} -> Shuffled;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"non_empty_list"/utf8>>,
function => <<"shuffle"/utf8>>,
line => 492,
value => _assert_fail,
start => 12323,
'end' => 12409,
pattern_start => 12334,
pattern_end => 12346})
end,
Shuffled@1.
-file("src/non_empty_list.gleam", 523).
?DOC(
" Sorts a given non-empty list from smallest to largest based upon the\n"
" ordering specified by a given function.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" import gleam/int\n"
" assert new(4, [1, 3, 4, 2, 6, 5]) |> sort(by: int.compare)\n"
" == new(1, [2, 3, 4, 4, 5, 6])\n"
" ```\n"
).
-spec sort(non_empty_list(DVC), fun((DVC, DVC) -> gleam@order:order())) -> non_empty_list(DVC).
sort(List, Compare) ->
Sorted@1 = case begin
_pipe = List,
_pipe@1 = to_list(_pipe),
_pipe@2 = gleam@list:sort(_pipe@1, Compare),
from_list(_pipe@2)
end of
{ok, Sorted} -> Sorted;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"non_empty_list"/utf8>>,
function => <<"sort"/utf8>>,
line => 527,
value => _assert_fail,
start => 13007,
'end' => 13101,
pattern_start => 13018,
pattern_end => 13028})
end,
Sorted@1.
-file("src/non_empty_list.gleam", 575).
?DOC(
" Returns a list containing the first given number of elements from the given\n"
" non-empty list.\n"
"\n"
" If the element has less than the number of elements then the full list is\n"
" returned.\n"
"\n"
" This function runs in linear time but does not copy the list.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" assert new(1, [2, 3, 4]) |> take(2) == [1, 2]\n"
" assert new(1, [2, 3, 4]) |> take(9) == [1, 2, 3, 4]\n"
" ```\n"
).
-spec take(non_empty_list(DVM), integer()) -> list(DVM).
take(List, N) ->
_pipe = List,
_pipe@1 = to_list(_pipe),
gleam@list:take(_pipe@1, N).
-file("src/non_empty_list.gleam", 605).
?DOC(
" Removes any duplicate elements from a given list.\n"
"\n"
" This function returns in loglinear time.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" assert new(1, [1, 2, 3, 1, 4, 4, 3]) |> unique == new(1, [2, 3, 4])\n"
" ```\n"
).
-spec unique(non_empty_list(DVS)) -> non_empty_list(DVS).
unique(List) ->
Unique@1 = case begin
_pipe = List,
_pipe@1 = to_list(_pipe),
_pipe@2 = gleam@list:unique(_pipe@1),
from_list(_pipe@2)
end of
{ok, Unique} -> Unique;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"non_empty_list"/utf8>>,
function => <<"unique"/utf8>>,
line => 606,
value => _assert_fail,
start => 14983,
'end' => 15066,
pattern_start => 14994,
pattern_end => 15004})
end,
Unique@1.
-file("src/non_empty_list.gleam", 624).
?DOC(
" Takes a single non-empty list of 2-element tuples and returns two\n"
" non-empty lists.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" assert new(#(1, \"a\"), [#(2, \"b\"), #(3, \"c\")]) |> unzip\n"
" == #(new(1, [2, 3]), new(\"a\", [\"b\", \"c\"]))\n"
" ```\n"
).
-spec unzip(non_empty_list({DVV, DVW})) -> {non_empty_list(DVV),
non_empty_list(DVW)}.
unzip(List) ->
_pipe = gleam@list:unzip(erlang:element(3, List)),
_pipe@1 = gleam@pair:map_first(
_pipe,
fun(_capture) ->
new(erlang:element(1, erlang:element(2, List)), _capture)
end
),
gleam@pair:map_second(
_pipe@1,
fun(_capture@1) ->
new(erlang:element(2, erlang:element(2, List)), _capture@1)
end
).
-file("src/non_empty_list.gleam", 645).
?DOC(
" Takes two non-empty lists and returns a single non-empty list of 2-element\n"
" tuples.\n"
"\n"
" If one of the non-empty lists is longer than the other, the remaining\n"
" elements from the longer non-empty list are not used.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" assert zip(new(1, [2, 3]), single(\"a\")) == new(#(1, \"a\"), [])\n"
" assert zip(single(1), new(\"a\", [\"b\", \"c\"])) == new(#(1, \"a\"), [])\n"
" assert zip(new(1, [2, 3]), new(\"a\", [\"b\", \"c\"])) ==\n"
" new(#(1, \"a\"), [#(2, \"b\"), #(3, \"c\")])\n"
" ```\n"
).
-spec zip(non_empty_list(DWA), non_empty_list(DWC)) -> non_empty_list({DWA, DWC}).
zip(List, Other) ->
new(
{erlang:element(2, List), erlang:element(2, Other)},
gleam@list:zip(erlang:element(3, List), erlang:element(3, Other))
).
-file("src/non_empty_list.gleam", 550).
?DOC(
" Takes two non-empty lists and returns a single non-empty list of 2-element\n"
" tuples.\n"
"\n"
" If one of the non-empty lists is longer than the other, an `Error` is\n"
" returned.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" assert strict_zip(single(1), new(\"a\", [\"b\", \"c\"])) == Error(Nil)\n"
" assert strict_zip(new(1, [2, 3]), single(\"a\")) == Error(Nil)\n"
" assert strict_zip(new(1, [2, 3]), new(\"a\", [\"b\", \"c\"]))\n"
" == Ok(new(#(1, \"a\"), [#(2, \"b\"), #(3, \"c\")]))\n"
" ```\n"
).
-spec strict_zip(non_empty_list(DVF), non_empty_list(DVH)) -> {ok,
non_empty_list({DVF, DVH})} |
{error, nil}.
strict_zip(List, Other) ->
case erlang:length(to_list(List)) =:= erlang:length(to_list(Other)) of
true ->
{ok, zip(List, Other)};
false ->
{error, nil}
end.