wfc stuff

This commit is contained in:
2025-09-19 22:24:38 -07:00
parent 60b149d520
commit 5e559b540b
3 changed files with 175 additions and 0 deletions
+82
View File
@@ -0,0 +1,82 @@
% @doc a word is an ordset of wfchars
%
% multiplication is implied in a word
%
% empty word is 1
%
% anything times itself equals itself, so duplication is ignored
-module(wfc_word).
-export_type([
word/0
]).
-export([
%% constructors
one/0,
validate/1,
from_wfchars/1, to_wfchars/1,
%% ops
mul/2, mul/1
]).
-opaque word() :: {w, ordsets:ordset(wfc_wfchar:wfchar())}.
%%----------------------------
%% constructor
%%----------------------------
-spec one() -> word().
one() ->
{w, []}.
validate(_) -> error(nyi).
to_wfchars(_) -> error(nyi).
-spec from_wfchars(WfChars) -> Result
when WfChars :: list(wfc_char:wfchar()),
Result :: {ok, word()}
| {error, Reason :: string()}.
from_wfchars(Chars) ->
from_wfchars(ordsets:from_list(Chars), []).
%% validate each char
from_wfchars([WfChar | Rest], Acc) ->
case wfc_wfchar:validate(WfChar) of
ok -> from_wfchars(Rest, [WfChar | Acc]);
Error -> Error
end;
% done, all good
from_wfchars([], Acc) ->
{ok, {w, lists:reverse(Acc)}}.
%%----------------------------
%% ops
%%----------------------------
-spec mul(word(), word()) -> Result
when Result :: {ok, word()}
| {error, Reason :: string()}.
% @doc product of two words
mul({w, X}, {w, Y}) ->
case from_wfchars(ordsets:union(X, Y)) of
Result = {ok, _} -> Result;
Error -> Error
end.
-spec mul(Words) -> Result
when Words :: [word()],
Result :: {ok, word()}
| {error, Reason},
Reason :: string().
% @doc multiply a list of words together
mul([Word | Rest]) -> mul(Word, mul(Rest));
mul([]) -> {ok, one()}.