pushing fewd
This commit is contained in:
parent
5e559b540b
commit
91f0064a5b
@ -2,3 +2,8 @@
|
|||||||
|
|
||||||
this is me (PRH) trying to learn some front end web dev because pixels are
|
this is me (PRH) trying to learn some front end web dev because pixels are
|
||||||
important despite my wishes.
|
important despite my wishes.
|
||||||
|
|
||||||
|
# notes
|
||||||
|
|
||||||
|
## goal queue
|
||||||
|
|
||||||
|
|||||||
@ -1,10 +1,10 @@
|
|||||||
% @doc called wfchar to disambiguate from
|
% @doc called ltr to disambiguate from
|
||||||
%
|
%
|
||||||
% mathematically, this is a variable like "a", "b", "c", etc
|
% mathematically, this is a variable like "a", "b", "c", etc
|
||||||
-module(wfc_wfchar).
|
-module(wfc_ltr).
|
||||||
|
|
||||||
-export_type([
|
-export_type([
|
||||||
wfchar/0
|
ltr/0
|
||||||
]).
|
]).
|
||||||
|
|
||||||
-export([
|
-export([
|
||||||
@ -22,17 +22,17 @@
|
|||||||
% - numbers ok
|
% - numbers ok
|
||||||
% - no non-ascii chars
|
% - no non-ascii chars
|
||||||
|
|
||||||
-opaque wfchar() :: {c, binary()}.
|
-opaque ltr() :: {c, binary()}.
|
||||||
|
|
||||||
%%-----------------------
|
%%-----------------------
|
||||||
%% constructors/destructors
|
%% constructors/destructors
|
||||||
%%-----------------------
|
%%-----------------------
|
||||||
|
|
||||||
-spec validate(WfChar) -> Result
|
-spec validate(Ltr) -> Result
|
||||||
when WfChar :: wfchar(),
|
when Ltr :: ltr(),
|
||||||
Result :: ok
|
Result :: ok
|
||||||
| {error, Reason :: string()}.
|
| {error, Reason :: string()}.
|
||||||
% @doc validate if a candidate wfchar is well-formed and the inner binary is
|
% @doc validate if a candidate ltr is well-formed and the inner binary is
|
||||||
% legal
|
% legal
|
||||||
|
|
||||||
validate({c, Binary}) ->
|
validate({c, Binary}) ->
|
||||||
@ -43,25 +43,25 @@ validate({c, Binary}) ->
|
|||||||
Error
|
Error
|
||||||
end;
|
end;
|
||||||
validate(Term) ->
|
validate(Term) ->
|
||||||
Error = wfc_utils:emsg("wfc_wfchar:validate failed; malformed wfchar: ~p", [Term]),
|
Error = wfc_utils:emsg("wfc_ltr:validate failed; malformed ltr: ~p", [Term]),
|
||||||
{error, Error}.
|
{error, Error}.
|
||||||
|
|
||||||
|
|
||||||
-spec from_binary(Binary) -> Result
|
-spec from_binary(Binary) -> Result
|
||||||
when Binary :: binary(),
|
when Binary :: binary(),
|
||||||
Result :: {ok, wfchar()}
|
Result :: {ok, ltr()}
|
||||||
| {error, Reason :: string()}.
|
| {error, Reason :: string()}.
|
||||||
% @doc
|
% @doc
|
||||||
% make sure the binary is a legal wfchar and wrap it in a {c, Binary} tuple
|
% make sure the binary is a legal ltr and wrap it in a {c, Binary} tuple
|
||||||
% @end
|
% @end
|
||||||
|
|
||||||
%% initial char must be [a-z]
|
%% initial char must be [a-z]
|
||||||
from_binary(<<L:8, Rest/binary>>) when $a =< L, L =< $z ->
|
from_binary(<<L:8, Rest/binary>>) when $a =< L, L =< $z ->
|
||||||
new2(<<L>>, Rest);
|
new2(<<L>>, Rest);
|
||||||
from_binary(Char = <<L:8, _/binary>>) ->
|
from_binary(Char = <<L:8, _/binary>>) ->
|
||||||
{error, wfc_utils:emsg("wfc_wfchar:from_binary(~p): illegal lead character: ~p", [Char, L])};
|
{error, wfc_utils:emsg("wfc_ltr:from_binary(~p): illegal lead character: ~p", [Char, L])};
|
||||||
from_binary(Char) ->
|
from_binary(Char) ->
|
||||||
wfc_utils:err("wfc_wfchar:from_binary(~p): malformed argument", [Char]).
|
wfc_utils:err("wfc_ltr:from_binary(~p): malformed argument", [Char]).
|
||||||
|
|
||||||
%% rest must be [A-Za-z0-9_]
|
%% rest must be [A-Za-z0-9_]
|
||||||
new2(Acc, <<L:8, Rest/binary>>) when ($A =< L andalso L =< $Z)
|
new2(Acc, <<L:8, Rest/binary>>) when ($A =< L andalso L =< $Z)
|
||||||
@ -73,11 +73,11 @@ new2(Acc, <<>>) ->
|
|||||||
{ok, {c, Acc}};
|
{ok, {c, Acc}};
|
||||||
new2(Acc, Rest = <<BadChar:8, _/binary>>) ->
|
new2(Acc, Rest = <<BadChar:8, _/binary>>) ->
|
||||||
WholeChar = <<Acc/binary, Rest/binary>>,
|
WholeChar = <<Acc/binary, Rest/binary>>,
|
||||||
Error = wfc_utils:emsg("wfc_wfchar:new2(~p, ~p): illegal character in wfchar: ~p; WholeChar: ~p", [Acc, Rest, BadChar, WholeChar]),
|
Error = wfc_utils:emsg("wfc_ltr:new2(~p, ~p): illegal character in ltr: ~p; WholeChar: ~p", [Acc, Rest, BadChar, WholeChar]),
|
||||||
{error, Error}.
|
{error, Error}.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-spec to_binary(wfchar()) -> binary().
|
-spec to_binary(ltr()) -> binary().
|
||||||
|
|
||||||
to_binary({c, X}) -> X.
|
to_binary({c, X}) -> X.
|
||||||
@ -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
|
% multiplication is implied in a word
|
||||||
%
|
%
|
||||||
@ -15,12 +15,12 @@
|
|||||||
%% constructors
|
%% constructors
|
||||||
one/0,
|
one/0,
|
||||||
validate/1,
|
validate/1,
|
||||||
from_wfchars/1, to_wfchars/1,
|
from_ltrs/1, to_ltrs/1,
|
||||||
%% ops
|
%% ops
|
||||||
mul/2, mul/1
|
mul/2, mul/1
|
||||||
]).
|
]).
|
||||||
|
|
||||||
-opaque word() :: {w, ordsets:ordset(wfc_wfchar:wfchar())}.
|
-opaque word() :: {w, ordsets:ordset(wfc_ltr:ltr())}.
|
||||||
|
|
||||||
%%----------------------------
|
%%----------------------------
|
||||||
%% constructor
|
%% constructor
|
||||||
@ -32,26 +32,26 @@ one() ->
|
|||||||
{w, []}.
|
{w, []}.
|
||||||
|
|
||||||
|
|
||||||
validate(_) -> error(nyi).
|
validate(_) -> error(nyi).
|
||||||
to_wfchars(_) -> error(nyi).
|
to_ltrs(_) -> error(nyi).
|
||||||
|
|
||||||
-spec from_wfchars(WfChars) -> Result
|
-spec from_ltrs(Ltrs) -> Result
|
||||||
when WfChars :: list(wfc_char:wfchar()),
|
when Ltrs :: list(wfc_ltr:ltr()),
|
||||||
Result :: {ok, word()}
|
Result :: {ok, word()}
|
||||||
| {error, Reason :: string()}.
|
| {error, Reason :: string()}.
|
||||||
|
|
||||||
from_wfchars(Chars) ->
|
from_ltrs(Chars) ->
|
||||||
from_wfchars(ordsets:from_list(Chars), []).
|
from_ltrs(ordsets:from_list(Chars), []).
|
||||||
|
|
||||||
|
|
||||||
%% validate each char
|
%% validate each letter
|
||||||
from_wfchars([WfChar | Rest], Acc) ->
|
from_ltrs([Ltr | Rest], Acc) ->
|
||||||
case wfc_wfchar:validate(WfChar) of
|
case wfc_ltr:validate(Ltr) of
|
||||||
ok -> from_wfchars(Rest, [WfChar | Acc]);
|
ok -> from_ltrs(Rest, [Ltr | Acc]);
|
||||||
Error -> Error
|
Error -> Error
|
||||||
end;
|
end;
|
||||||
% done, all good
|
% done, all good
|
||||||
from_wfchars([], Acc) ->
|
from_ltrs([], Acc) ->
|
||||||
{ok, {w, lists:reverse(Acc)}}.
|
{ok, {w, lists:reverse(Acc)}}.
|
||||||
|
|
||||||
|
|
||||||
@ -59,24 +59,17 @@ from_wfchars([], Acc) ->
|
|||||||
%% ops
|
%% ops
|
||||||
%%----------------------------
|
%%----------------------------
|
||||||
|
|
||||||
-spec mul(word(), word()) -> Result
|
-spec mul(word(), word()) -> word().
|
||||||
when Result :: {ok, word()}
|
|
||||||
| {error, Reason :: string()}.
|
|
||||||
% @doc product of two words
|
% @doc product of two words
|
||||||
|
%
|
||||||
|
% assumes the words are valid
|
||||||
|
|
||||||
mul({w, X}, {w, Y}) ->
|
mul({w, X}, {w, Y}) ->
|
||||||
case from_wfchars(ordsets:union(X, Y)) of
|
{w, ordsets:union(X, Y)}.
|
||||||
Result = {ok, _} -> Result;
|
|
||||||
Error -> Error
|
|
||||||
end.
|
|
||||||
|
|
||||||
|
|
||||||
-spec mul(Words) -> Result
|
-spec mul([word()]) -> word().
|
||||||
when Words :: [word()],
|
|
||||||
Result :: {ok, word()}
|
|
||||||
| {error, Reason},
|
|
||||||
Reason :: string().
|
|
||||||
% @doc multiply a list of words together
|
% @doc multiply a list of words together
|
||||||
|
|
||||||
mul([Word | Rest]) -> mul(Word, mul(Rest));
|
mul([Word | Rest]) -> mul(Word, mul(Rest));
|
||||||
mul([]) -> {ok, one()}.
|
mul([]) -> one().
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user