Current section
Files
Jump to
Current section
Files
src/tobble@internal@rows.erl
-module(tobble@internal@rows).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([to_lists/1, columns/1, pop_row/1, map_rows/2, rowwise_fold/3, map/2, columnwise_fold/3, from_lists/1]).
-export_type([rows/1, rows_error/0, non_empty_list/1]).
-opaque rows(ODN) :: {rows, list(list(ODN))}.
-type rows_error() :: {inconsistent_lengths_error, integer(), integer()}.
-type non_empty_list(ODO) :: {non_empty_list, ODO, list(ODO)}.
-file("/home/nick/Documents/code/tobble/src/tobble/internal/rows.gleam", 24).
-spec to_lists(rows(ODV)) -> list(list(ODV)).
to_lists(Rows) ->
{rows, Lists} = Rows,
Lists.
-file("/home/nick/Documents/code/tobble/src/tobble/internal/rows.gleam", 30).
-spec columns(rows(any())) -> integer().
columns(Rows) ->
{rows, Lists} = Rows,
case Lists of
[] ->
0;
[First_list | _] ->
erlang:length(First_list)
end.
-file("/home/nick/Documents/code/tobble/src/tobble/internal/rows.gleam", 38).
-spec pop_row(rows(OEB)) -> {ok, {list(OEB), rows(OEB)}} | {error, nil}.
pop_row(Rows) ->
case Rows of
{rows, []} ->
{error, nil};
{rows, [Head | Rest]} ->
{ok, {Head, {rows, Rest}}}
end.
-file("/home/nick/Documents/code/tobble/src/tobble/internal/rows.gleam", 59).
-spec map_rows(rows(OEL), fun((list(OEL)) -> OEO)) -> list(OEO).
map_rows(Rows, Func) ->
{rows, Lists} = Rows,
gleam@list:map(Lists, Func).
-file("/home/nick/Documents/code/tobble/src/tobble/internal/rows.gleam", 48).
-spec rowwise_fold(rows(OEH), OEJ, fun((OEJ, OEH) -> OEJ)) -> list(OEJ).
rowwise_fold(Rows, Initial_value, Folder) ->
map_rows(Rows, fun(Row) -> gleam@list:fold(Row, Initial_value, Folder) end).
-file("/home/nick/Documents/code/tobble/src/tobble/internal/rows.gleam", 65).
-spec map(rows(OEQ), fun((OEQ) -> OES)) -> rows(OES).
map(Rows, Func) ->
_pipe = map_rows(Rows, fun(Row) -> gleam@list:map(Row, Func) end),
{rows, _pipe}.
-file("/home/nick/Documents/code/tobble/src/tobble/internal/rows.gleam", 116).
-spec must_first(list(OFI)) -> OFI.
must_first(List) ->
[Head | _] = case List of
[_ | _] -> List;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"tobble/internal/rows"/utf8>>,
function => <<"must_first"/utf8>>,
line => 117})
end,
Head.
-file("/home/nick/Documents/code/tobble/src/tobble/internal/rows.gleam", 121).
-spec must_rest(list(OFK)) -> list(OFK).
must_rest(List) ->
[_ | Rest] = case List of
[_ | _] -> List;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"tobble/internal/rows"/utf8>>,
function => <<"must_rest"/utf8>>,
line => 122})
end,
Rest.
-file("/home/nick/Documents/code/tobble/src/tobble/internal/rows.gleam", 95).
-spec do_transpose(non_empty_list(list(OFB)), list(list(OFB))) -> list(list(OFB)).
do_transpose(Lists, Acc) ->
case erlang:element(2, Lists) of
[] ->
lists:reverse(Acc);
_ ->
Row = [must_first(erlang:element(2, Lists)) |
gleam@list:map(erlang:element(3, Lists), fun must_first/1)],
All_rests = {non_empty_list,
must_rest(erlang:element(2, Lists)),
gleam@list:map(erlang:element(3, Lists), fun must_rest/1)},
do_transpose(All_rests, [Row | Acc])
end.
-file("/home/nick/Documents/code/tobble/src/tobble/internal/rows.gleam", 81).
-spec transpose(rows(OEY)) -> rows(OEY).
transpose(Rows) ->
{rows, Lists} = Rows,
case Lists of
[] ->
Rows;
[Head | Rest] ->
Non_empty = {non_empty_list, Head, Rest},
{rows, do_transpose(Non_empty, [])}
end.
-file("/home/nick/Documents/code/tobble/src/tobble/internal/rows.gleam", 71).
-spec columnwise_fold(rows(OEU), OEW, fun((OEW, OEU) -> OEW)) -> list(OEW).
columnwise_fold(Rows, Initial_value, Folder) ->
_pipe = Rows,
_pipe@1 = transpose(_pipe),
rowwise_fold(_pipe@1, Initial_value, Folder).
-file("/home/nick/Documents/code/tobble/src/tobble/internal/rows.gleam", 137).
-spec do_equal_lengths_check(integer(), list(list(any()))) -> {ok, nil} |
{error, rows_error()}.
do_equal_lengths_check(Last_length, Lists) ->
case Lists of
[] ->
{ok, nil};
[List] ->
Got_length = erlang:length(List),
case Got_length =:= Last_length of
true ->
{ok, nil};
false ->
{error,
{inconsistent_lengths_error, Last_length, Got_length}}
end;
[List@1 | Rest] ->
Length = erlang:length(List@1),
case Length =:= Last_length of
false ->
{error, {inconsistent_lengths_error, Last_length, Length}};
true ->
do_equal_lengths_check(Length, Rest)
end
end.
-file("/home/nick/Documents/code/tobble/src/tobble/internal/rows.gleam", 126).
-spec equal_lengths(list(list(any()))) -> {ok, nil} | {error, rows_error()}.
equal_lengths(Lists) ->
case Lists of
[] ->
{ok, nil};
[_] ->
{ok, nil};
[List | Rest] ->
Length = erlang:length(List),
do_equal_lengths_check(Length, Rest)
end.
-file("/home/nick/Documents/code/tobble/src/tobble/internal/rows.gleam", 18).
-spec from_lists(list(list(ODP))) -> {ok, rows(ODP)} | {error, rows_error()}.
from_lists(Lists) ->
_pipe = Lists,
_pipe@1 = equal_lengths(_pipe),
gleam@result:map(_pipe@1, fun(_) -> {rows, Lists} end).