-module(tobble@internal@rows). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -define(FILEPATH, "src/tobble/internal/rows.gleam"). -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]). -if(?OTP_RELEASE >= 27). -define(MODULEDOC(Str), -moduledoc(Str)). -define(DOC(Str), -doc(Str)). -else. -define(MODULEDOC(Str), -compile([])). -define(DOC(Str), -compile([])). -endif. ?MODULEDOC(false). -opaque rows(LWS) :: {rows, list(list(LWS))}. -type rows_error() :: {inconsistent_lengths_error, integer(), integer()}. -type non_empty_list(LWT) :: {non_empty_list, LWT, list(LWT)}. -file("src/tobble/internal/rows.gleam", 24). ?DOC(false). -spec to_lists(rows(LXA)) -> list(list(LXA)). to_lists(Rows) -> {rows, Lists} = Rows, Lists. -file("src/tobble/internal/rows.gleam", 30). ?DOC(false). -spec columns(rows(any())) -> integer(). columns(Rows) -> {rows, Lists} = Rows, case Lists of [] -> 0; [First_list | _] -> erlang:length(First_list) end. -file("src/tobble/internal/rows.gleam", 38). ?DOC(false). -spec pop_row(rows(LXG)) -> {ok, {list(LXG), rows(LXG)}} | {error, nil}. pop_row(Rows) -> case Rows of {rows, []} -> {error, nil}; {rows, [Head | Rest]} -> {ok, {Head, {rows, Rest}}} end. -file("src/tobble/internal/rows.gleam", 59). ?DOC(false). -spec map_rows(rows(LXQ), fun((list(LXQ)) -> LXT)) -> list(LXT). map_rows(Rows, Func) -> {rows, Lists} = Rows, gleam@list:map(Lists, Func). -file("src/tobble/internal/rows.gleam", 48). ?DOC(false). -spec rowwise_fold(rows(LXM), LXO, fun((LXO, LXM) -> LXO)) -> list(LXO). rowwise_fold(Rows, Initial_value, Folder) -> map_rows(Rows, fun(Row) -> gleam@list:fold(Row, Initial_value, Folder) end). -file("src/tobble/internal/rows.gleam", 65). ?DOC(false). -spec map(rows(LXV), fun((LXV) -> LXX)) -> rows(LXX). map(Rows, Func) -> _pipe = map_rows(Rows, fun(Row) -> gleam@list:map(Row, Func) end), {rows, _pipe}. -file("src/tobble/internal/rows.gleam", 116). ?DOC(false). -spec must_first(list(LYN)) -> LYN. must_first(List) -> Head@1 = case List of [Head | _] -> Head; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, file => <>, module => <<"tobble/internal/rows"/utf8>>, function => <<"must_first"/utf8>>, line => 117, value => _assert_fail, start => 2819, 'end' => 2847, pattern_start => 2830, pattern_end => 2840}) end, Head@1. -file("src/tobble/internal/rows.gleam", 121). ?DOC(false). -spec must_rest(list(LYP)) -> list(LYP). must_rest(List) -> Rest@1 = case List of [_ | Rest] -> Rest; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, file => <>, module => <<"tobble/internal/rows"/utf8>>, function => <<"must_rest"/utf8>>, line => 122, value => _assert_fail, start => 2901, 'end' => 2934, pattern_start => 2912, pattern_end => 2927}) end, Rest@1. -file("src/tobble/internal/rows.gleam", 95). ?DOC(false). -spec do_transpose(non_empty_list(list(LYG)), list(list(LYG))) -> list(list(LYG)). 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("src/tobble/internal/rows.gleam", 81). ?DOC(false). -spec transpose(rows(LYD)) -> rows(LYD). 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("src/tobble/internal/rows.gleam", 71). ?DOC(false). -spec columnwise_fold(rows(LXZ), LYB, fun((LYB, LXZ) -> LYB)) -> list(LYB). columnwise_fold(Rows, Initial_value, Folder) -> _pipe = Rows, _pipe@1 = transpose(_pipe), rowwise_fold(_pipe@1, Initial_value, Folder). -file("src/tobble/internal/rows.gleam", 137). ?DOC(false). -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("src/tobble/internal/rows.gleam", 126). ?DOC(false). -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("src/tobble/internal/rows.gleam", 18). ?DOC(false). -spec from_lists(list(list(LWU))) -> {ok, rows(LWU)} | {error, rows_error()}. from_lists(Lists) -> _pipe = Lists, _pipe@1 = equal_lengths(_pipe), gleam@result:map(_pipe@1, fun(_) -> {rows, Lists} end).