Current section
Files
Jump to
Current section
Files
src/argamak@space.erl
-module(argamak@space).
-compile(no_auto_import).
-export([d0/0, d1/1, d2/2, d3/3, d4/4, d5/5, d6/6, axes/1, degree/1, elements/1, shape/1, map_elements/2, to_string/1]).
-export_type([space/2, space_error/0, d0/0, d1/0, d2/0, d3/0, d4/0, d5/0, d6/0, validate_acc/1, invalid/0]).
-opaque space(JLT, JLU) :: {space, JLT, list({JLU, integer()})}.
-type space_error() :: {space_error, binary(), binary()}.
-type d0() :: d0.
-type d1() :: d1.
-type d2() :: d2.
-type d3() :: d3.
-type d4() :: d4.
-type d5() :: d5.
-type d6() :: d6.
-type validate_acc(JLW) :: {validate_acc,
list(JLW),
boolean(),
list({ok, {JLW, integer()}} |
{error, list(space_error())})}.
-type invalid() :: {invalid, binary(), boolean()}.
-spec d0() -> {ok, space(d0(), any())} | {error, list(space_error())}.
d0() ->
_pipe = {space, d0, []},
{ok, _pipe}.
-spec d1({JME, integer()}) -> {ok, space(d1(), JME)} |
{error, list(space_error())}.
d1(A) ->
_pipe = {space, d1, [A]},
validate(_pipe).
-spec d2({JMJ, integer()}, {JMJ, integer()}) -> {ok, space(d2(), JMJ)} |
{error, list(space_error())}.
d2(A, B) ->
_pipe = {space, d2, [A, B]},
validate(_pipe).
-spec d3({JMO, integer()}, {JMO, integer()}, {JMO, integer()}) -> {ok,
space(d3(), JMO)} |
{error, list(space_error())}.
d3(A, B, C) ->
_pipe = {space, d3, [A, B, C]},
validate(_pipe).
-spec d4({JMT, integer()}, {JMT, integer()}, {JMT, integer()}, {JMT, integer()}) -> {ok,
space(d4(), JMT)} |
{error, list(space_error())}.
d4(A, B, C, D) ->
_pipe = {space, d4, [A, B, C, D]},
validate(_pipe).
-spec d5(
{JMY, integer()},
{JMY, integer()},
{JMY, integer()},
{JMY, integer()},
{JMY, integer()}
) -> {ok, space(d5(), JMY)} | {error, list(space_error())}.
d5(A, B, C, D, E) ->
_pipe = {space, d5, [A, B, C, D, E]},
validate(_pipe).
-spec d6(
{JND, integer()},
{JND, integer()},
{JND, integer()},
{JND, integer()},
{JND, integer()},
{JND, integer()}
) -> {ok, space(d6(), JND)} | {error, list(space_error())}.
d6(A, B, C, D, E, F) ->
_pipe = {space, d6, [A, B, C, D, E, F]},
validate(_pipe).
-spec axes(space(any(), JNJ)) -> list(JNJ).
axes(Space) ->
_pipe = Space,
_pipe@1 = elements(_pipe),
gleam@list:map(_pipe@1, fun gleam@pair:first/1).
-spec degree(space(JNN, any())) -> JNN.
degree(Space) ->
erlang:element(2, Space).
-spec elements(space(any(), JNS)) -> list({JNS, integer()}).
elements(Space) ->
erlang:element(3, Space).
-spec shape(space(any(), any())) -> list(integer()).
shape(Space) ->
_pipe = Space,
_pipe@1 = elements(_pipe),
gleam@list:map(_pipe@1, fun gleam@pair:second/1).
-spec map_elements(space(JOB, JOC), fun(({JOC, integer()}) -> {JOF, integer()})) -> {ok,
space(JOB, JOF)} |
{error, list(space_error())}.
map_elements(Space, Fun) ->
_pipe@2 = {space,
degree(Space),
begin
_pipe = Space,
_pipe@1 = elements(_pipe),
gleam@list:map(_pipe@1, Fun)
end},
validate(_pipe@2).
-spec to_string(space(any(), any())) -> binary().
to_string(Space) ->
Elements = begin
_pipe = Space,
_pipe@1 = elements(_pipe),
_pipe@2 = gleam@list:map(_pipe@1, fun element_to_string/1),
gleam@string:join(_pipe@2, <<", "/utf8>>)
end,
Elements@1 = case Elements /= <<""/utf8>> of
true ->
gleam@string:append(<<" "/utf8>>, Elements);
false ->
Elements
end,
{ok, Degree@1} = case begin
_pipe@3 = Space,
_pipe@4 = degree(_pipe@3),
argamak@util:record_to_string(_pipe@4)
end of
{ok, Degree} -> {ok, Degree};
_try ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try,
module => <<"argamak/space"/utf8>>,
function => <<"to_string"/utf8>>,
line => 374})
end,
gleam@string:append(Degree@1, Elements@1).
-spec element_to_string({any(), integer()}) -> binary().
element_to_string(Element) ->
{ok, Axis@1} = case begin
_pipe = Element,
_pipe@1 = gleam@pair:first(_pipe),
argamak@util:record_to_string(_pipe@1)
end of
{ok, Axis} -> {ok, Axis};
_try ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try,
module => <<"argamak/space"/utf8>>,
function => <<"element_to_string"/utf8>>,
line => 383})
end,
Size = begin
_pipe@2 = Element,
_pipe@3 = gleam@pair:second(_pipe@2),
gleam@int:to_string(_pipe@3)
end,
gleam@string:concat(
[<<"#("/utf8>>, Axis@1, <<", "/utf8>>, Size, <<")"/utf8>>]
).
-spec validate(space(JOP, JOQ)) -> {ok, space(JOP, JOQ)} |
{error, list(space_error())}.
validate(Space) ->
Initial = {validate_acc, [], false, []},
{validate_acc, _@2, _@3, Results} = begin
_pipe = Space,
_pipe@1 = elements(_pipe),
gleam@list:fold(
_pipe@1,
Initial,
fun(Acc, Element) ->
{Axis, Size} = Element,
Errors = begin
_pipe@2 = [{invalid,
<<"multiple axis records from same constructor"/utf8>>,
gleam@list:contains(
erlang:element(2, Acc),
Axis
)},
{invalid,
<<"multiple inferred dimension sizes"/utf8>>,
erlang:element(3, Acc)
andalso (Size
=:= -1)},
{invalid,
<<"dimension size < 1"/utf8>>,
(Size
< 1)
andalso (Size
/= -1)}],
_pipe@3 = gleam@list:map(
_pipe@2,
fun(Invalid) -> case erlang:element(3, Invalid) of
false ->
[];
true ->
[{space_error,
erlang:element(2, Invalid),
element_to_string(Element)}]
end end
),
gleam@list:flatten(_pipe@3)
end,
Result = case Errors of
[] ->
{ok, Element};
_@1 ->
{error, Errors}
end,
{validate_acc,
[Axis | erlang:element(2, Acc)],
erlang:element(3, Acc)
orelse (Size
=:= -1),
[Result | erlang:element(4, Acc)]}
end
)
end,
case gleam@list:any(Results, fun gleam@result:is_error/1) of
false ->
{ok, Space};
true ->
_pipe@4 = Results,
_pipe@5 = gleam@list:reverse(_pipe@4),
_pipe@6 = gleam@list:map(_pipe@5, fun(Result@1) -> case Result@1 of
{ok, _@4} ->
[];
{error, Errors@1} ->
Errors@1
end end),
_pipe@7 = gleam@list:flatten(_pipe@6),
{error, _pipe@7}
end.