-module(mesv@util).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src\\mesv\\util.gleam").
-export([list_merge_map/2, count_occurences/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(
" A file containing various utility functions that don't exactly fit into any of the other modules.\n"
" \n"
" They are purely for internal use - but since most of them are generic, there's nothing stopping you, as the end user, from calling them in your own code.\n"
" \n"
" However, beacause:\n"
" 1. I doubt most people would find themselves in a situation where they need them\n"
" 2. They are not particularly easy to understand\n"
" \n"
" these functions are not considered part of the functionality provided by this library, and therefore are not documented that well.\n"
).
-file("src\\mesv\\util.gleam", 25).
-spec list_merge_map_loop(
list(DOO),
fun((DOO, DOO) -> gleam@option:option(DOO)),
list(DOO)
) -> list(DOO).
list_merge_map_loop(List, Merge, Acc) ->
case List of
[] ->
lists:reverse(Acc);
[Last] ->
lists:reverse([Last | Acc]);
[First, Second | Rest] ->
case Merge(First, Second) of
{some, Merged} ->
list_merge_map_loop([Merged | Rest], Merge, Acc);
none ->
list_merge_map_loop([Second | Rest], Merge, [First | Acc])
end
end.
-file("src\\mesv\\util.gleam", 21).
?DOC(
" Internal helper function that traverses a list, calling the `merge` function on\n"
" all consecutive elements.\n"
" \n"
" If the function returns `Some(a)`, then the two elements are replaced with the contents,\n"
" and if it returns `None`, the function advances to the next pair of elements.\n"
).
-spec list_merge_map(list(DOK), fun((DOK, DOK) -> gleam@option:option(DOK))) -> list(DOK).
list_merge_map(List, Merge) ->
list_merge_map_loop(List, Merge, []).
-file("src\\mesv\\util.gleam", 68).
-spec recursive_count(
DOT,
fun((DOT) -> {DOT, integer()}),
fun((DOT) -> boolean()),
integer()
) -> integer().
recursive_count(From, Fun, Exhausted, Acc) ->
case Exhausted(From) of
true ->
Acc;
false ->
{Next, Add} = Fun(From),
recursive_count(Next, Fun, Exhausted, Acc + Add)
end.
-file("src\\mesv\\util.gleam", 46).
?DOC(
" Internal helper function to count how many **overlapping** occurences of the first argument\n"
" appear in the second argument.\n"
" \n"
" If there are none, this function returns the length of the second argument.\n"
).
-spec count_occurences(binary(), binary()) -> integer().
count_occurences(Find, In) ->
case gleam@string:is_empty(Find) of
true ->
string:length(In);
false ->
recursive_count(
In,
fun(Source) ->
{gleam@string:drop_start(Source, 1),
case gleam@string:slice(Source, 0, string:length(Find))
=:= Find of
true ->
1;
false ->
0
end}
end,
fun gleam@string:is_empty/1,
0
)
end.