Current section
Files
Jump to
Current section
Files
src/apiculture@alphabet.erl
-module(apiculture@alphabet).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/apiculture/alphabet.gleam").
-export([new_alphabet/1, characters/1, size/1]).
-export_type([alphabet/0]).
-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(
" Alphabet representation and validation.\n"
"\n"
" An alphabet is a validated set of graphemes for direct random character\n"
" sampling. It is deliberately separate from an encoding, which transforms\n"
" arbitrary bytes into text.\n"
).
-type alphabet() :: {alphabet, binary()}.
-file("src/apiculture/alphabet.gleam", 50).
-spec has_duplicates(list(binary())) -> boolean().
has_duplicates(Items) ->
case Items of
[] ->
false;
[First | Rest] ->
case gleam@list:contains(Rest, First) of
true ->
true;
false ->
has_duplicates(Rest)
end
end.
-file("src/apiculture/alphabet.gleam", 21).
?DOC(
" Creates a new alphabet from a string of characters.\n"
"\n"
" Returns an error if the alphabet is empty, has only one character,\n"
" or contains duplicate characters.\n"
).
-spec new_alphabet(binary()) -> {ok, alphabet()} |
{error, apiculture@error:error()}.
new_alphabet(Chars) ->
Char_list = gleam@string:to_graphemes(Chars),
case gleam@list:is_empty(Char_list) of
true ->
{error, empty_alphabet};
false ->
case erlang:length(Char_list) of
1 ->
{error, single_character_alphabet};
_ ->
case has_duplicates(Char_list) of
true ->
{error, duplicate_characters};
false ->
{ok, {alphabet, Chars}}
end
end
end.
-file("src/apiculture/alphabet.gleam", 41).
?DOC(" Returns the characters in the alphabet as a list of graphemes.\n").
-spec characters(alphabet()) -> list(binary()).
characters(Alphabet) ->
gleam@string:to_graphemes(erlang:element(2, Alphabet)).
-file("src/apiculture/alphabet.gleam", 46).
?DOC(" Returns the size (number of characters) in the alphabet.\n").
-spec size(alphabet()) -> integer().
size(Alphabet) ->
string:length(erlang:element(2, Alphabet)).