Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Erlang-列表理解-填充记录_Erlang_List_Record - Fatal编程技术网

Erlang-列表理解-填充记录

Erlang-列表理解-填充记录,erlang,list,record,Erlang,List,Record,我有一个简单的记录结构,由标题(H)和数据行列表(D)1:N组成。所有标题行必须以数字开头。所有数据行都有前导空格。中间可能还有一些空行(E),必须忽略 L = [H, D, D, E, H, D, E, H, D, D, D]. 我想创建一个记录列表: -record(posting,{header,data}). 使用列表理解。最好的方法是什么?在这种情况下,您必须使用lists:foldl/3,而不是列表理解。使用foldl/3,您可以通过整个列表L累积标题和数据的值。您应该这样做:

我有一个简单的记录结构,由标题(H)和数据行列表(D)1:N组成。所有标题行必须以数字开头。所有数据行都有前导空格。中间可能还有一些空行(E),必须忽略

L = [H, D, D, E, H, D, E, H, D, D, D].
我想创建一个记录列表:

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

使用列表理解。最好的方法是什么?

在这种情况下,您必须使用lists:foldl/3,而不是列表理解。使用foldl/3,您可以通过整个列表L累积标题和数据的值。

您应该这样做:

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.
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.
尾部递归本机Erlang解决方案:

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

。。。以及许多其他变体。

我需要将所有数据行折叠到标题下-因此,目前我有:

  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.
消毒->修剪:修剪。
当列表(L)时生成记录(L)->生成记录(L,未定义,[])。
制作_记录([],u,R)->列表:反向(R);
当元组(H)->
制作记录(T,{元素(1,H),[消毒(D){元素(2,H)]},Acc;
当元组(H)->
做好记录(T、H、Acc);
当F>=0,F时,制作u记录([[F | |]=H | T],B,Acc)=
如果是元组(B)->
制作记录(T,{sanitize(H),[]},[#posting{header=元素(1,B),
数据=列表:反向(元素(2,B))}Acc];
正确->
做记录(T,{消毒(H),[]},Acc)
结束。
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.
  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.