开发者

Erlang - list comprehensions - populating records

开发者 https://www.devze.com 2023-01-03 17:52 出处:网络
I have a simple record structure consisting of a header (H) and a list of the data lines (D) 1:N. All header lines must start with a digit. All data lines have a leading whitespace. Th开发者_如何学JAV

I have a simple record structure consisting of a header (H) and a list of the data lines (D) 1:N. All header lines must start with a digit. All data lines have a leading whitespace. Th开发者_如何学JAVAere also might be some empty lines (E) in between that must be ignored.

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

I would like to create a list of records:

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

using list comprehension. Whats the best way to do it?


You must use lists:foldl/3 instead of list comprehensions in this case. With foldl/3 you can accumulate values of header and data through whole list L.


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.

Anyway I think that straightforward Erlang version doesn't seems too complicated and should be little bit 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).

Edit: If you have to add better row classification or parsing, adding new function is better because it improves readability.

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}.

You can use it like this:

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.

Tail recursive native Erlang solution:

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).

I think that there is no reason use tail recursion from performance point of view:

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.

... and many many other variants.


I needed to collapse all Data lines beneath the header - so for the moment here 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

精彩评论

暂无评论...
验证码 换一张
取 消