Erlang - concepts list - filling records

I have a simple record structure consisting of a header (H) and a list of data lines (D) 1: N. All header lines should start with a digit. All data lines have leading spaces. There may also be some empty lines (E) between them that must be ignored.

L = [H, D, D, E, H, D, E, H, D, D, D].

I would like to create a list of entries:

-record(posting,{header,data}).

using list comprehension. What is the best way to do this?

+3
source share
3 answers

You should do something like this:

make_records(L) when is_list(L) ->
  F = fun([32|_]=D,{#posting{}=H,Acc}) -> {H,[H#posting{data=D}|Acc]};
         ([], Acc) -> Acc;
         ([F|_]=H, {_,Acc}) when F=<$0, F>=$9 -> {#posting{header=>H}, Acc}
      end,
  {_, R} = lists:foldl(F, {undefined, []}, L),
  R.

In any case, I think the simple version of Erlang does not seem too complicated and should be a little faster.

make_records2(L) when is_list(L) ->
  make_records2(L, undefined, []).

make_records2([], _, R) -> R;
make_records2([[32|_]=D|T], H, Acc) when is_list(H) ->
  make_records2(T, H, [#posting{header=H,data=D}|Acc]);
make_records2([[]|T], H, Acc) ->
  make_records2(T, H, Acc);
make_records2([[F|_]=H|T], _, Acc) when F>=$0, F=<$9 ->
  make_records2(T, H, Acc).

. , , .

parse_row([Digit|_]=R) when Digit >= $0, Digit =< $9 -> {header, R};
parse_row(R) -> try_spaces(R).

try_spaces([]) -> empty;
try_spaces([Sp|R]) when Sp=:=$\s; Sp=:=$\t; Sp=:=$\n ->
    try_spaces(R); % skip all white spaces from Data field
try_spaces(Data) -> {data, Data}.

:

make_records(L) when is_list(L) ->
  F = fun(Row, {H, Acc}) ->
           case parse_row(Row) of
             {data, D} when is_record(H, posting) -> {H,[H#posting{data=D}|Acc]};
             empty -> Acc;
             {header, H} -> {#posting{header=>H}, Acc}
      end,
  {_, R} = lists:foldl(F, {undefined, []}, L),
  R.

:

make_records2(L) when is_list(L) ->
  make_records2([parse_row(R) || R<-L], undefined, []).

make_records2([], _, R) -> R;
make_records2([{data, D}|T], H, Acc) when is_list(H) ->
  make_records2(T, H, [#posting{header=H,data=D}|Acc]);
make_records2([empty|T], H, Acc) ->
  make_records2(T, H, Acc);
make_records2([{header,H}|T], _, Acc) ->
  make_records2(T, H, Acc).

, :

make_records3(L) when is_list(L) ->
  make_records3(L, undefined).

make_records3([], _) -> [];
make_records3([R|T], H) ->
  case parse_row(R) of
    {data, D} when is_list(H) -> [#posting{head=H,data=D}|make_records3(T, H)];
    empty -> make_records3(T, H);
    {header, H2} -> make_records3(T, H2)
  end.

... .

+3

: foldl/3 . foldl/3 L.

+6

I needed to collapse all the rows of data under the heading, so for now this is what I have:

  sanitize(S) -> trim:trim(S).

  make_records(L) when is_list(L) -> make_records(L, undefined, []).

  make_records([], _, R) -> lists:reverse(R);

  make_records([[32|_]=D|T], H, Acc) when is_tuple(H) ->
        make_records(T, {element(1,H),[sanitize(D)|element(2,H)]},Acc);

  make_records([[$\n|_]=D|T], H, Acc) when is_tuple(H) ->
        make_records(T, H, Acc);


  make_records([[F|_]=H|T], B, Acc) when F>=$0, F=<$9 ->
      if is_tuple(B) ->
          make_records(T, {sanitize(H),[]}, [#posting{header=element(1,B),
          data=lists:reverse(element(2,B))}|Acc]);
      true ->
          make_records(T, {sanitize(H),[]}, Acc)
      end.
0
source

Source: https://habr.com/ru/post/1750622/


All Articles