pushing fewd

This commit is contained in:
2025-09-29 11:52:59 -07:00
parent 5e559b540b
commit 91f0064a5b
3 changed files with 39 additions and 41 deletions
+20 -27
View File
@@ -1,4 +1,4 @@
% @doc a word is an ordset of wfchars
% @doc a word is an ordset of ltrs
%
% multiplication is implied in a word
%
@@ -15,12 +15,12 @@
%% constructors
one/0,
validate/1,
from_wfchars/1, to_wfchars/1,
from_ltrs/1, to_ltrs/1,
%% ops
mul/2, mul/1
]).
-opaque word() :: {w, ordsets:ordset(wfc_wfchar:wfchar())}.
-opaque word() :: {w, ordsets:ordset(wfc_ltr:ltr())}.
%%----------------------------
%% constructor
@@ -32,26 +32,26 @@ one() ->
{w, []}.
validate(_) -> error(nyi).
to_wfchars(_) -> error(nyi).
validate(_) -> error(nyi).
to_ltrs(_) -> error(nyi).
-spec from_wfchars(WfChars) -> Result
when WfChars :: list(wfc_char:wfchar()),
-spec from_ltrs(Ltrs) -> Result
when Ltrs :: list(wfc_ltr:ltr()),
Result :: {ok, word()}
| {error, Reason :: string()}.
from_wfchars(Chars) ->
from_wfchars(ordsets:from_list(Chars), []).
from_ltrs(Chars) ->
from_ltrs(ordsets:from_list(Chars), []).
%% validate each char
from_wfchars([WfChar | Rest], Acc) ->
case wfc_wfchar:validate(WfChar) of
ok -> from_wfchars(Rest, [WfChar | Acc]);
%% validate each letter
from_ltrs([Ltr | Rest], Acc) ->
case wfc_ltr:validate(Ltr) of
ok -> from_ltrs(Rest, [Ltr | Acc]);
Error -> Error
end;
% done, all good
from_wfchars([], Acc) ->
from_ltrs([], Acc) ->
{ok, {w, lists:reverse(Acc)}}.
@@ -59,24 +59,17 @@ from_wfchars([], Acc) ->
%% ops
%%----------------------------
-spec mul(word(), word()) -> Result
when Result :: {ok, word()}
| {error, Reason :: string()}.
-spec mul(word(), word()) -> word().
% @doc product of two words
%
% assumes the words are valid
mul({w, X}, {w, Y}) ->
case from_wfchars(ordsets:union(X, Y)) of
Result = {ok, _} -> Result;
Error -> Error
end.
{w, ordsets:union(X, Y)}.
-spec mul(Words) -> Result
when Words :: [word()],
Result :: {ok, word()}
| {error, Reason},
Reason :: string().
-spec mul([word()]) -> word().
% @doc multiply a list of words together
mul([Word | Rest]) -> mul(Word, mul(Rest));
mul([]) -> {ok, one()}.
mul([]) -> one().