2

私はChicagoBossでErlangを学んでいるところですが、これに似たものを(擬似コードで)どのように実行できるか知りたいです。

foreach (items as item)
    if (i % 10 == 0)
        <tr>
    endif
    <td>...</td>
    if (i++ % 10 == 0)
        </tr>
    endif
endforeach

私のテンプレートで?

4

2 に答える 2

1

Erlangは関数型言語なので、慣用的な方法で関数型で実行します。最初にデータを表形式にする関数を用意します。

-module(tabify).

-export([tabify/2]).

tabify(N, L) when is_list(L), is_integer(N), N > 0 ->
  tabify_(N, L).

tabify_(_, []) -> [];
tabify_(N, L) ->
  {Row, Rest} = row(L, N),
  [Row|tabify_(N, Rest)].

row(L, N) ->
  row(L, N, []).

row([], _, Accu) -> {lists:reverse(Accu), []};
row(Rest, 0, Accu) -> {lists:reverse(Accu), Rest};
row([H|T], N, Accu) -> row(T, N-1, [H|Accu]).

そして今、私たちはそれを次のように使うことができます:

1> c(tabify).
{ok,tabify}
2> Data = [integer_to_list(X) || X <- lists:seq(1,100)].
["1","2","3","4","5","6","7","8","9","10","11","12","13",
 "14","15","16","17","18","19","20","21","22","23","24","25",
 "26","27","28",
 [...]|...]
3> Table = tabify:tabify(10,Data).
[["1","2","3","4","5","6","7","8","9","10"],
 ["11","12","13","14","15","16","17","18","19","20"],
 ["21","22","23","24","25","26","27","28","29","30"],
 ["31","32","33","34","35","36","37","38","39","40"],
 ["41","42","43","44","45","46","47","48","49","50"],
 ["51","52","53","54","55","56","57","58","59","60"],
 ["61","62","63","64","65","66","67","68","69","70"],
 ["71","72","73","74","75","76","77","78","79","80"],
 ["81","82","83","84","85","86","87","88","89","90"],
 ["91","92","93","94","95","96","97","98","99","100"]]
4> T = [["<tr>", [["<td>", Item, "</td>"] || Item <- Row ], "</tr>"]|| Row <- Table].
[["<tr>",
  [["<td>","1","</td>"],
   ["<td>","2","</td>"],
   ...

そして、ioサブシステムに残りを任せるよりも。上記の構造はiolistとしてよく知られており、任意のioに配置すると、次のように適切にシリアル化されます。

6> iolist_to_binary(T).
<<"<tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td><td>8</td><td>9</td><td>10</td></tr><tr><t"...>>

テーブルに数千のアイテムがあり、効率が重要である場合は、すべてのリスト定数をバイナリに変換できます。でバイナリデータに変換することもできますData。最後の手段としてtabify/2、より効率的ですが読みにくい方法で書き直し、フォーマットすることができます。

于 2012-06-16T13:45:50.460 に答える
0

リストを「集計」する必要がある場合は、私が作成した次の関数を使用できます。

tab_list(List1) ->
    lists:append(
        lists:flatten(
            lists:map(
                fun({Item, Idx}) -> 
                    if 
                        ((Idx - 1) rem 10) == 0 -> lists:concat(["<TR><TD>", Item, "</TD>"]); 
                        (Idx rem 10) == 0 -> lists:concat(["<TD>", Item, "</TD></TR>"]);
                        true -> lists:concat(["<TD>", Item, "</TD>"])
                    end
                end,
            lists:zip(List1, lists:seq(1, length(List1)))
            )
        ),
    if (length(List1) rem 10) == 0 -> ""; true -> "</TR>" end
).

このようなリストが渡される["a", "b", "c", "d", "e", "f", "g", "h", "i", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "z"]と、次の結果が生成されます。

"<TR><TD>a</TD><TD>b</TD><TD>c</TD><TD>d</TD><TD>e</TD><TD>f</TD><TD>g</TD><TD>h</TD><TD>i</TD><TD>l</TD></TR><TR><TD>m</TD><TD>n</TD><TD>o</TD><TD>p</TD><TD>q</TD><TD>r</TD><TD>s</TD><TD>t</TD><TD>u</TD><TD>v</TD></TR><TR><TD>z</TD></TR>"

これはあなたが必要なものですか?問題がないか聞いてください!

于 2012-06-15T15:12:08.773 に答える