Current section
Files
Jump to
Current section
Files
src/kitazith@internal@validation@duplicate.erl
-module(kitazith@internal@validation@duplicate).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/kitazith/internal/validation/duplicate.gleam").
-export([find/1]).
-export_type([occurrence/2, duplicate/2]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(false).
-type occurrence(ENE, ENF) :: {occurrence, ENE, ENF}.
-type duplicate(ENG, ENH) :: {duplicate, ENG, list(ENH)}.
-file("src/kitazith/internal/validation/duplicate.gleam", 44).
?DOC(false).
-spec collect_paths(list(occurrence(ENZ, EOA)), ENZ) -> list(EOA).
collect_paths(Occurrences, Target_value) ->
_pipe = Occurrences,
gleam@list:filter_map(
_pipe,
fun(Occurrence) ->
case erlang:element(2, Occurrence) =:= Target_value of
true ->
{ok, erlang:element(3, Occurrence)};
false ->
{error, nil}
end
end
).
-file("src/kitazith/internal/validation/duplicate.gleam", 57).
?DOC(false).
-spec contains(list(EOF), EOF) -> boolean().
contains(Items, Target) ->
case Items of
[] ->
false;
[Item | Rest] ->
(Item =:= Target) orelse contains(Rest, Target)
end.
-file("src/kitazith/internal/validation/duplicate.gleam", 17).
?DOC(false).
-spec collect_duplicates(list(occurrence(ENQ, ENR)), list(ENQ)) -> list(duplicate(ENQ, ENR)).
collect_duplicates(Occurrences, Seen_values) ->
case Occurrences of
[] ->
[];
[{occurrence, Value, Path} | Rest] ->
case contains(Seen_values, Value) of
true ->
collect_duplicates(Rest, Seen_values);
false ->
Paths = [Path | collect_paths(Rest, Value)],
Duplicates = case erlang:length(Paths) > 1 of
true ->
[{duplicate, Value, Paths}];
false ->
[]
end,
lists:append(
Duplicates,
collect_duplicates(Rest, [Value | Seen_values])
)
end
end.
-file("src/kitazith/internal/validation/duplicate.gleam", 11).
?DOC(false).
-spec find(list(occurrence(ENI, ENJ))) -> list(duplicate(ENI, ENJ)).
find(Occurrences) ->
collect_duplicates(Occurrences, []).